From e9e4e5f5729c81d6a1b03fe91d00d00ec0df261b Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 31 Oct 2019 18:57:11 +0800 Subject: [PATCH 1/2] update factorial --- maths/factorial_python.py | 15 +++++++++------ maths/factorial_recursive.py | 21 +++++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/maths/factorial_python.py b/maths/factorial_python.py index ab97cd41e681..91accfdb5d44 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -12,10 +12,13 @@ def factorial(input_number: int) -> int: if input_number < 0: raise ValueError("Input input_number should be non-negative") - elif input_number == 0: - return 1 - else: - result = 1 - for i in range(input_number): - result = result * (i + 1) + result = 1 + for i in range(1, input_number): + result = result * (i + 1) return result + + +if __name__ == '__main__': + import doctest + + doctest.testmod() \ No newline at end of file diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index f346c65f1962..4026f093af36 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -1,14 +1,23 @@ -def fact(n): +def factorial(n): """ - Return 1, if n is 1 or below, + Return 1, if n is 1 or 0, otherwise, return n * fact(n-1). + + >>> factorial(1) + 1 + >>> factorial(6) + 720 + >>> factorial(0) + 1 """ - return 1 if n <= 1 else n * fact(n - 1) + if n < 0: + raise ValueError(n, "is negative") + return 1 if n == 0 or n == 1 else n * factorial(n - 1) """ Show factorial for i, -where i ranges from 1 to 20. +where i ranges from 0 to 20. """ -for i in range(1, 21): - print(i, ": ", fact(i), sep="") +for i in range(21): + print(i, ": ", factorial(i), sep="") From d5470a10a328f3a3f6f9dc46123bfd4dd5e07271 Mon Sep 17 00:00:00 2001 From: shellhub Date: Thu, 31 Oct 2019 20:24:19 +0800 Subject: [PATCH 2/2] update factorial --- maths/factorial_python.py | 30 ++++++++++++++++++++---------- maths/factorial_recursive.py | 27 +++++++++++++++++---------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/maths/factorial_python.py b/maths/factorial_python.py index 91accfdb5d44..b9adfdbaeaff 100644 --- a/maths/factorial_python.py +++ b/maths/factorial_python.py @@ -1,17 +1,27 @@ def factorial(input_number: int) -> int: """ - Non-recursive algorithm of finding factorial of the - input number. - >>> factorial(1) - 1 - >>> factorial(6) - 720 - >>> factorial(0) - 1 + 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 """ if input_number < 0: - raise ValueError("Input input_number should be non-negative") + 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) @@ -21,4 +31,4 @@ def factorial(input_number: int) -> int: if __name__ == '__main__': import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() diff --git a/maths/factorial_recursive.py b/maths/factorial_recursive.py index 4026f093af36..013560b28b42 100644 --- a/maths/factorial_recursive.py +++ b/maths/factorial_recursive.py @@ -1,7 +1,6 @@ -def factorial(n): +def factorial(n: int) -> int: """ - Return 1, if n is 1 or 0, - otherwise, return n * fact(n-1). + Calculate the factorial of specified number >>> factorial(1) 1 @@ -9,15 +8,23 @@ def factorial(n): 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 """ if n < 0: - raise ValueError(n, "is negative") + 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) -""" -Show factorial for i, -where i ranges from 0 to 20. -""" -for i in range(21): - print(i, ": ", factorial(i), sep="") +if __name__ == '__main__': + import doctest + + doctest.testmod()