From 20015e15132eb752d4fe94ebb2eda91d0e873ada Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Mon, 18 May 2020 11:44:24 +0200 Subject: [PATCH 1/3] Recursive euclidean algorithm + doctests and type hints --- ciphers/hill_cipher.py | 1 + other/euclidean_gcd.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ciphers/hill_cipher.py b/ciphers/hill_cipher.py index 9efada3e2b7d..f18fc9e9a9b4 100644 --- a/ciphers/hill_cipher.py +++ b/ciphers/hill_cipher.py @@ -231,6 +231,7 @@ def main(): if __name__ == "__main__": import doctest + doctest.testmod() main() diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py index c6c11f947a08..48137481513d 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -1,7 +1,15 @@ -# https://en.wikipedia.org/wiki/Euclidean_algorithm +""" https://en.wikipedia.org/wiki/Euclidean_algorithm """ -def euclidean_gcd(a, b): +def euclidean_gcd(a: int, b: int) -> int: + """ + Examples: + >>> euclidean_gcd(3, 5) + 1 + + >>> euclidean_gcd(6, 3) + 3 + """ while b: t = b b = a % b @@ -9,6 +17,23 @@ def euclidean_gcd(a, b): return a +def euclidean_gcd_recursive(a: int, b: int) -> int: + """ + Recursive method for euclicedan gcd algorithm + + Examples: + >>> euclidean_gcd(3, 5) + 1 + + >>> euclidean_gcd(6, 3) + 3 + """ + if b == 0: + return a + else: + return euclidean_gcd_recursive(b, a % b) + + def main(): print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) From 5840532c75e06af05c4cb57723881562d63f6bdf Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Mon, 18 May 2020 11:47:30 +0200 Subject: [PATCH 2/3] Fix doctests in recursive method --- other/euclidean_gcd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py index 48137481513d..8765a09236fd 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -22,10 +22,10 @@ def euclidean_gcd_recursive(a: int, b: int) -> int: Recursive method for euclicedan gcd algorithm Examples: - >>> euclidean_gcd(3, 5) + >>> euclidean_gcd_recursive(3, 5) 1 - >>> euclidean_gcd(6, 3) + >>> euclidean_gcd_recursive(6, 3) 3 """ if b == 0: From 201da1435f0aa2f563941dabae4f52ff17b34649 Mon Sep 17 00:00:00 2001 From: mateuszz0000 Date: Mon, 18 May 2020 14:50:05 +0200 Subject: [PATCH 3/3] Added commit suggestions --- other/euclidean_gcd.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/other/euclidean_gcd.py b/other/euclidean_gcd.py index 8765a09236fd..de4b250243db 100644 --- a/other/euclidean_gcd.py +++ b/other/euclidean_gcd.py @@ -11,9 +11,7 @@ def euclidean_gcd(a: int, b: int) -> int: 3 """ while b: - t = b - b = a % b - a = t + a, b = b, a % b return a @@ -28,18 +26,21 @@ def euclidean_gcd_recursive(a: int, b: int) -> int: >>> euclidean_gcd_recursive(6, 3) 3 """ - if b == 0: - return a - else: - return euclidean_gcd_recursive(b, a % b) + return a if b == 0 else euclidean_gcd_recursive(b, a % b) def main(): - print("GCD(3, 5) = " + str(euclidean_gcd(3, 5))) - print("GCD(5, 3) = " + str(euclidean_gcd(5, 3))) - print("GCD(1, 3) = " + str(euclidean_gcd(1, 3))) - print("GCD(3, 6) = " + str(euclidean_gcd(3, 6))) - print("GCD(6, 3) = " + str(euclidean_gcd(6, 3))) + print(f"euclidean_gcd(3, 5) = {euclidean_gcd(3, 5)}") + print(f"euclidean_gcd(5, 3) = {euclidean_gcd(5, 3)}") + print(f"euclidean_gcd(1, 3) = {euclidean_gcd(1, 3)}") + print(f"euclidean_gcd(3, 6) = {euclidean_gcd(3, 6)}") + print(f"euclidean_gcd(6, 3) = {euclidean_gcd(6, 3)}") + + print(f"euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3, 5)}") + print(f"euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5, 3)}") + print(f"euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1, 3)}") + print(f"euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3, 6)}") + print(f"euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6, 3)}") if __name__ == "__main__":