Comments (20)
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.
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.
#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.
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.
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.
#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.
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.
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.
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.
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.
# 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.
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.
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.
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.
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.
# 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.
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.
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. returning0
in these cases)?
Hello,
In my case it returned 0
with negative numbers, as expected
from 2020-2021.
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. returning0
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.
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)
- Lecture "Brute-force algorithms", exercise 5 HOT 15
- Lecture "Organising information: unordered structures", exercise 1 HOT 21
- Lecture "Organising information: unordered structures", exercise 2 HOT 19
- Lecture "Organising information: unordered structures", exercise 3 HOT 20
- Lecture "Recursion", exercise 1 HOT 19
- Lecture "Divide and conquer algorithms", exercise 1 HOT 19
- Lecture "Divide and conquer algorithms", exercise 2 HOT 17
- Lecture "Divide and conquer algorithms", exercise 3 HOT 5
- Lecture "Dynamic programming algorithms", exercise 1 HOT 21
- Lecture "Dynamic programming algorithms", exercise 2 HOT 19
- Lecture "Organising information: trees", exercise 1 HOT 9
- Lecture "Organising information: trees", exercise 2 HOT 7
- Bonus exercise (AtariGo) HOT 2
- Lecture "Backtracking algorithms", exercise 1 HOT 6
- Lecture "Backtracking algorithms", exercise 2 HOT 5
- Lecture "Organising information: graphs", exercise 1 HOT 15
- Lecture "Organising information: graphs", exercise 2 HOT 14
- Lecture "Greedy algorithms", exercise 1 HOT 5
- Lecture "Greedy algorithms", exercise 2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from 2020-2021.