Skip to content

Commit e1a6d73

Browse files
Fibonacci implementation (TODO)
1 parent 301481d commit e1a6d73

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

docs/Fibonacci.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=========
2+
Fibonacci
3+
=========
4+
5+
Learning fibonacci implementations in few ways!
6+
7+
Quick Start Guide
8+
-----------------
9+
10+
.. code-block:: python
11+
12+
from pygorithm.fibonacci import recursion as fib_recursion
13+
14+
sequence = fib_recursion.get_sequence(10)
15+
print(sequence) # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
16+
17+
Features
18+
--------
19+
20+
* Fibonacci implementations available:
21+
- Recursion
22+
- Cache (with save results of particular numbers)
23+
24+
* Get the code used for any of the implementation
25+
26+
.. code:: python
27+
28+
from pygorithm.fibonacci import recursion as fib_recursion
29+
30+
code = fib_recursion.get_code()
31+
print(code)
32+
33+
Implementations API
34+
-------------------
35+
36+
* Functions and their uses
37+
38+
.. function:: get_sequence(number)
39+
40+
- **number** : arbitrary integer, that need to be calculated in Fibonacci number type
41+
- **Return Value** : return Fibonacci value by specified number as integer
42+
43+
.. function:: get_code()
44+
45+
- **Return Value** : returns the code for the ``get_sequence()`` function

pygorithm/fibonacci/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from pygorithm.fibonacci import cache
2+
from pygorithm.fibonacci import recursion

pygorithm/fibonacci/cache.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Fibonacci implementation through cache.
3+
"""
4+
5+
import inspect
6+
7+
8+
def get_sequence(n):
9+
"""
10+
Return Fibonacci sequence from zero to specified number.
11+
"""
12+
cache = {0: 0, 1: 1}
13+
14+
def fib(n):
15+
"""
16+
Return Fibonacci value by specified number as integer.
17+
"""
18+
if n in cache:
19+
return cache[n]
20+
21+
cache[n] = fib(n - 1) + fib(n - 2)
22+
23+
return cache[n]
24+
25+
def sequence(n):
26+
"""
27+
Return sequence if Fibonacci values as list.
28+
"""
29+
return [fib(value) for value in range(n + 1)]
30+
31+
return sequence(n)
32+
33+
def get_code():
34+
"""
35+
Return source code of Fibonacci sequence logic's implementation.
36+
"""
37+
return inspect.getsource(get_sequence)

pygorithm/fibonacci/recursion.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Fibonacci implementation through recursion.
3+
"""
4+
5+
import inspect
6+
7+
8+
def get_sequence(n):
9+
"""
10+
Return Fibonacci sequence from zero to specified number as list.
11+
"""
12+
def fib(n):
13+
"""
14+
Return Fibonacci value by specified number as integer.
15+
"""
16+
if n == 0:
17+
return 0
18+
19+
if n == 1:
20+
return 1
21+
22+
return fib(n - 1) + fib(n - 2)
23+
24+
def sequence(n):
25+
"""
26+
Return sequence if Fibonacci values as list.
27+
"""
28+
return [fib(value) for value in range(n + 1)]
29+
30+
return sequence(n)
31+
32+
33+
def get_code():
34+
"""
35+
Return source code of Fibonacci sequence logic's implementation.
36+
"""
37+
return inspect.getsource(get_sequence)

tests/test_fibonacci.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Test for Fibonacci implementations logic.
3+
"""
4+
5+
import unittest
6+
7+
from pygorithm.fibonacci import cache, recursion
8+
9+
10+
class TestFibonacciImplementations(unittest.TestCase):
11+
"""
12+
Tests for Fibonacci implementations.
13+
"""
14+
15+
def test_implementations_same_result(self):
16+
"""
17+
Verify that all implementations have same result.
18+
"""
19+
fibonacci_implementations = [cache, recursion]
20+
21+
for implementation in fibonacci_implementations:
22+
result = getattr(implementation, 'get_sequence')(0)
23+
self.assertEqual([0], result)
24+
25+
result = getattr(implementation, 'get_sequence')(1)
26+
self.assertEqual([0, 1], result)
27+
28+
result = getattr(implementation, 'get_sequence')(10)
29+
self.assertEqual([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55], result)
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)