Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active November 18, 2018 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ratstail91/3a952cd2e03d3a564f3be0cae83c3cfd to your computer and use it in GitHub Desktop.
Save Ratstail91/3a952cd2e03d3a564f3be0cae83c3cfd to your computer and use it in GitHub Desktop.
# 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