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

Open
essepuntato opened this issue Nov 30, 2018 · 20 comments
Open

Lecture "Recursion", exercise 2 #24

essepuntato opened this issue Nov 30, 2018 · 20 comments
Labels
Exercise The exercises that are introduced in the lectures.

Comments

@essepuntato
Copy link
Contributor

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.

@essepuntato essepuntato added the Exercise The exercises that are introduced in the lectures. label Nov 30, 2018
@hizclick
Copy link

hizclick commented Nov 30, 2018

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

@delfimpandiani
Copy link

delfimpandiani commented Nov 30, 2018

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

@simayguzel
Copy link

#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

@federicabologna
Copy link

federicabologna commented Nov 30, 2018

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

@EleonoraPeruch
Copy link

EleonoraPeruch commented Dec 1, 2018

# 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

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

True
True
True

@bluebell94
Copy link

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}

@MattiaSpadoni
Copy link

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

@beccadelbens
Copy link

beccadelbens commented Dec 1, 2018

#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

@friendlynihilist
Copy link

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

@saraarmaroli
Copy link

saraarmaroli commented Dec 2, 2018

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

@mangiafrangette
Copy link

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

@lisasiurina
Copy link

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

@tceron
Copy link

tceron commented Dec 3, 2018

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

@SeverinJB
Copy link

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

@ilsamoano
Copy link


#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
        
        

@MilenaCorbellini
Copy link

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

@DavideApolloni
Copy link

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

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

@essepuntato
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Exercise The exercises that are introduced in the lectures.
Projects
None yet
Development

No branches or pull requests