From 042ac0b781a9da7699bdd432b7c36c2427eb7385 Mon Sep 17 00:00:00 2001 From: sayan som Date: Mon, 19 Jan 2026 23:40:15 +0530 Subject: [PATCH 1/4] Add recursive factorial implementation --- maths/factorial.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index ba61447c7564..e482f60c3225 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -34,35 +34,35 @@ def factorial(number: int) -> int: value *= i return value - -def factorial_recursive(n: int) -> int: +def factorial_recursive(number: int) -> int: """ - Calculate the factorial of a positive integer - https://en.wikipedia.org/wiki/Factorial + Calculate the factorial of a number using recursion. >>> import math - >>> all(factorial(i) == math.factorial(i) for i in range(20)) + >>> all(factorial_recursive(i) == math.factorial(i) for i in range(10)) True - >>> factorial(0.1) - Traceback (most recent call last): - ... - ValueError: factorial() only accepts integral values - >>> factorial(-1) + >>> factorial_recursive(0) + 1 + >>> factorial_recursive(5) + 120 + >>> factorial_recursive(-1) Traceback (most recent call last): ... - ValueError: factorial() not defined for negative values + ValueError: factorial_recursive() not defined for negative values """ - if not isinstance(n, int): - raise ValueError("factorial() only accepts integral values") - if n < 0: - raise ValueError("factorial() not defined for negative values") - return 1 if n in {0, 1} else n * factorial_recursive(n - 1) + if number != int(number): + raise ValueError( + "factorial_recursive() only accepts integral values" + ) + if number < 0: + raise ValueError( + "factorial_recursive() not defined for negative values" + ) + if number in (0, 1): + return 1 + return number * factorial_recursive(number - 1) if __name__ == "__main__": import doctest - doctest.testmod() - - n = int(input("Enter a positive integer: ").strip() or 0) - print(f"factorial{n} is {factorial(n)}") From b0120ab9400cab37c7aec93d6282393375dd2d92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 19 Jan 2026 18:11:16 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/factorial.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index e482f60c3225..d29b4c581f43 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -34,6 +34,7 @@ def factorial(number: int) -> int: value *= i return value + def factorial_recursive(number: int) -> int: """ Calculate the factorial of a number using recursion. @@ -51,13 +52,9 @@ def factorial_recursive(number: int) -> int: ValueError: factorial_recursive() not defined for negative values """ if number != int(number): - raise ValueError( - "factorial_recursive() only accepts integral values" - ) + raise ValueError("factorial_recursive() only accepts integral values") if number < 0: - raise ValueError( - "factorial_recursive() not defined for negative values" - ) + raise ValueError("factorial_recursive() not defined for negative values") if number in (0, 1): return 1 return number * factorial_recursive(number - 1) @@ -65,4 +62,5 @@ def factorial_recursive(number: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() From d1f40c981febf7d6bab05555bcaf9cc475bc4fb0 Mon Sep 17 00:00:00 2001 From: cclauss Date: Thu, 17 Sep 2026 09:08:56 +0000 Subject: [PATCH 3/4] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 24a948802f68..c50995721f50 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1028,6 +1028,8 @@ * [Activity Selection](other/activity_selection.py) * [Alternative List Arrange](other/alternative_list_arrange.py) * [Bankers Algorithm](other/bankers_algorithm.py) + * [Cheap Progress](other/cheap_progress.py) + * [Dancing Links](other/dancing_links.py) * [Davis Putnam Logemann Loveland](other/davis_putnam_logemann_loveland.py) * [Doomsday](other/doomsday.py) * [Fischer Yates Shuffle](other/fischer_yates_shuffle.py) @@ -1051,6 +1053,7 @@ * [Scoring Algorithm](other/scoring_algorithm.py) * [Sdes](other/sdes.py) * [Sliding Window Maximum](other/sliding_window_maximum.py) + * [Token Bucket](other/token_bucket.py) * [Tower Of Hanoi](other/tower_of_hanoi.py) * [Word Search](other/word_search.py) From 65925dbcb05b9b12d15efba87937a3dc9269528c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 17 Sep 2026 11:13:56 +0200 Subject: [PATCH 4/4] Fix docstring and update input for factorial functions Corrected docstring and improved input handling. --- maths/factorial.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index 57e8c1119345..a04fd5887802 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -5,7 +5,7 @@ def factorial(number: int) -> int: """ - Calculate the factorial of specified number (n!). + Calculate the factorial of the specified number. >>> import math >>> all(factorial(i) == math.factorial(i) for i in range(20)) @@ -61,10 +61,12 @@ def factorial_recursive(number: int) -> int: raise ValueError("factorial_recursive() only accepts integral values") if number < 0: raise ValueError("factorial_recursive() not defined for negative values") - return 1 if number in {0, 1} else number * factorial_recursive(n - 1) + return 1 if number in {0, 1} else number * factorial_recursive(number - 1) if __name__ == "__main__": import doctest doctest.testmod() + n = int(input("Enter a positive integer: ").strip() or 0) + print(f"{n = } {factorial(n) = } {factorial_recursive(n) = }")