FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm-examples/python-algorithm/algorithm/dp/fibonacci.py at main · codejsha/algorithm-examples · GitHub
codejsha
/
algorithm-examples
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
2
Code
Issues
0
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm-examples
/
python-algorithm
/
algorithm
/
dp
/
fibonacci.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (63 loc) · 2.2 KB
Breadcrumbs
algorithm-examples
/
python-algorithm
/
algorithm
/
dp
/
fibonacci.py
Copy path
File metadata and controls
75 lines (63 loc) · 2.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
65
66
67
68
69
70
71
72
73
74
75
from
functools
import
lru_cache
from
typing
import
Dict
from
typing
import
Generator
def
fibonacci_recursive
(
n
:
int
)
->
int
:
"""
Calculate the Fibonacci number of the given number.
:param n: input number
:return: Fibonacci number
"""
if
n
<
2
:
# base case
return
n
return
fibonacci_recursive
(
n
-
2
)
+
fibonacci_recursive
(
n
-
1
)
# recursive case
def
fibonacci_memoization
(
n
:
int
)
->
int
:
"""
Calculate the Fibonacci number of the given number using memoization.
:param n: input number
:return: Fibonacci number
"""
memo
:
Dict
[
int
,
int
]
=
{
0
:
0
,
1
:
1
}
# our base cases
def
helper
(
m
:
int
)
->
int
:
if
m
not
in
memo
:
memo
[
m
]
=
helper
(
m
-
1
)
+
helper
(
m
-
2
)
# memoization
return
memo
[
m
]
return
helper
(
n
)
@
lru_cache
(
maxsize
=
None
)
def
fibonacci_automatic_memoization
(
n
:
int
)
->
int
:
"""
Calculate the Fibonacci number of the given number using automatic memoization.
It uses the @lru_cache decorator to cache the results of the function.
It has same definition as :func:`fibonacci_recursive`.
:param n: input number
:return: Fibonacci number
"""
if
n
<
2
:
# base case
return
n
return
fibonacci_recursive
(
n
-
2
)
+
fibonacci_recursive
(
n
-
1
)
# recursive case
def
fibonacci_iterative
(
n
:
int
)
->
int
:
"""
Calculate the Fibonacci number of the given number using iterative approach.
:param n: input number
:return: Fibonacci number
"""
if
n
==
0
:
return
n
# special case
_last
:
int
=
0
# initially set to fib(0)
_next
:
int
=
1
# initially set to fib(1)
for
_
in
range
(
1
,
n
):
_last
,
_next
=
_next
,
_last
+
_next
return
_next
def
fibonacci_generator
(
n
:
int
)
->
Generator
[
int
,
None
,
None
]:
"""
Calculate the Fibonacci number of the given number using generator approach.
:param n: input number
:return: generator of Fibonacci numbers
"""
yield
0
# special case
if
n
>
0
:
yield
1
# special case
_last
:
int
=
0
# initially set to fibonacci(0)
_next
:
int
=
1
# initially set to fibonacci(1)
for
_
in
range
(
1
,
n
):
_last
,
_next
=
_next
,
_last
+
_next
yield
_next
# main generation step
Back
|
FazBrowse Home
|
New Git URL