From 9121b129e0663b1b583b66dcc6ed36a05a640b3c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 21 Oct 2021 03:41:10 +0000 Subject: [PATCH 1/6] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e3b40530a6b7..7023cf30a233 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -75,6 +75,7 @@ * [Morse Code](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code.py) * [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py) * [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py) + * [Polybius](https://github.com/TheAlgorithms/Python/blob/master/ciphers/polybius.py) * [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py) * [Rabin Miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py) * [Rail Fence Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rail_fence_cipher.py) @@ -565,6 +566,7 @@ ## Other * [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py) + * [Check Strong Password](https://github.com/TheAlgorithms/Python/blob/master/other/check_strong_password.py) * [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py) * [Davisb Putnamb Logemannb Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davisb_putnamb_logemannb_loveland.py) * [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py) @@ -952,6 +954,7 @@ * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) * [Upper](https://github.com/TheAlgorithms/Python/blob/master/strings/upper.py) + * [Wildcard Pattern Matching](https://github.com/TheAlgorithms/Python/blob/master/strings/wildcard_pattern_matching.py) * [Word Occurrence](https://github.com/TheAlgorithms/Python/blob/master/strings/word_occurrence.py) * [Word Patterns](https://github.com/TheAlgorithms/Python/blob/master/strings/word_patterns.py) * [Z Function](https://github.com/TheAlgorithms/Python/blob/master/strings/z_function.py) From 6fcf1b80d66f968dcda82fc36c0c875b0a7cbf62 Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 21 Oct 2021 11:42:11 +0800 Subject: [PATCH 2/6] pass integer to `math.factorial` in `project_euler/problem_015` --- project_euler/problem_015/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_015/sol1.py b/project_euler/problem_015/sol1.py index da079d26120a..fb2020d6179f 100644 --- a/project_euler/problem_015/sol1.py +++ b/project_euler/problem_015/sol1.py @@ -26,7 +26,7 @@ def solution(n: int = 20) -> int: """ n = 2 * n # middle entry of odd rows starting at row 3 is the solution for n = 1, # 2, 3,... - k = n / 2 + k = n // 2 return int(factorial(n) / (factorial(k) * factorial(n - k))) From d60dc64cd60b288ebf60c0d952e01a62460af163 Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 21 Oct 2021 11:48:49 +0800 Subject: [PATCH 3/6] remove duplicated factorial function --- maths/factorial_iterative.py | 23 ++++++++++++++++++----- maths/factorial_python.py | 34 ---------------------------------- 2 files changed, 18 insertions(+), 39 deletions(-) delete mode 100644 maths/factorial_python.py diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 64314790c11c..db7a276d1085 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -1,8 +1,11 @@ -# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial +"""Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial +""" -def factorial(n: int) -> int: +def factorial(input_number: int) -> int: """ + Calculate the factorial of specified number (n!). + >>> import math >>> all(factorial(i) == math.factorial(i) for i in range(20)) True @@ -14,17 +17,27 @@ def factorial(n: int) -> int: Traceback (most recent call last): ... ValueError: factorial() not defined for negative values + >>> factorial(1) + 1 + >>> factorial(6) + 720 + >>> factorial(0) + 1 """ - if n != int(n): + if input_number != int(input_number): raise ValueError("factorial() only accepts integral values") - if n < 0: + if input_number < 0: raise ValueError("factorial() not defined for negative values") value = 1 - for i in range(1, n + 1): + for i in range(1, input_number + 1): value *= i return value if __name__ == "__main__": + import doctest + + doctest.testmod() + n = int(input("Enter a positive integer: ").strip() or 0) print(f"factorial{n} is {factorial(n)}") diff --git a/maths/factorial_python.py b/maths/factorial_python.py deleted file mode 100644 index 46688261af56..000000000000 --- a/maths/factorial_python.py +++ /dev/null @@ -1,34 +0,0 @@ -def factorial(input_number: int) -> int: - """ - 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("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) - return result - - -if __name__ == "__main__": - import doctest - - doctest.testmod() From 78b05344897359d472ec81e6352565e8f2c59cbd Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 21 Oct 2021 03:49:13 +0000 Subject: [PATCH 4/6] updating DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7023cf30a233..10149eac5aac 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -454,7 +454,6 @@ * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) * [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py) * [Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_iterative.py) - * [Factorial Python](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py) * [Factorial Recursive](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_recursive.py) * [Factors](https://github.com/TheAlgorithms/Python/blob/master/maths/factors.py) * [Fermat Little Theorem](https://github.com/TheAlgorithms/Python/blob/master/maths/fermat_little_theorem.py) From 258816d250d276475e09fcd20b1200c100290abe Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 21 Oct 2021 14:58:51 +0800 Subject: [PATCH 5/6] Update maths/factorial_iterative.py Co-authored-by: Christian Clauss --- maths/factorial_iterative.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index db7a276d1085..5f096e9dd082 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -2,7 +2,7 @@ """ -def factorial(input_number: int) -> int: +def factorial(number: int) -> int: """ Calculate the factorial of specified number (n!). From b85d78e1c9b831a3ac00aeb54c17f7633f50b46b Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 21 Oct 2021 15:00:11 +0800 Subject: [PATCH 6/6] Update factorial_iterative.py --- maths/factorial_iterative.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 5f096e9dd082..c6cf7de57ab2 100644 --- a/maths/factorial_iterative.py +++ b/maths/factorial_iterative.py @@ -24,12 +24,12 @@ def factorial(number: int) -> int: >>> factorial(0) 1 """ - if input_number != int(input_number): + if number != int(number): raise ValueError("factorial() only accepts integral values") - if input_number < 0: + if number < 0: raise ValueError("factorial() not defined for negative values") value = 1 - for i in range(1, input_number + 1): + for i in range(1, number + 1): value *= i return value