Write a function that calculates and returns the factorial of a non-negative integer.
def calculate_factorial(n):
return n * calculate_factorial(n - 1)
Input: `calculate_factorial(5)`
Output: `120`
Explanation: The factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
Input: `calculate_factorial(10)`
Output: `3628800`
Explanation: The factorial of 10 is 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800.
- Do not use the built-in
math.factorial()
function. - Do not use recursion.
- Do not use any loops.
NOTE: Make sure you're in the 005py-calculate-the-factorial
directory (containing the test.py
file) when executing the command above.
- To navigate to the directory containing the test file from the current terminal, run the following command:
cd scr/
- Then run the following command in a terminal to test your code:
python -m unittest -v test
- And make sure you get the following output:
test_factorial_of_large_number (test.TestCalculateFactorial.test_factorial_of_large_number) ... ok
test_factorial_of_positive_number (test.TestCalculateFactorial.test_factorial_of_positive_number) ... ok
test_factorial_of_zero (test.TestCalculateFactorial.test_factorial_of_zero) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK