Giter VIP home page Giter VIP logo

Comments (20)

diegochillo avatar diegochillo commented on July 24, 2024
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))

from 2020-2021.

SusannaPinotti avatar SusannaPinotti commented on July 24, 2024
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 

from 2020-2021.

dbrembilla avatar dbrembilla commented on July 24, 2024
#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

from 2020-2021.

gabrielefiorenza avatar gabrielefiorenza commented on July 24, 2024
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

from 2020-2021.

AlessandraFa avatar AlessandraFa commented on July 24, 2024
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

from 2020-2021.

IlaRoss avatar IlaRoss commented on July 24, 2024
#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)

from 2020-2021.

ChiaraCati avatar ChiaraCati commented on July 24, 2024
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))

from 2020-2021.

AleRosae avatar AleRosae commented on July 24, 2024
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))

from 2020-2021.

yunglong28 avatar yunglong28 commented on July 24, 2024
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))

from 2020-2021.

edoardodalborgo avatar edoardodalborgo commented on July 24, 2024
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))

from 2020-2021.

fcagnola avatar fcagnola commented on July 24, 2024
# 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

from 2020-2021.

SofiBar avatar SofiBar commented on July 24, 2024
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))

from 2020-2021.

giorgiasampo avatar giorgiasampo commented on July 24, 2024
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))

from 2020-2021.

vanessabonanno avatar vanessabonanno commented on July 24, 2024
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"

from 2020-2021.

laurentfintoni avatar laurentfintoni commented on July 24, 2024
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))

from 2020-2021.

AlessandroBertozzi avatar AlessandroBertozzi commented on July 24, 2024
# 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)))

from 2020-2021.

essepuntato avatar essepuntato commented on July 24, 2024

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

from 2020-2021.

dbrembilla avatar dbrembilla commented on July 24, 2024

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

from 2020-2021.

diegochillo avatar diegochillo commented on July 24, 2024

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

from 2020-2021.

GiuliaMenna avatar GiuliaMenna commented on July 24, 2024
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))

from 2020-2021.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.