-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfibonacci.py
More file actions
40 lines (33 loc) · 812 Bytes
/
fibonacci.py
File metadata and controls
40 lines (33 loc) · 812 Bytes
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
# A naive recursive solution
def fib(n):
if n == 1 or n == 2:
result = 1
else:
result = fib(n-1) + fib(n-2)
return result
# A memoized solution
def fib_2(n, memo):
if memo[n] is not None:
return memo[n]
if n == 1 or n == 2:
result = 1
else:
result = fib_2(n-1, memo) + fib_2(n-2, memo)
memo[n] = result
return result
def fib_memo(n):
memo = [None] * (n + 1)
return fib_2(n, memo)
# A bottom-up solution
def fib_bottom_up(n):
if n == 1 or n == 2:
return 1
bottom_up = [None] * (n+1)
bottom_up[1] = 1
bottom_up[2] = 1
for i in range(3, n+1):
bottom_up[i] = bottom_up[i-1] + bottom_up[i-2]
return bottom_up[n]
V = {'a', 'b', 'c', 'd', 'e'}
E = {'ab', 'ac', 'bd', 'cd', 'de'}
dic = zip(v,)