Skip to content

Commit 844234b

Browse files
committed
Inital solution for fibonacci sequence
1 parent 48a4ed2 commit 844234b

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Optional, List
2+
3+
# Recursive fuction
4+
def fib_rec(n: int) -> int:
5+
# base case
6+
if n == 0 or n == 1:
7+
return n
8+
return fib_rec(n - 1) + fib_rec(n - 2)
9+
10+
11+
# Dynamic function
12+
def fib_dyn(n: int) -> int:
13+
# base case
14+
if n == 0 or n == 1:
15+
return n
16+
# Instantiate Cache information
17+
18+
cache_size = 10
19+
cache: List[Optional[int]]
20+
cache = [None] * (cache_size + 1)
21+
22+
cache[0] = 0
23+
cache[1] = 1
24+
current_num = 2
25+
26+
while current_num < n:
27+
cache[current_num % cache_size] = (
28+
cache[(current_num - 1) % cache_size]
29+
+ cache[(current_num - 2) % cache_size]
30+
)
31+
current_num += 1
32+
33+
return cache[(n - 1) % cache_size] + cache[(n - 2) % cache_size]
34+
35+
36+
# Iterative function
37+
def fib_iter(n: int) -> int:
38+
# base case
39+
if n == 0 or n == 1:
40+
return n
41+
phi = (1 + 5 ** 0.5) / 2
42+
psi = 1 - phi
43+
return round((phi ** n - psi ** n) / 5 ** 0.5)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
3+
from fibonacci import fib_rec, fib_dyn, fib_iter
4+
5+
6+
class FibonacciTest(unittest.TestCase):
7+
def test_rec(self):
8+
self.assertEqual(fib_rec(10), 55)
9+
self.assertEqual(fib_rec(1), 1)
10+
self.assertEqual(fib_rec(23), 28657)
11+
12+
def test_dyn(self):
13+
self.assertEqual(fib_dyn(10), 55)
14+
self.assertEqual(fib_dyn(1), 1)
15+
self.assertEqual(fib_dyn(23), 28657)
16+
17+
def test_iter(self):
18+
self.assertEqual(fib_iter(10), 55)
19+
self.assertEqual(fib_iter(1), 1)
20+
self.assertEqual(fib_iter(23), 28657)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Fibonnaci Sequence
2+
3+
## Problem
4+
5+
Implement a [Fibonnaci Sequence](https://en.wikipedia.org/wiki/Fibonacci_number) in three different ways:
6+
7+
- Recursively
8+
- Dynamically (Using Memoization to store results)
9+
- Iteratively
10+
11+
Your function will accept a number **n** and return the **nth** number of the fibonacci sequence
12+
13+
Remember that a fibonacci sequence: 0,1,1,2,3,5,8,13,21,... starts off with a base case checking to see if n = 0 or 1, then it returns 1.
14+
15+
Else it returns fib(n-1)+fib(n+2).
16+
17+
## Code
18+
19+
### Recursively
20+
21+
Solve the problem using simple recursion.
22+
23+
```python
24+
def fib_rec(n):
25+
pass
26+
```
27+
28+
### Dynamically
29+
30+
Implement the function using dynamic programming by using a cache to store results (memoization).
31+
32+
```python
33+
# Instantiate Cache information
34+
n = 10
35+
cache = [None] * (n + 1)
36+
37+
38+
def fib_dyn(n):
39+
pass
40+
```
41+
42+
### Iteratively
43+
44+
Implement the solution with simple iteration.
45+
46+
```python
47+
def fib_iter(n):
48+
pass
49+
```

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Each problem contains the problem description and unit tests in a separate file.
3434
1. [Word Split](https://github.com/orfeasa/data-structure-problems/tree/master/04.%20Recursion/04-01%20Word%20Split)
3535
1. [Reverse String](https://github.com/orfeasa/data-structure-problems/tree/master/04.%20Recursion/04-02%20Reverse%20a%20String)
3636
1. [String Permutation](https://github.com/orfeasa/data-structure-problems/tree/master/04.%20Recursion/04-03%20String%20Permutation)
37-
1. Fibonacci Sequence
37+
1. [Fibonacci Sequence](https://github.com/orfeasa/data-structure-problems/tree/master/04.%20Recursion/04-04%20Fibonacci%20Sequence)
3838
1. Coin Change
3939

4040
### 5. Trees

0 commit comments

Comments
 (0)