diff --git a/DIRECTORY.md b/DIRECTORY.md index e3b40530a..10149eac5 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) @@ -453,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) @@ -565,6 +565,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 +953,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) diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py index 64314790c..c6cf7de57 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(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 number != int(number): raise ValueError("factorial() only accepts integral values") - if n < 0: + if number < 0: raise ValueError("factorial() not defined for negative values") value = 1 - for i in range(1, n + 1): + for i in range(1, 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 46688261a..000000000 --- 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() diff --git a/project_euler/problem_015/sol1.py b/project_euler/problem_015/sol1.py index da079d261..fb2020d61 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)))