-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
64 lines (53 loc) · 1.93 KB
/
fibonacci.py
File metadata and controls
64 lines (53 loc) · 1.93 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
# Note that with recursive solutions using memoization, it can't be as memory
# efficient as bottom-up because we need to store the entire collection of
# numbers as we work from the top down - otherwise we will be overwriting the calculation with different recursive calls using a pass-by-reference data type.
def fibonacci_memo_list(n: int, memo: list = None):
if n < 2: return n
if memo is None: memo = [None] * (n + 1)
if memo[n] is not None: return memo[n]
memo[n] = fibonacci_memo_list(n - 1, memo) + fibonacci_memo_list(n - 2, memo)
return memo[n]
def fibonacci_memo_dict(n: int, memo: dict = {0: 0, 1: 1, 2: 1}):
if n in memo: return memo[n]
memo[n] = fibonacci_memo_dict(n - 1, memo) + fibonacci_memo_dict(n - 2, memo)
# Memory optimization
if n > 2: del memo[n - 3]
return memo[n]
def fibonacci():
memo = {0: 0, 1: 1, 2: 1}
def fibonacci_with_closure(n: int):
nonlocal memo
if n in memo: return memo[n]
memo[n] = fibonacci_with_closure(n - 1) + fibonacci_with_closure(n - 2)
# Memory optimization
del memo[n - 3]
return memo[n]
return fibonacci_with_closure
# Bottom-up is more memory efficient because we only need to save the
# accumulator as we calculate it from the bottom up.
def fibonacci_bottom_up_tabulation(n: int):
if n < 2: return n
nums = [0, 1]
for i in range(2, n + 1):
nums += [nums[i - 1] + nums[i - 2]]
return nums[n]
def fibonacci_bottom_up_space_optimized(n: int):
if n < 2: return n
prev1 = 1
prev2 = 0
# Note that iterating over a range uses a generator instead of a list
for _ in range(2, n + 1):
curr = prev1 + prev2
prev2 = prev1
prev1 = curr
return prev1
def fibonacci_generator(n: int):
prev2 = 0
prev1 = 1
yield 0
yield 1
for _ in range(n - 1):
curr = prev1 + prev2
prev2 = prev1
prev1 = curr
yield prev1