-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
73 lines (59 loc) · 2.03 KB
/
fibonacci.py
File metadata and controls
73 lines (59 loc) · 2.03 KB
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
68
69
70
71
72
73
from functools import lru_cache
class Fibonacci:
fibonacci_cache = {}
def normal(self, n):
# Check that the input is a positive integer
if type(n) != int:
raise TypeError("n must be positive integer")
if n < 1:
raise TypeError("n must be positive integer")
# Compute the Nth item
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return self.normal(n - 1) + self.normal(n - 2)
def with_custom_memoization(self, n):
# Check that the input is a positive integer
if type(n) != int:
raise TypeError("n must be positive integer")
if n < 1:
raise TypeError("n must be positive integer")
# If we have cached the value, then return it
if n in self.fibonacci_cache:
return self.fibonacci_cache[n]
# Compute the Nth item
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
value = self.with_custom_memoization(n - 1) + self.with_custom_memoization(n - 2)
# Cache the value and return it
self.fibonacci_cache[n] = value
return value
@lru_cache(maxsize=1000)
def with_built_in_cache(self, n):
# Check that the input is a positive integer
if type(n) != int:
raise TypeError("n must be positive integer")
if n < 1:
raise TypeError("n must be positive integer")
# Compute the nth term
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return self.with_built_in_cache(n - 1) + self.with_built_in_cache(n - 2)
obj = Fibonacci()
print('=========== Normal ===========')
for n in range(1, 10):
print(obj.normal(n))
print('=========== Normal Cache ===========')
for n in range(1, 10):
print(obj.with_custom_memoization(n))
print('=========== Builtin Cache ===========')
for n in range(1, 10):
print(obj.with_built_in_cache(n))