Open essepuntato opened 3 years ago
def test_fib(n,expected):
result=fib(n)
return result==expected
def fib(n):
if n<2:
return n
else:
return fib(n-1)+fib(n-2)
print(test_fib(7,13))
print(test_fib(6,8))
print(test_fib(8,21))
print(test_fib(0,0))
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)) #returns True
print(test_fib(1, 1)) #returns True
print(test_fib(19, 4181)) #returns Tru
```e
#test
def test(n, expected):
return fib(n) == expected
#fibonacci
def fib(n):
if n <= 0:
return 0
elif n < 2:
return 1
else:
return fib(n-1) + fib(n-2)
print(test(10, 55)) # True
print(test(8, 21)) # True
print(test(1, 1)) # True
def test_fib(n,expected):
result = fib(n)
return result == expected
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)) #returns True
print(test_fib(1,1))#returns True
print(test_fib(0,0))#returns True
def test_fib(n, expected):
return expected == fib(n)
def fib(n):
if n <= 0:
return 0
if n == 1:
return 1
else:
return fib(n-1)+fib(n-2)
print(test_fib(4, 3)) #True
print(test_fib(-2, 0)) #True
print(test_fib(1, 1)) #True
#tests
def test_fibonacci(n, expected):
result = fibonacci(n)
if result == expected:
return True
else:
return False
#algorithm with recursion
def fibonacci(n):
if n == 1:
return 1
if n <= 0:
return 0
else:
return fibonacci(n-1) + fibonacci(n-2)
#test runs
a = test_fibonacci(8, 21)
b = test_fibonacci(5, 5)
c = test_fibonacci(1, 1)
d = test_fibonacci(-3, 0)
print(a)
print(b)
print(c)
print(d)
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(-5, 0))
print(test_fib(0,0))
print(test_fib(1,1))
print(test_fib(2,1))
print(test_fib(3,2))
print(test_fib(4,3))
def test_fin(n, expected):
return expected == fin(n)
def fin(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fin(n-1) + fin(n-2)
print(test_fin(7,13))
print(test_fin(1,1))
print(test_fin(-7,0))
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(7, 13))
def test_fib(n, expected):
return fib(n) == expected
def fib(n):
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)
print(test_fib(3, 2))
print(test_fib(5, 5))
print(test_fib(10, 55))
# Define a recursive function def fib(n) that implements the algorithm to find the nth 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.
from test import test_1_parameter
def fib(number):
if number <= 0:
return 0
elif number == 1:
return 1
else:
return fib(number-1) + fib(number-2)
print(test_1_parameter(fib,3,2)) # returns True
print(test_1_parameter(fib,7,13)) # returns True
def test_fib(n, expected):
return fib(n) == expected
def fib(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1)+fib(n-2)
print(test_fib(4, 3))
print(test_fib(6, 8))
print(test_fib(0, 0))
def test_fib(n,expected):
result = fib(n)
return result == expected
def fib(n):
if n <= 0:
return 0
else:
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(4,3))
print (test_fib(6,8))
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
elif n > 1:
return fib(n - 1) + fib(n - 2)
else:
return "Wrong input"
print(fib(7)) # 13
print(fib(4)) # 3
print(test_fib(7, 13)) # True
print(test_fib(1, 1)) # True
print(test_fib(0, 0)) # True
print(test_fib(4, 4)) # False
# With big inputs the recursion may take a long time.
# PythonTutor shows the following alert with input = 77:
# "Stopped since stack has 30 functions on it.
# You may have infinite recursion"
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(4, 3))
print(test_fib(1, 1))
print(test_fib(0, 0))
# Test case for the algorithm
def test_fib(n, expected):
result = fib(n)
if expected == result:
return True
else:
return False
# Code of the algorithm
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))
print(test_fib(1, 1))
print((test_fib(0, 0)))
Hi all,
Thanks for your solution. In your tests, can I suggest to add also an execution with negative input, e.g. fib(-4)
? Can you try to see if your function still behaves as expected (i.e. returning 0
in these cases)?
Hi all,
Thanks for your solution. In your tests, can I suggest to add also an execution with negative input, e.g.
fib(-4)
? Can you try to see if your function still behaves as expected (i.e. returning0
in these cases)?
Hello,
In my case it returned 0
with negative numbers, as expected
Hi all,
Thanks for your solution. In your tests, can I suggest to add also an execution with negative input, e.g.
fib(-4)
? Can you try to see if your function still behaves as expected (i.e. returning0
in these cases)?
My function didn't work well with negatives. Here is a reviewed version:
def test_fib(n,expected):
result=fib(n)
return result==expected
def fib(n):
if 0<=n<2:
return n
elif n>1:
return fib(n-1)+fib(n-2)
return 0
# Test cases
print(test_fib(7,13))
print(test_fib(6,8))
print(test_fib(8,21))
print(test_fib(0,0))
print(test_fib(-4,0))
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)
return result
print(test_fib(0,0))
print(test_fib(1,1))
print(test_fib(7,13))
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.