From 4c7cc73ae2ff27cc1b00bff079a9c4cfe1eee926 Mon Sep 17 00:00:00 2001 From: Khushi Pal Date: Thu, 19 Jun 2025 15:43:24 +0530 Subject: [PATCH 1/3] Add recursive factorial method with tests --- recursion/__init__.py | 0 recursion/factorial.py | 28 ++++++++++++++++++++++++++++ recursion/tests/__init__.py | 0 recursion/tests/test_factorial.py | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 recursion/__init__.py create mode 100644 recursion/factorial.py create mode 100644 recursion/tests/__init__.py create mode 100644 recursion/tests/test_factorial.py diff --git a/recursion/__init__.py b/recursion/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/recursion/factorial.py b/recursion/factorial.py new file mode 100644 index 000000000000..3150ce17bce3 --- /dev/null +++ b/recursion/factorial.py @@ -0,0 +1,28 @@ +""" +Fibonacci +https://en.wikipedia.org/wiki/Fibonacci_number +""" +def factorial(number: int) -> int: + """ + Compute the factorial of a non-negative integer using recursion. + + >>> factorial(5) + 120 + >>> factorial(0) + 1 + >>> factorial(1) + 1 + >>> factorial(3) + 6 + >>> factorial(10) + 3628800 + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: Input must be a non-negative integer. + """ + if number < 0: + raise ValueError("Input must be a non-negative integer.") + if number == 0: + return 1 + return number * factorial(number - 1) diff --git a/recursion/tests/__init__.py b/recursion/tests/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/recursion/tests/test_factorial.py b/recursion/tests/test_factorial.py new file mode 100644 index 000000000000..13cf907491e2 --- /dev/null +++ b/recursion/tests/test_factorial.py @@ -0,0 +1,18 @@ +import unittest +from recursion.factorial import factorial + + +class TestFactorial(unittest.TestCase): + def test_factorial_valid_inputs(self): + self.assertEqual(factorial(0), 1) + self.assertEqual(factorial(1), 1) + self.assertEqual(factorial(5), 120) + self.assertEqual(factorial(10), 3628800) + + def test_factorial_invalid_input(self): + with self.assertRaises(ValueError): + factorial(-1) + + +if __name__ == "__main__": + unittest.main() From 137dda1a8c507f5a5b5ea1c6530977a44ff58ec4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Jun 2025 10:35:17 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- recursion/factorial.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/recursion/factorial.py b/recursion/factorial.py index 3150ce17bce3..0fe09e2005bf 100644 --- a/recursion/factorial.py +++ b/recursion/factorial.py @@ -2,6 +2,8 @@ Fibonacci https://en.wikipedia.org/wiki/Fibonacci_number """ + + def factorial(number: int) -> int: """ Compute the factorial of a non-negative integer using recursion. From 9696330432148d50b29d08e699c41ce1e462a7dc Mon Sep 17 00:00:00 2001 From: Khushi Pal <114483555+khushipy@users.noreply.github.com> Date: Thu, 19 Jun 2025 17:03:47 +0530 Subject: [PATCH 3/3] Update test_factorial.py --- recursion/tests/test_factorial.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/recursion/tests/test_factorial.py b/recursion/tests/test_factorial.py index 13cf907491e2..91bcc2bb2c61 100644 --- a/recursion/tests/test_factorial.py +++ b/recursion/tests/test_factorial.py @@ -1,18 +1,15 @@ -import unittest -from recursion.factorial import factorial +import pytest +from recursion.factorial import factorial -class TestFactorial(unittest.TestCase): - def test_factorial_valid_inputs(self): - self.assertEqual(factorial(0), 1) - self.assertEqual(factorial(1), 1) - self.assertEqual(factorial(5), 120) - self.assertEqual(factorial(10), 3628800) - def test_factorial_invalid_input(self): - with self.assertRaises(ValueError): - factorial(-1) +def test_factorial_valid_inputs() -> None: + assert factorial(0) == 1 + assert factorial(1) == 1 + assert factorial(5) == 120 + assert factorial(10) == 3628800 -if __name__ == "__main__": - unittest.main() +def test_factorial_invalid_input() -> None: + with pytest.raises(ValueError): + factorial(-1)