comp-think/2018-2019

Lecture "Recursion", exercise 2

Opened this issue · 20 comments

Define a recursive function def fib(n) that implements the algorithm to find the nth Fibonacci number – where if n is less than or equal to 0, then 0 is returned as result; 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.

def test_fab(n,expected):
    if expected == fab(n):
        return True
    else:
        return False

def fab(n):
    if(n<=0):
        result = 0
    elif(n==1):
        result = 1
    else: 
        result = fab(n-1) + fab(n-2)
    return result
print(test_fab(10,55))
print(test_fab(4,3)) 
#true 
#true
# Test case for the exponentiation algorithm

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

# Code of the exponentiation algorithm

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

# Three test runs of the algorithm

print(test_fib(7, 13))
print(test_fib(9, 34))

#true
#true```
#I wrote this algorithm for exercise #2 
def F(n):
if (n == 0) :
return 0
if (n == 1 or n== 2) :
return 1
else:
return F(n-1)+F(n-2)

#The one I did today:

def test_F(n,expected):
    
    result = F(n)
    if result == expected:
        return True
    else:
        return False

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

print(test_F(14,377))     #True
print(test_F(12,144))     #True
def test_fib(n, expected):
    if expected == fib(n):
        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))
print(test_fib(0,0))
print(test_fib(4,3))

True
True
True

# Test case for the algorithm
def test_fib(n, expected):
    result = fib(n)
    if result == expected:
        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)
        
# Test some cases
print(test_fib(7, 13))
print(test_fib(12, 144))
print(test_fib(1, 1))

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

True
True
True

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

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

print fib(5) {5}

print fib_check(11,89) {True}
print fib_check(7,13) {True}

I have to check it on Pycharm, increasing n is a quite problematic about the computational work. I've written 44 as input of "cosalunga" on pycharm and I'm still waiting the result.

image

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

I've found this recursive algorithm very resource-consuming because it has to calculate each previous number for every number, so I've used low inputs to mitigate the effect. I guess there's a faster way to do that...maybe creating a list to store numbers and reduce the counting?

def test_fib(n, expected): #my test case
    result = fib(n)
    if expected == result:
        return True
    else:
        return False

def fib(n): #fib alg

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

print(test_fib(3, 2)) #true
print(test_fib(5, 5)) #true
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
    return fib(n-1)+ fib(n-2)

print(test_fib(8,21)) #true 
print(test_fib(5,5)) #true 
print(fib(8))
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)


n1 = 1
expected1 = 1
print(test_fib(n1, expected1))
n2 = 4
expected2 = 3
print(test_fib(n2, expected2))
n3 = 16
expected3 = 987
print(test_fib(n3, expected3))

def test_fab(n,expected):
if expected == fab(n):
return True
else:
return False

def fab(n):
if(n<=0):
result = 0
elif(n==1):
result = 1
else:
result = fab(n-1) + fab(n-2)
return result
print(test_fab(8,21))
print(test_fab(2,1))

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

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

# Test Cases 
print(test_fib(3, 2)) 
print(test_fib(5, 5)) 
print(test_fib(4, 3))

#test
def test_fib(n, expected):
    if fib(n) == expected:
        return True
    else:
        return False
    
#code    
def fib(n):
    if n <= 0:
        return 0
    if n == 1:
        return 1
    else:
        return fib(n - 1) + fib (n - 2)
    
#test cases    
print(test_fib(5,5)) #true
print(test_fib(8,21)) #true
print(test_fib(9,34)) #true
        
        
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(7, 13))
print(test_fib(5, 5))
print(test_fib(1, 1))

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

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

Hi guys,

here my take on the exercise (source code available online):

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


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

Some comments:

  1. Python code and indentation: please, in your answers to the various questions, if you have to write down a Python code, be sure that the correct indent is preserved by previewing your post before to publish it. You can use the ``` environment for defining your Python code, e.g.:

```
write your Python code here
```