From b7e876a7ec1de64c4823da2d711ef9538cd49c05 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 25 Jan 2023 19:27:25 +0000 Subject: [PATCH 1/2] feat: Concatenate both factorial implementations --- .../{factorial_iterative.py => factorial.py} | 24 ++++++++++++++++ maths/factorial_recursive.py | 28 ------------------- 2 files changed, 24 insertions(+), 28 deletions(-) rename maths/{factorial_iterative.py => factorial.py} (59%) delete mode 100644 maths/factorial_recursive.py diff --git a/maths/factorial_iterative.py b/maths/factorial.py similarity index 59% rename from maths/factorial_iterative.py rename to maths/factorial.py index c6cf7de57ab2..3aa5821813ae 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial.py @@ -34,6 +34,30 @@ def factorial(number: int) -> int: return value +def factorial(n: int) -> int: + """ + Calculate the factorial of a positive integer + https://en.wikipedia.org/wiki/Factorial + + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> factorial(0.1) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: factorial() 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 == 0 or n == 1 else n * factorial(n - 1) + + if __name__ == "__main__": import doctest diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py deleted file mode 100644 index 137112738905..000000000000 --- a/maths/factorial_recursive.py +++ /dev/null @@ -1,28 +0,0 @@ -def factorial(n: int) -> int: - """ - Calculate the factorial of a positive integer - https://en.wikipedia.org/wiki/Factorial - - >>> import math - >>> all(factorial(i) == math.factorial(i) for i in range(20)) - True - >>> factorial(0.1) - Traceback (most recent call last): - ... - ValueError: factorial() only accepts integral values - >>> factorial(-1) - Traceback (most recent call last): - ... - ValueError: factorial() 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 == 0 or n == 1 else n * factorial(n - 1) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 49436282e7371e48669340519052f70c521635b4 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Wed, 25 Jan 2023 19:35:22 +0000 Subject: [PATCH 2/2] fix: Rename factorial recursive method --- maths/factorial.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/factorial.py b/maths/factorial.py index 3aa5821813ae..bbf0efc011d8 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -34,7 +34,7 @@ def factorial(number: int) -> int: return value -def factorial(n: int) -> int: +def factorial_recursive(n: int) -> int: """ Calculate the factorial of a positive integer https://en.wikipedia.org/wiki/Factorial