diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 71ff479f9cc2..23eb636efc5c 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -90,37 +90,28 @@ def fib_iterative(n: int) -> list[int]: def fib_recursive(n: int) -> list[int]: """ - Calculates the first n (0-indexed) Fibonacci numbers using recursion - >>> fib_iterative(0) - [0] - >>> fib_iterative(1) - [0, 1] - >>> fib_iterative(5) - [0, 1, 1, 2, 3, 5] - >>> fib_iterative(10) - [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) - Traceback (most recent call last): - ... - ValueError: n is negative - """ + Calculate the first n (0-indexed) Fibonacci numbers using recursion. - 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) + Args: + n (int): The number of Fibonacci terms to generate. + + Returns: + list[int]: A list of the first n Fibonacci numbers. + + Examples: + >>> fib_recursive(0) + [0] + >>> fib_recursive(1) + [0, 1] + >>> fib_recursive(5) + [0, 1, 1, 2, 3, 5] + >>> fib_recursive(-1) Traceback (most recent call last): ... - Exception: n is negative - """ + ValueError: n is negative + """ + + def fib_recursive_term(i: int) -> int: if i < 0: raise ValueError("n is negative") if i < 2: @@ -134,7 +125,8 @@ def fib_recursive_term(i: int) -> int: def fib_recursive_cached(n: int) -> list[int]: """ - Calculates the first n (0-indexed) Fibonacci numbers using recursion + Calculates the first n (0-indexed) Fibonacci numbers using recursion. + >>> fib_iterative(0) [0] >>> fib_iterative(1) @@ -152,7 +144,7 @@ def fib_recursive_cached(n: int) -> list[int]: @functools.cache def fib_recursive_term(i: int) -> int: """ - Calculates the i-th (0-indexed) Fibonacci number using recursion + Calculates the i-th (0-indexed) Fibonacci number using recursion. """ if i < 0: raise ValueError("n is negative")