Created
April 3, 2015 20:15
-
-
Save jvantuyl/6bea532ac6d5743e1b4d to your computer and use it in GitHub Desktop.
Fibonnaci Series List
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 FibSeries(list): | |
def __init__(self): | |
bare_self = super(FibSeries, self) | |
bare_self.__setslice__(0, bare_self.__len__(), (1, 1,)) | |
def __getitem__(self, i): | |
bare_self = super(FibSeries, self) | |
bare_len = bare_self.__len__() | |
if not i < bare_len: | |
self._fill_fib(bare_len, i) | |
return bare_self.__getitem__(i) | |
def _fill_fib(self, start, end): | |
bare_self = super(FibSeries, self) | |
bare_get = bare_self.__getitem__ | |
bare_append = bare_self.append | |
assert start > 1 | |
a, b = None, bare_get(start - 2) | |
for i in range(start, end + 1): | |
a, b = b, bare_get(i - 1) | |
bare_append(a + b) | |
def __repr__(self): | |
return '<FibSeries(0..{})>'.format(super(FibSeries, self).__len__() - 1) | |
def __setitem__(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def __setslice__(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def __delitem__(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def __delslice__(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def __len__(*args, **kwargs): raise RuntimeError('FibSeries are (effectively) infinite') | |
def insert(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def append(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def pop(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def remove(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def reverse(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
def sort(*args, **kwargs): raise RuntimeError('FibSeries are immutable') | |
if __name__ == '__main__': | |
f = FibSeries() | |
assert f[0] == 1 | |
assert f[1] == 1 | |
assert f[2] == 2 | |
assert f[3] == 3 | |
assert f[4] == 5 | |
assert f[5] == 8 | |
assert f[6] == 13 | |
assert f[7] == 21 | |
assert f[268] == 73822750993122698578207436143903804565580923764844306069 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment