Created
December 16, 2019 23:38
-
-
Save diofeher/cd653f398391d9c5449e9cd567bb49e3 to your computer and use it in GitHub Desktop.
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 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