-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Recursion", exercise 2 #25
Labels
Comments
# Test case for the algorithm
def test_fib(int_1, expected):
result = fib(int_1)
if result == expected:
return True
else:
return False
# Code for the algorithm
def fib(n):
if n <= 0:
return 0
if n ==1:
return 1
else:
return fib(n-1)+fib(n-2)
# Three test runs
print(test_fib(2, 1))
print(test_fib(16, 987))
print(test_fib(24, 46368))
# Console results
True
True
True |
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
def fib(n):
if n <= 0:
return 0
if n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(test_fib(1,1))
print(test_fib(2,1))
print(test_fib(6, 8))
print(test_fib(0,0)) |
def test_my_fib(n, expected):
result = fib(n)
if result == expected:
return True
else:
return False
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(test_my_fib(19, 4181))
print(test_my_fib(22, 17711))
print(test_my_fib(4, 3))
#Console output
True
True
True |
def test_fibonacci(n,expected):
if fibonacci(n) == expected:
return True
else:
return False
def fibonacci(n):
if n <= 0:
result = 0
elif n == 1:
result = 1
else:
result = fibonacci(n-1) + fibonacci(n-2)
return result
print(test_fibonacci(0,0)) #True
print(test_fibonacci(1,1)) #True
print(test_fibonacci(4,3)) #True
print(test_fibonacci(7,13)) #True |
|
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
def fib(n):
if n <= 0:
return 0
if n == 1:
return 1
else:
return fib(n-1)+fib(n-2)
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(-1, 0))
print(test_fib(2, 1))
print(test_fib(12, 144)) |
|
def test_fib(n, expected):
result = fib(n)
if result == expected:
return True
else:
return False
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(4, 3))
print(test_fib(11, 89)) |
|
|
|
|
|
|
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Define a recursive function
def fib(n)
that implements the algorithm to find the nthe Fibonacci number. In particular, ifn
is less than or equal to 0, then 0 is returned as a result. Otherwise, ifn
is equal to 1, then 1 is returned. Otherwise, return the sum of the same function called withn-1
andn-2
as input. Please accompany the function with the related test case.The text was updated successfully, but these errors were encountered: