forked from CMU-313/github-recitation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fib.py
30 lines (22 loc) · 774 Bytes
/
fib.py
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
"""
Fibonacci number generator
When given a position, the function returns the fibonacci at that position in the sequence.
The zeroth number in the fibonacci sequence is 0. The first number is 1
Negative numbers should return None
"""
def fibonacci(position):
if position < 0:
return None
if(position == 0):
return 0
if(position == 1):
return 1
return fibonacci(position - 1) + fibonacci(position - 2)
# Test cases
print("The 1st Fibonacci number: ", fibonacci(1))
print("The 21st Fibonacci number: ", fibonacci(21))
assert(fibonacci(0) == 0)
print("The 0th Fibonacci number: ", fibonacci(0)) # should return 0
assert(fibonacci(-1) == None)
print("The -1st Fibonacci number: ", fibonacci(-1)) # should return None
print("Code ran successfully!")