Lecture "Recursion", exercise 2
Opened this issue ยท 26 comments
essepuntato commented
Define a recursive function def fib(n)
that implements the algorithm to find the nthe Fibonacci number. In particular, if n
is less than or equal to 0, then 0 is returned as a result. Otherwise, if n
is equal to 1, then 1 is returned. Otherwise, return the sum of the same function called with n-1
and n-2
as input. Please accompany the function with the related test case.
martasoricetti commented
11051620 commented
def fib(n):
if n <= 0:
return 0
if n == 1:
return 1
else:
return fib(n - 1) + fib(n - 2)
ManueleVeggi commented
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, 0))
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(5, 5))
print(test_fib(10,55))
The output is:
True
True
True
True
True
Postitisnt commented
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
def test_fib (n, expected):
if expected == fib(n):
return True
else:
return False
testF_1 = test_fib(7, 13)
testF_2 = test_fib(-2, 0)
testF_3 = test_fib(1, 1)
testF_4 = test_fib(0, 0)
tommasobattisti commented
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, 0))
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(7, 13))
giorgimariachiara commented
def test_fib(n, expected):
result = fib(n)
if expected == result:
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(3, 2))
CarmenSantaniello commented
def test_fib_n(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_n(4, 3))
print(test_fib_n(8, 21))
print(fib(4))
print(fib(8))
It returns:
True
True
3
21
OrsolaMBorrini commented
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(0,0)) #True
print(test_fib(1,1)) #True
print(test_fib(2,1)) #True
print(test_fib(12,144)) #True
AmeliaLamargese commented
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(-2, 0))
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(5, 5))
elizastuglik commented
def test_fib(n, expected):
result = fib(n)
if expected == result:
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(7, 13))
SaraVell1 commented
RebeccaJillianBeattie commented
olgagolgan commented
Bianca-LM commented
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(7,13))
print(test_fib(4,3))
MaddaGh commented
NoraPs commented
katya-avem commented
AnastasiyaSopyryaeva commented
Input
#Defining test function
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
#Function code
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
#print
print(test_fib(7, 13))
print(test_fib(9, 34))
print(test_fib(1, 0))
Output
True
True
False
francescabudel commented
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print(fib(7))
print(fib(1))
print(fib(0))
print(fib(9))
13
1
0
34
angstigone commented
chloeppd commented
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(-3,0))
print(test_fib(1,1))
print(test_fib(7,13))
sanyuezoe commented
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 <= 2:
return 1
else:
return fib(n-1)+fib(n-2)
print(test_fib(0, 0))
print(test_fib(2, 1))
print(test_fib(7, 13))
Output
True
True
True
ghasempouri1984 commented
def test_fib(n, expected):
result = fib(n)
if result == expected:
return True
else:
return False
def fib(n):
f = [0, 1]
for i in range(2, n + 1):
f.append(f[i - 1] + f[i - 2])
return f[n]
print(test_fib(0, 0))
print(test_fib(-2, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(9, 34))
essepuntato commented
Hi all, just a couple of notes:
- @ghasempouri1984 it should be a recursive function
- the tests are important (i.e. they should be provided) and they must pass
- please remember to format your code appropriately
teragramgius commented
ahsanv101 commented