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

issue #10837 Move fib_recursive_term() outside of fib_recursive() for proper doctest coverage and improved code organization. #11301

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions maths/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ def fib_iterative(n: int) -> list[int]:
return fib


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
"""
Comment on lines +86 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we:

  1. Removed the large block of empty lines in the middle of the fib_recursive function.
  2. Ensured the docstring for the fib_recursive function is correctly formatted and includes the expected outputs for the doctests.

if i < 0:
raise ValueError("n is negative")
if i < 2:
return i
return fib_recursive_term(i - 1) + fib_recursive_term(i - 2)


def fib_recursive(n: int) -> list[int]:
"""
Calculates the first n (0-indexed) Fibonacci numbers using recursion
Expand All @@ -100,28 +123,6 @@ def fib_recursive(n: int) -> list[int]:
ValueError: n is negative
"""

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 ValueError("n is negative")
if i < 2:
return i
return fib_recursive_term(i - 1) + fib_recursive_term(i - 2)

if n < 0:
raise ValueError("n is negative")
return [fib_recursive_term(i) for i in range(n + 1)]
Expand Down
Loading