2019-10-18 05:27:55 +00:00
|
|
|
def factorial(input_number: int) -> int:
|
|
|
|
"""
|
2019-10-31 12:45:32 +00:00
|
|
|
Calculate the factorial of specified number
|
|
|
|
|
|
|
|
>>> factorial(1)
|
|
|
|
1
|
|
|
|
>>> factorial(6)
|
|
|
|
720
|
|
|
|
>>> factorial(0)
|
|
|
|
1
|
|
|
|
>>> factorial(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: factorial() not defined for negative values
|
|
|
|
>>> factorial(0.1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: factorial() only accepts integral values
|
2019-10-18 05:27:55 +00:00
|
|
|
"""
|
2018-10-31 07:28:41 +00:00
|
|
|
|
2019-10-18 05:27:55 +00:00
|
|
|
if input_number < 0:
|
2019-10-31 12:45:32 +00:00
|
|
|
raise ValueError("factorial() not defined for negative values")
|
|
|
|
if not isinstance(input_number, int):
|
|
|
|
raise ValueError("factorial() only accepts integral values")
|
|
|
|
result = 1
|
|
|
|
for i in range(1, input_number):
|
|
|
|
result = result * (i + 1)
|
2019-10-18 05:27:55 +00:00
|
|
|
return result
|
2019-10-31 12:45:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|