Skip to content

Instantly share code, notes, and snippets.

@diofeher
Created December 16, 2019 23:38
Show Gist options
  • Save diofeher/cd653f398391d9c5449e9cd567bb49e3 to your computer and use it in GitHub Desktop.
Save diofeher/cd653f398391d9c5449e9cd567bb49e3 to your computer and use it in GitHub Desktop.
class Memoization(object):
def __init__(self, func):
self.func = func
self.data = {}
def __call__(self, n, **kwargs):
if n in self.data:
return self.data[n]
self.data[n] = self.func(n)
return self.data[n]
@Memoization
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
# Decorator
print(fib(50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment