-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoization_github.py
67 lines (49 loc) · 2.05 KB
/
memoization_github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing
#the results of expensive function calls and returning the cached result when the same inputs occur again.
#YWikipedia, “Memoization”,https://en.wikipedia.org/wiki/Memoization#
def classic_fib(n):
if n>0 and n<=2:
return 1
else:
return classic_fib(n-2) + classic_fib(n-1)
#(it does not calculate bşg number quickly)
print("Classic fib function (3. step):", classic_fib(3))
def memoization_fib(n):
if n<=2:
return 1
else:
if not n in mem:
mem[n]= memoization_fib(n-2) + memoization_fib(n-1)
return mem[n]
mem = {} #Python dictionary
print("Fib function that use memoization in simple way (50. step):", memoization_fib(50))
memo = {}
def memoize_decorators(f): # f peresent function in this case it is fib_decorators
def wrapper(*args):
if args not in memo:
memo[args] = f(*args)
return memo[args]
return wrapper # return the wrapper to call wrapper otherwise wrapper does not run
@memoize_decorators # Every time fib_decorators func call the memoize_decorators call
# ---> if you have any struggle to understand decorators you can research first-class function and closures first
def fib_decorators(n):
if n <=2:
return 1
else:
return fib_decorators(n-1) + fib_decorators(n-2)
print("Fib function that use memoization with decorators (50. step):", fib_decorators(50))
class Memoize_class:
def __init__(self, fn):
self.fn = fn
self.memo = {}
def __call__(self, *args):
if args not in self.memo:
self.memo[args] = self.fn(*args)
return self.memo[args]
@Memoize_class
def fib_class_decorators(n):
if n<=2:
return 1
else:
return fib_class_decorators(n-1) + fib_class_decorators(n-2)
print("Fib function that use deorators in class (50. step):",fib_class_decorators(50))