Created
May 8, 2010 02:11
-
-
Save cocoatomo/394248 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InfiniteList: | |
def __init__(self, init_val=1, next_val=1): | |
self.real_list = [] | |
self.init_val = init_val | |
self.diff = next_val - init_val | |
def __getitem__(self, index): | |
if index < 0: | |
raise IndexError('Out of index range.') | |
if index < len(self.real_list): | |
return self.real_list[index] | |
length = len(self.real_list) | |
if length == 0: | |
self.real_list.append(self.init_val) | |
while length <= index: | |
self.real_list.append(self.real_list[-1] + self.diff) | |
length += 1 | |
return self.real_list[index] | |
def __str__(self): | |
return str(self.real_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment