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
36 lines (29 loc) · 770 Bytes
/
memoization.py
File metadata and controls
36 lines (29 loc) · 770 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
"""
Fibonacci implementation through cache.
"""
import inspect
# TODO: Fix shadowed parameter names
def get_sequence(n):
"""
Return Fibonacci sequence from zero to specified number.
"""
cache = {0: 0, 1: 1}
def fib(n):
"""
Return Fibonacci value by specified number as integer.
"""
if n in cache:
return cache[n]
cache[n] = fib(n - 1) + fib(n - 2)
return cache[n]
def sequence(n):
"""
Return sequence of Fibonacci values as list.
"""
return [fib(value) for value in range(n + 1)]
return sequence(n)
def get_code():
"""
Return source code of Fibonacci sequence logic's implementation.
"""
return inspect.getsource(get_sequence)