Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
MPKenley committed Jan 11, 2025
1 parent f1df2d9 commit 27c806f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
20 changes: 18 additions & 2 deletions solutions/challenge_36/factorial.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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.")
Expand All @@ -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)}")
Expand Down
17 changes: 7 additions & 10 deletions solutions/tests/challenge_36/test_factorial.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

0 comments on commit 27c806f

Please sign in to comment.