comp-think / 2024-2025

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2024/2025).
12 stars 0 forks source link

Lecture "Recursion", exercise 2 #27

Open essepuntato opened 5 days ago

essepuntato commented 5 days ago

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.

epistrephein commented 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
KikaYang commented 4 days ago
Screenshot 2024-11-25 at 19 32 59
maridematteis commented 4 days ago
Screenshot 2024-11-25 alle 19 32 49
mir-pin commented 4 days ago

image

ValkyrieCain9 commented 4 days ago
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
theair-hub commented 4 days ago

image

shiho1000 commented 4 days ago
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
lisitein commented 4 days ago
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)
截屏2024-11-26 15 25 05
martinamrc commented 4 days ago
# 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))
Screenshot 2024-11-26 alle 17 43 10
arinee01 commented 3 days ago
Снимок экрана 2024-11-26 в 20 03 46
justyna09549 commented 3 days ago
Zrzut ekranu 2024-11-26 o 10 21 37 PM
orboboro commented 3 days ago
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
digitalctrlv commented 2 days ago
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