Skip to content

Commit 42ed73f

Browse files
committed
feat(day 3): add fibonacci sequence generator with dynamic starting values
1 parent 0d302a8 commit 42ed73f

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Fibonacci Sequence
3+
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.
4+
When starting with 0 and 1, the first 10 numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
5+
6+
Given an array containing the first two numbers of a Fibonacci sequence, and an integer representing
7+
the length of the sequence, return an array containing the sequence of the given length.
8+
9+
Your function should handle sequences of any length greater than or equal to zero.
10+
If the length is zero, return an empty array.
11+
Note that the starting numbers are part of the sequence.
12+
===================================================================================
13+
O/P ==============>
14+
15+
1. fibonacci_sequence([0, 1], 20) should return [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181].
16+
2. fibonacci_sequence([21, 32], 1) should return [21].
17+
"""
18+
19+
def fibonacci_sequence(start_sequence,length):
20+
21+
if length == 0:
22+
return []
23+
if length == 1:
24+
return [start_sequence[0]]
25+
if length == 2:
26+
return start_sequence[:2]
27+
28+
result = start_sequence[:2]
29+
while len(result) < length:
30+
result.append(result[-1] + result[-2])
31+
return result
32+
33+
34+
if __name__ == "__main__":
35+
print(fibonacci_sequence([0, 1], 20))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from fiboSequence import fibonacci_sequence
3+
4+
5+
class fiboSequenceTest(unittest.TestCase):
6+
7+
def test1(self):
8+
self.assertEqual(fibonacci_sequence([0, 1], 20),[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181])
9+
10+
def test2(self):
11+
self.assertEqual(fibonacci_sequence([21, 32], 1),[21])
12+
13+
def test3(self):
14+
self.assertEqual(fibonacci_sequence([0, 1], 0),[])
15+
16+
def test4(self):
17+
self.assertEqual(fibonacci_sequence([10, 20], 2),[10,20])
18+
19+
def test5(self):
20+
self.assertEqual(fibonacci_sequence([123456789, 987654321], 5),[123456789, 987654321, 1111111110, 2098765431, 3209876541])
21+
22+
23+
if __name__ == "__main__":
24+
unittest.main()

0 commit comments

Comments
 (0)