Skip to content

Commit 512365b

Browse files
author
meshmote
committed
Added fibonacci and lucas series code and test blocks
1 parent ed25cb1 commit 512365b

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
1+
__author__ = 'Robert W. Perkins'
2+
3+
14
def fibonacci(n):
2-
"""Return the 'n'th value in the Fibonacci series"""
5+
"""Return the 'n'th value in the Fibonacci series"""
6+
7+
# test for input in valid range
8+
if n < 0:
9+
return None
10+
11+
# test for n == 0
12+
if n == 0:
13+
return 0
14+
15+
# base case is n == 1
16+
if n == 1:
17+
return 1
18+
else:
19+
return fibonacci(n-1) + fibonacci(n-2)
20+
21+
def lucas(n):
22+
"""Return the 'n'th value in the Lucas number series"""
23+
24+
# test for input in valid range
25+
if n < 0:
26+
return None
27+
28+
# test for n == 0
29+
if n == 0:
30+
return 2
31+
32+
# base case is n == 1
33+
if n == 1:
34+
return 1
35+
else:
36+
return lucas(n-1) + lucas(n-2)
37+
38+
# Testing Block
39+
if __name__ == "__main__":
40+
assert fibonacci(-3) is None
41+
assert fibonacci(0) == 0
42+
assert fibonacci(1) == 1
43+
assert fibonacci(17) == 1597
44+
assert lucas(-3) is None
45+
assert lucas(0) == 2
46+
assert lucas(1) == 1
47+
assert lucas(10) == 123

0 commit comments

Comments
 (0)