From 1469a9500d70c171c98549f38aa1cd5bed1a866a Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 07:41:16 +0800 Subject: [PATCH 1/6] add type hints to math/gcd --- maths/greatest_common_divisor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 0926ade5dec2..9b7a22574677 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -5,7 +5,7 @@ """ -def greatest_common_divisor(a, b): +def greatest_common_divisor(a: int, b: int) -> int: """ Calculate Greatest Common Divisor (GCD). >>> greatest_common_divisor(24, 40) @@ -31,7 +31,7 @@ def greatest_common_divisor(a, b): """ -def gcd_by_iterative(x, y): +def gcd_by_iterative(x: int, y: int) -> int: """ >>> gcd_by_iterative(24, 40) 8 From bda4aa3be70de20d5e3bfa4429cb290b8d8f090c Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 07:43:41 +0800 Subject: [PATCH 2/6] add doctest --- maths/greatest_common_divisor.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 9b7a22574677..97c0da12e7ee 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -59,4 +59,6 @@ def main(): if __name__ == "__main__": + import doctest + doctest.testmod() main() From 4f7c9eb518acef004782e716ac7e10ae588a81d4 Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 07:51:06 +0800 Subject: [PATCH 3/6] math/gcd - run black formatter --- maths/greatest_common_divisor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 97c0da12e7ee..0c341deb39cf 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -60,5 +60,6 @@ def main(): if __name__ == "__main__": import doctest + doctest.testmod() main() From 83b57dd439efe351b749aef319cf96762de30a20 Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 08:12:43 +0800 Subject: [PATCH 4/6] math/gcd: remove manual doctest --- maths/greatest_common_divisor.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 0c341deb39cf..9b7a22574677 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -59,7 +59,4 @@ def main(): if __name__ == "__main__": - import doctest - - doctest.testmod() main() From de154b2faed4b056396683dd2203b3018a3d2937 Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 13:41:30 +0800 Subject: [PATCH 5/6] add correction to gcd of negative numbers --- maths/greatest_common_divisor.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 9b7a22574677..121869cb2be2 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -2,6 +2,8 @@ Greatest Common Divisor. Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor + +gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility """ @@ -20,31 +22,40 @@ def greatest_common_divisor(a: int, b: int) -> int: 1 >>> greatest_common_divisor(16, 4) 4 + >>> greatest_common_divisor(-3, 9) + 3 + >>> greatest_common_divisor(9, -3) + 3 + >>> greatest_common_divisor(3, -9) + 3 + >>> greatest_common_divisor(-3, -9) + 3 """ - return b if a == 0 else greatest_common_divisor(b % a, a) - - -""" -Below method is more memory efficient because it does not use the stack (chunk of -memory). While above method is good, uses more memory for huge numbers because of the -recursive calls required to calculate the greatest common divisor. -""" + return abs(b) if a == 0 else greatest_common_divisor(b % a, a) def gcd_by_iterative(x: int, y: int) -> int: """ + Below method is more memory efficient because it does not create additional + stack frames for recursive functions calls (as done in the above method). >>> gcd_by_iterative(24, 40) 8 >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40) True + >>> gcd_by_iterative(-3, -9) + 3 + >>> gcd_by_iterative(3, -9) + 3 """ while y: # --> when y=0 then loop will terminate and return x as final GCD. x, y = y, x % y - return x + return abs(x) def main(): - """Call Greatest Common Divisor function.""" + """ + Call Greatest Common Divisor function. + """ try: nums = input("Enter two integers separated by comma (,): ").split(",") num_1 = int(nums[0]) From cd0f1c4c03b61eacbcf2a126d41d1c28532735fc Mon Sep 17 00:00:00 2001 From: Joyce Date: Fri, 2 Oct 2020 14:19:10 +0800 Subject: [PATCH 6/6] add more doctest in iterative gcd --- maths/greatest_common_divisor.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/maths/greatest_common_divisor.py b/maths/greatest_common_divisor.py index 121869cb2be2..a2174a8eb74a 100644 --- a/maths/greatest_common_divisor.py +++ b/maths/greatest_common_divisor.py @@ -46,6 +46,10 @@ def gcd_by_iterative(x: int, y: int) -> int: 3 >>> gcd_by_iterative(3, -9) 3 + >>> gcd_by_iterative(1, -800) + 1 + >>> gcd_by_iterative(11, 37) + 1 """ while y: # --> when y=0 then loop will terminate and return x as final GCD. x, y = y, x % y