Created
July 13, 2013 10:37
-
-
Save whiteclover/5990289 to your computer and use it in GitHub Desktop.
memozie python
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 memoize: | |
# class as decorator | |
def __init__(self, function): | |
self.function = function | |
self.memoized = {} | |
def __call__(self, *args): | |
try: | |
return self.memoized[args] | |
except KeyError: | |
self.memoized[args] = self.function(*args) | |
return self.memoized[args] | |
@memoize | |
def fibonacci_memoized(n): | |
if n in (0, 1): return n | |
return fibonacci_memoized(n - 1) + fibonacci_memoized(n - 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment