Created
January 8, 2013 00:32
-
-
Save allieus/4479945 to your computer and use it in GitHub Desktop.
Fibonachi Number
This file contains hidden or 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
def cache(fn): | |
_cached = {} | |
def wrap(*args, **kwargs): | |
key = repr((args, kwargs)) | |
if key not in _cached: | |
value = fn(*args, **kwargs) | |
_cached[key] = value | |
return _cached[key] | |
return wrap | |
@cache | |
def fib(n): | |
if n <= 0: | |
return 0 | |
elif n == 1: | |
return 1 | |
return fib(n-1) + fib(n-2) | |
import unittest | |
class TestFib(unittest.TestCase): | |
def test_fib(self): | |
self.assertEqual(fib(250), 7896325826131730509282738943634332893686268675876375) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment