Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture "Recursion", exercise 2 #25

Open
essepuntato opened this issue Nov 21, 2022 · 16 comments
Open

Lecture "Recursion", exercise 2 #25

essepuntato opened this issue Nov 21, 2022 · 16 comments
Labels

Comments

@essepuntato
Copy link
Contributor

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.

@delete4ever
Copy link

# Test case for the algorithm
def test_fib(int_1, expected):
    result = fib(int_1)
    if result == expected:
        return True
    else:
        return False
# Code for the algorithm
def fib(n):
    if n <= 0:
        return 0
    if n ==1:
        return 1
    else:
        return fib(n-1)+fib(n-2)
# Three test runs
print(test_fib(2, 1))
print(test_fib(16, 987))
print(test_fib(24, 46368))
# Console results
True
True
True

@giorgiacrosilla
Copy link

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(1,1))
print(test_fib(2,1))
print(test_fib(6, 8))
print(test_fib(0,0))

@lucia1299
Copy link

def test_my_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_my_fib(19, 4181))
print(test_my_fib(22, 17711))
print(test_my_fib(4, 3))

#Console output
True
True
True

@ranacoskun
Copy link

def test_fibonacci(n,expected):
  if fibonacci(n) == expected:
    return True
  else:
    return False

def fibonacci(n):
  if n <= 0:
    result = 0
  elif n == 1:
    result = 1
  else:
    result = fibonacci(n-1) + fibonacci(n-2)
  return result    

print(test_fibonacci(0,0)) #True
print(test_fibonacci(1,1)) #True
print(test_fibonacci(4,3)) #True
print(test_fibonacci(7,13)) #True

@mary-lev
Copy link

mary-lev commented Nov 22, 2022

def fib(n):
    # n can't be less than 0
    if n == 0 or n == 1:
        return n
    return fib(n - 1) + fib(n - 2)

def test_fib(n, expected):
    if fib(n) == expected:
        return True
    return False

print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(4, 3))
print(test_fib(12, 144))
print(test_fib(19, 4181))

@ChiaraParravicini
Copy link

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(0, 0))
print(test_fib(1, 1))
print(test_fib(-1, 0))
print(test_fib(2, 1))
print(test_fib(12, 144))

@corrado877
Copy link

# Define Test Algorithm
def test_fib(n, expected):
    result= fib(n)
    if result==expected:
        return True
    else:
        return False
    
# Code of Fibonacci Seuquence Algorithm
def fib(n):
    if n<=0:
        return 0
    elif n==1:
        return 1
    else:
        return fib(n-1) + fib (n-2)

# Five Test runs
print (test_fib(6, 8))
print (test_fib(11, 89))
print (test_fib( 0, 0))
print (test_fib( 1, 1))
print (test_fib(-5, 0 ))

@lvcasmariani
Copy link

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, 1))
print(test_fib(2, 1))
print(test_fib(4, 3))
print(test_fib(11, 89))

@EricaAndreose
Copy link

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 runs that returns True
print(test_fib(0, 0))
print(test_fib(1, 1))
print(test_fib(2, 1))
print(test_fib(3, 2))
print(test_fib(7, 13))

@NicoleLiggeri
Copy link

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, 2))
print(test_fib(0, 0))
print(test_fib(9, 34))

@n1kg0r
Copy link

n1kg0r commented Nov 22, 2022

def test_fib(n, expected):
    return expected == fib(n)

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(13, 233)) # True
print(test_fib(0, 0)) # True

@irematmar
Copy link

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(10, 55))
print(test_fib(1, 1))
print(test_fib(0, 0))
print(test_fib(4, 3))

True
True
True
True

@matteo-guenci
Copy link

def test_fib(n, expected):
    result = fib(n)
    if result == expected:
        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(5, 5))
print(test_fib(7, 13))
print(test_fib(8, 21))
print(test_fib(9, 34))

@eugeniavd
Copy link

image

@alka2696
Copy link

# Test case for the algorithm
def test_fib(int_1, expected):
    result = fib(int_1)
    if result == expected:
        return True
    else:
        return False
# Code for the algorithm
def fib(n):
    if n <= 0:
        return 0
    if n ==1:
        return 1
    else:
        return fib(n-1)+fib(n-2)
# Three test runs
print(test_fib(2, 1)) #True
print(test_fib(1, 1)) #True
print(test_fib(4, 3)) #True

@SalvatoreDiMarzo
Copy link

SalvatoreDiMarzo commented Nov 29, 2022

def test_fib(n,expected):
  if fib(n) == expected:
    return True
  else:
    return False

def fib(n):
  if n <= 0:
    result = 0
  elif n == 1:
    result = 1
  else:
    result = fib(n-1) + fib(n-2)
  return result    

print(test_fib(5, 5))
print(test_fib(2, 1))
print(test_fib(7, 13))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests