1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- def helper(got, expect):
- if got == expect:
- print True
- else:
- print False, expect, got
- class Squares:
- def __init__(self, max):
- self.max = max
- self.sofar = []
- def __len__(self): return len(self.sofar)
- def __getitem__(self, i):
- if not 0 <= i < self.max: raise IndexError
- n = len(self.sofar)
- while n <= i:
- self.sofar.append(n*n)
- n += 1
- return self.sofar[i]
- class Counter:
- class CounterIterator:
- def __init__(self, c):
- self.count = 0
- self.c = c
- def next(self):
- self.count += 1
- if self.count < self.c.stop:
- return self.count
- raise StopIteration
- def __iter__(self):
- return self
- def __init__(self, stop):
- self.count = 0
- self.stop = stop
- def __iter__(self):
- return self.CounterIterator(self)
- helper(sum([]), 0)
- helper(sum(range(2,8)), 27)
- helper(sum([[1], [2], [3]], []), [1, 2, 3])
- helper(sum([[1,2],[3,4]],[5,6]), [5, 6, 1, 2, 3, 4])
- helper(sum(((1,2,3),(4,5)),(6,7)),(6, 7, 1, 2, 3, 4, 5))
- helper(sum(Counter(10), 5), 50)
|