Skip to content

Instantly share code, notes, and snippets.

@cocoatomo
Created May 8, 2010 02:11
Show Gist options
  • Save cocoatomo/394248 to your computer and use it in GitHub Desktop.
Save cocoatomo/394248 to your computer and use it in GitHub Desktop.
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