Skip to content

Added doctests for fibonacci.py #10836

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

Merged
merged 11 commits into from
Oct 29, 2023
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@
* [Shear Stress](physics/shear_stress.py)
* [Speed Of Sound](physics/speed_of_sound.py)
* [Speeds Of Gas Molecules](physics/speeds_of_gas_molecules.py)
* [Terminal Velocity](physics/terminal_velocity.py)

## Project Euler
* Problem 001
Expand Down
16 changes: 16 additions & 0 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def fib_recursive(n: int) -> list[int]:
def fib_recursive_term(i: int) -> int:
"""
Calculates the i-th (0-indexed) Fibonacci number using recursion
>>> fib_recursive_term(0)
0
>>> fib_recursive_term(1)
1
>>> fib_recursive_term(5)
5
>>> fib_recursive_term(10)
55
>>> fib_recursive_term(-1)
Traceback (most recent call last):
...
Exception: n is negative
"""
if i < 0:
raise Exception("n is negative")
Expand Down Expand Up @@ -197,6 +209,10 @@ def fib_binet(n: int) -> list[int]:


if __name__ == "__main__":
import doctest

doctest.testmod()

num = 30
time_func(fib_iterative, num)
time_func(fib_recursive, num) # Around 3s runtime
Expand Down