2019-10-31 12:45:32 +00:00
|
|
|
def factorial(n: int) -> int:
|
2019-07-02 04:05:43 +00:00
|
|
|
"""
|
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-07-02 04:05:43 +00:00
|
|
|
"""
|
2019-10-31 12:45:32 +00:00
|
|
|
if n < 0:
|
|
|
|
raise ValueError("factorial() not defined for negative values")
|
|
|
|
if not isinstance(n, int):
|
|
|
|
raise ValueError("factorial() only accepts integral values")
|
|
|
|
return 1 if n == 0 or n == 1 else n * factorial(n - 1)
|
|
|
|
|
2019-07-02 04:05:43 +00:00
|
|
|
|
2019-10-31 12:45:32 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import doctest
|
2019-04-22 14:53:56 +00:00
|
|
|
|
2019-10-31 12:45:32 +00:00
|
|
|
doctest.testmod()
|