From 27c806fefd8f25b902ee555fdfe8cdb4b718b0c2 Mon Sep 17 00:00:00 2001 From: unknown <mervilpierrekenley07@gmail.com> Date: Sat, 11 Jan 2025 04:55:32 -0500 Subject: [PATCH] code review --- solutions/challenge_36/factorial.py | 20 +++++++++++++++++-- .../tests/challenge_36/test_factorial.py | 17 +++++++--------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/solutions/challenge_36/factorial.py b/solutions/challenge_36/factorial.py index b906ec0d8..bca975d94 100644 --- a/solutions/challenge_36/factorial.py +++ b/solutions/challenge_36/factorial.py @@ -1,4 +1,4 @@ -"""this function allows to calculate the factorial of a non-negative integer n using recursive function""" +"""factorial python module.""" def factorial(n): @@ -9,7 +9,19 @@ def factorial(n): n (int): Non-negative integer whose factorial is to be calculated. Returns: - int: Factorial of the input number n. + int: Factorial of the input number n. The return value must be greater than or equal to 1. + + Raises: + ValueError: If n is a negative number. + Examples: + >>> factorial(0) # 0! = 1 + 1 + >>> factorial(4) # 4! = 24 + 24 + >>> factorial(-1) + Traceback (most recent call last): + ... + ValueError: Factorial is not defined for negative numbers. """ if n < 0: raise ValueError("Factorial is not defined for negative numbers.") @@ -19,6 +31,10 @@ def factorial(n): if __name__ == "__main__": + import doctest + + print("running doctest...") + doctest.testmod(verbose=True) try: num = int(input("Enter a non-negative integer: ")) print(f"Factorial of {num} is {factorial(num)}") diff --git a/solutions/tests/challenge_36/test_factorial.py b/solutions/tests/challenge_36/test_factorial.py index 510e05133..4d1d53019 100644 --- a/solutions/tests/challenge_36/test_factorial.py +++ b/solutions/tests/challenge_36/test_factorial.py @@ -1,4 +1,4 @@ -"""test the program factorial""" +"""This unittest tests the python factorial program for positive numbers, negative numbers and zero""" import unittest from solutions.challenge_36.factorial import factorial @@ -7,18 +7,15 @@ class TestFactorial(unittest.TestCase): """test the program for positive numbers, negative numbers and zero""" - def test_factorial_positive(self): - """test the program for positive numbers""" + def test_factorial_with_positive(self): + """test factorial of 5.""" self.assertEqual(factorial(5), 120) - self.assertEqual(factorial(4), 24) - self.assertEqual(factorial(3), 6) - self.assertEqual(factorial(1), 1) - def test_factorial_zero(self): - """test the program for zero""" + def test_factorial_with_zero(self): + """test factorial of 0""" self.assertEqual(factorial(0), 1) - def test_factorial_negative(self): - """test the program for negative numbers""" + def test_factorial_with_negative(self): + """test factorial of -1""" with self.assertRaises(ValueError): factorial(-1)