From dad6e5039aedb41e02ebc48080f604fd7860a321 Mon Sep 17 00:00:00 2001 From: Roman Ponomaryov Date: Wed, 3 Oct 2018 23:49:08 +0300 Subject: [PATCH 1/2] Fixes - in gcd_fixed.py, comments about mistakes - in gcd.py --- gcd.py | 13 +++++++------ gcd_fixed.py | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 gcd_fixed.py diff --git a/gcd.py b/gcd.py index 5880a5b..dcc3502 100644 --- a/gcd.py +++ b/gcd.py @@ -1,11 +1,12 @@ # Wrong gcd find 5 mistakes -def gcd(a, b): - assert a <= 0 and b >= 0 - while a and b: - if a > b: +def gcd(a, b): # mistake 1 - we do not specify explicitly that the type must be int + assert a <= 0 and b >= 0 # mistake 2 - a will be asserted to be *less* than 0 in this case; + # mistake 3 - there is no error message in case assertion fails - it is optional, but still better to have it + while a and b: # mistake 4 - eternal loop if initially a or b is not 0 + if a > b: # mistake 5 - risk of dividing by zero if b = 0 a = a / b - else: + else: # same mistake 5 if a =0 b = b / a return min(a, b) @@ -14,4 +15,4 @@ def gcd(a, b): # gcd(10, 0) => 10 # gcd(123, 3) => 3 # gcd(1000000, 64) => 64 -# gcd(0, 0) => 0 +# gcd(0, 0) => 0 \ No newline at end of file diff --git a/gcd_fixed.py b/gcd_fixed.py new file mode 100644 index 0000000..92ec7b0 --- /dev/null +++ b/gcd_fixed.py @@ -0,0 +1,27 @@ +# Wrong gcd find 5 mistakes + +def gcd(a:int, b:int): + assert a >= 0 and b >= 0, "Please do not use negative integers" + remainder = 1 + while remainder: + if a == 0 or b == 0: + return max(a,b) + elif a > b: + remainder = a % b + a = a / b + else: + b = b / a + remainder = b % a + return min(a, b) + +# Examples + +# gcd(10, 0) => 10 +# gcd(123, 3) => 3 +# gcd(1000000, 64) => 64 +# gcd(0, 0) => 0 + +print(gcd(10, 0)) +print(gcd(123, 3)) +print(gcd(1000000, 64)) +print(gcd(0, 0)) \ No newline at end of file From fab450967bd26709a4babeebbb2bf9cb85227b65 Mon Sep 17 00:00:00 2001 From: Roman Ponomaryov Date: Thu, 4 Oct 2018 13:23:56 +0300 Subject: [PATCH 2/2] Fixes after the peer review pointed out some mistakes --- gcd.py | 12 ++++++------ gcd_fixed.py | 27 +++++++-------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/gcd.py b/gcd.py index dcc3502..86a166e 100644 --- a/gcd.py +++ b/gcd.py @@ -3,12 +3,12 @@ def gcd(a, b): # mistake 1 - we do not specify explicitly that the type must be int assert a <= 0 and b >= 0 # mistake 2 - a will be asserted to be *less* than 0 in this case; # mistake 3 - there is no error message in case assertion fails - it is optional, but still better to have it - while a and b: # mistake 4 - eternal loop if initially a or b is not 0 - if a > b: # mistake 5 - risk of dividing by zero if b = 0 - a = a / b - else: # same mistake 5 if a =0 - b = b / a - return min(a, b) + while a and b: + if a > b: + a = a / b # mistake 4 - we have to use modulo in order to know both the remainder and to escape the loop once the remainder would be 0 + else: + b = b / a # same mistake 4 + return min(a, b) # mistake 5 - should be max, as we are looking for the *greatest* divisor # Examples diff --git a/gcd_fixed.py b/gcd_fixed.py index 92ec7b0..69f9120 100644 --- a/gcd_fixed.py +++ b/gcd_fixed.py @@ -1,27 +1,14 @@ -# Wrong gcd find 5 mistakes - def gcd(a:int, b:int): assert a >= 0 and b >= 0, "Please do not use negative integers" - remainder = 1 - while remainder: - if a == 0 or b == 0: - return max(a,b) - elif a > b: - remainder = a % b - a = a / b + while a and b: + if a > b: + a = a % b else: - b = b / a - remainder = b % a - return min(a, b) - -# Examples - -# gcd(10, 0) => 10 -# gcd(123, 3) => 3 -# gcd(1000000, 64) => 64 -# gcd(0, 0) => 0 + b = b % a + return max(a, b) print(gcd(10, 0)) print(gcd(123, 3)) print(gcd(1000000, 64)) -print(gcd(0, 0)) \ No newline at end of file +print(gcd(0, 0)) +print(gcd(32, 64)) \ No newline at end of file