forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoization.py
More file actions
35 lines (28 loc) · 751 Bytes
/
memoization.py
File metadata and controls
35 lines (28 loc) · 751 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
"""
Fibonacci implementation through cache.
"""
import inspect
def get_sequence(n):
"""
Return Fibonacci sequence from zero to specified number.
"""
cache = {0: 0, 1: 1}
def fib(num):
"""
Return Fibonacci value by specified number as integer.
"""
if num in cache:
return cache[num]
cache[num] = fib(num - 1) + fib(num - 2)
return cache[num]
def sequence(num):
"""
Return sequence of Fibonacci values as list.
"""
return [fib(value) for value in range(num + 1)]
return sequence(n)
def get_code():
"""
Return source code of Fibonacci sequence logic's implementation.
"""
return inspect.getsource(get_sequence)