Last active
November 18, 2018 14:13
-
-
Save Ratstail91/3a952cd2e03d3a564f3be0cae83c3cfd 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
# fibonacci sequence, memoized | |
def memoize(func, arg): | |
if func not in memoize.__dict__: | |
memoize.__dict__[func] = {} | |
if arg not in memoize.__dict__[func]: | |
memoize.__dict__[func][arg] = func(arg) | |
return memoize.__dict__[func][arg] | |
def fib(i): | |
if i < 2: | |
return i | |
return memoize(fib, i-1) + memoize(fib, i-2) | |
print(fib(12)) | |
print(memoize.__dict__) #for debugging |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment