comp-think / 2020-2021

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. 2020/2021).
14 stars 9 forks source link

Lecture "Recursion", exercise 2 #25

Open essepuntato opened 3 years ago

essepuntato commented 3 years 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.

diegochillo commented 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))
SusannaPinotti commented 3 years 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)) #returns True
print(test_fib(1, 1)) #returns True
print(test_fib(19, 4181)) #returns Tru
```e 
dbrembilla commented 3 years ago
#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
gabrielefiorenza commented 3 years ago
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
AlessandraFa commented 3 years ago
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
IlaRoss commented 3 years ago
#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)
ChiaraCati commented 3 years ago
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))
AleRosae commented 3 years ago
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))
yunglong28 commented 3 years ago
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))
edoardodalborgo commented 3 years ago
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))
fcagnola commented 3 years 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.

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
SofiBar commented 3 years ago
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))
giorgiasampo commented 3 years ago
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))
vanessabonanno commented 3 years ago
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"
laurentfintoni commented 3 years ago
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))
AlessandroBertozzi commented 3 years ago
# 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)))
essepuntato commented 3 years ago

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

dbrembilla commented 3 years ago

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

Hello,

In my case it returned 0 with negative numbers, as expected

diegochillo commented 3 years ago

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

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))
GiuliaMenna commented 3 years 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)
    return result

print(test_fib(0,0))
print(test_fib(1,1))
print(test_fib(7,13))