From bfa97f946286ab574fe58075f50d5603a6728b30 Mon Sep 17 00:00:00 2001 From: ADDALA MATHEW Date: Fri, 31 Oct 2025 09:01:45 +0530 Subject: [PATCH 1/2] fix-factorial-validation: Fix input validation and doctests Fix factorial() input validation to use isinstance(number, int) instead of value comparison Changes: - Replace 'number != int(number)' with 'not isinstance(number, int)' in factorial() - Fix doctests in factorial_recursive() to reference factorial_recursive() instead of factorial() - Add negative test case for factorial(1.5) to verify non-integer rejection This ensures consistent input validation between factorial() and factorial_recursive() functions. --- maths/factorial.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/maths/factorial.py b/maths/factorial.py index ba61447c7564..131ffc49a1c2 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -1,12 +1,9 @@ """ Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial """ - - def factorial(number: int) -> int: """ Calculate the factorial of specified number (n!). - >>> import math >>> all(factorial(i) == math.factorial(i) for i in range(20)) True @@ -14,6 +11,10 @@ def factorial(number: int) -> int: Traceback (most recent call last): ... ValueError: factorial() only accepts integral values + >>> factorial(1.5) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values >>> factorial(-1) Traceback (most recent call last): ... @@ -25,7 +26,7 @@ def factorial(number: int) -> int: >>> factorial(0) 1 """ - if number != int(number): + if not isinstance(number, int): raise ValueError("factorial() only accepts integral values") if number < 0: raise ValueError("factorial() not defined for negative values") @@ -33,21 +34,22 @@ def factorial(number: int) -> int: for i in range(1, number + 1): value *= i return value - - def factorial_recursive(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)) + >>> all(factorial_recursive(i) == math.factorial(i) for i in range(20)) True - >>> factorial(0.1) + >>> factorial_recursive(0.1) Traceback (most recent call last): ... ValueError: factorial() only accepts integral values - >>> factorial(-1) + >>> factorial_recursive(1.5) + Traceback (most recent call last): + ... + ValueError: factorial() only accepts integral values + >>> factorial_recursive(-1) Traceback (most recent call last): ... ValueError: factorial() not defined for negative values @@ -57,12 +59,8 @@ def factorial_recursive(n: int) -> int: 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 __name__ == "__main__": import doctest - doctest.testmod() - n = int(input("Enter a positive integer: ").strip() or 0) print(f"factorial{n} is {factorial(n)}") From 1805195a8a9030f2d4b7ee41e49ebf1634026d84 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 31 Oct 2025 03:32:36 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/factorial.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/maths/factorial.py b/maths/factorial.py index 131ffc49a1c2..f2e3ae094e57 100644 --- a/maths/factorial.py +++ b/maths/factorial.py @@ -1,6 +1,8 @@ """ Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial """ + + def factorial(number: int) -> int: """ Calculate the factorial of specified number (n!). @@ -34,6 +36,8 @@ def factorial(number: int) -> int: for i in range(1, number + 1): value *= i return value + + def factorial_recursive(n: int) -> int: """ Calculate the factorial of a positive integer @@ -59,8 +63,11 @@ def factorial_recursive(n: int) -> int: 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 __name__ == "__main__": import doctest + doctest.testmod() n = int(input("Enter a positive integer: ").strip() or 0) print(f"factorial{n} is {factorial(n)}")