comp-think/2021-2022

Lecture "Recursion", exercise 2

Opened this issue ยท 26 comments

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.

image

def fib(n):
if n <= 0:
return 0
if n == 1:
return 1

else:
    return fib(n - 1) + fib(n - 2)
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
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)
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))
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))

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
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

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))  
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))

image

image
image

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))

fibonacci
resluts are
13
121393
True
True

Cattura

image

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

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

Schermata 2021-11-21 alle 18 08 47

Schermata 2021-11-21 alle 18 12 04

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))

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
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))

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

image

result:
True
True
True
True
True