-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci_dict_wqu.py
44 lines (39 loc) · 992 Bytes
/
fibonacci_dict_wqu.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
def fibonacci_recursive(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return (fibonacci_recursive(n-1) +
fibonacci_recursive(n-2))
from collections import defaultdict
def fibonacci_count(n, d):
d[n] += 1
if n == 0:
return 0, d
elif n == 1:
return 1, d
else:
n1, _ = fibonacci_count (n-1, d)
n2, _ = fibonacci_count (n-2, d)
return n1+n2, d
n = 5
ans, d = fibonacci_count(n, defaultdict(int))
for i in range(5):
print(i, d[i])
def fibonacci_memo(n, d):
if n in d:
return d[n]
elif n == 0:
ans = 0
elif n == 1:
ans = 1
else:
ans = (fibonacci_recursive(n-1, d) +
fibonacci_recursive(n-2, d))
d[n] = ans
return ans
def percentile (data, rank):
data = sorted(data)
idx = int(len(data) * (rank/100))
return f'The {rank} percentile is {data[idx]}'