Open essepuntato opened 5 days ago
# Code
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
# Test case
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
# Test runs
print(test_fib(0, 0)) # True
print(test_fib(1, 1)) # True
print(test_fib(2, 1)) # True
print(test_fib(6, 8)) # True
def test_fib(n,expected):
if fib(n) == 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(17,1597))
#True
print(test_fib(12,144))
#True
print(test_fib(15,610))
#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
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(test_fib(7,13)) #True
print(test_fib(10,55)) #True
print(test_fib(-1,0)) #True
print(test_fib(1,1)) #True
def test_fib(n, expected):
result = fib(n)
if result == expected:
return True
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
# Testing
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
# Code
def fib(n):
if n <= 0:
return 0
elif n <= 1:
return 1
else:
res = fib(n - 2) + fib(n - 1)
return res
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(4, 3))
print(fib(4))
print(fib(7))
def test_Fib(input_n, expected):
result=Fib(input_n)
if result!=expected:
return False
else:
return True
def Fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return Fib(n-1) + Fib(n-2)
print(test_Fib(0, 0))
# True
print(test_Fib(3, 2))
# True
print(test_Fib(10, 55))
# 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)
test_fib(6,8)
True
Define a recursive function
def fib(n)
that implements the algorithm to find the nth 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.