Created
April 30, 2011 07:04
-
-
Save hakobe/949498 to your computer and use it in GitHub Desktop.
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
| from hashlib import sha1 | |
| memoize = lambda f, m = {}, key = lambda values:'-'.join([sha1(str(v)).hexdigest() for v in values]): lambda *xs, **ks: (lambda k = key([xs, ks]): m.get(k) or m.update( { k: f(*xs, **ks) } ) or m.get(k))() | |
| @memoize | |
| def fib(n): return 1 if (n == 0 or n == 1) else fib(n-1) + fib(n-2) | |
| assert fib(9) == 55 | |
| assert fib(55) == 225851433717 # cannot calculate without memoize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment