From 5347b662bc4ab8b63a3c68b75a47f75059f3102c Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 15:37:40 +0300 Subject: [PATCH 1/5] renamed module to extend the acronym --- maths/{find_lcm.py => least_common_multiple.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename maths/{find_lcm.py => least_common_multiple.py} (91%) diff --git a/maths/find_lcm.py b/maths/least_common_multiple.py similarity index 91% rename from maths/find_lcm.py rename to maths/least_common_multiple.py index dffadd1f3c5b..26bd131868f6 100644 --- a/maths/find_lcm.py +++ b/maths/least_common_multiple.py @@ -17,7 +17,7 @@ def find_lcm(num_1, num_2): lcm = max_num while True: - if (lcm % num_1 == 0) and (lcm % num_2 == 0): + if all(lcm % num_1 == 0) and (lcm % num_2 == 0): break lcm += max_num return lcm From 3160b314a78a7126bc0ce332ed52119aeeb2562d Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 15:42:12 +0300 Subject: [PATCH 2/5] add type hints (will not work with Python less than 3.4) --- maths/least_common_multiple.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index 26bd131868f6..1c434d7b1b1c 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -3,7 +3,7 @@ # https://en.wikipedia.org/wiki/Least_common_multiple -def find_lcm(num_1, num_2): +def find_lcm(num_1: int, num_2: int) -> int: """Find the least common multiple of two numbers. >>> find_lcm(5,2) 10 @@ -17,7 +17,7 @@ def find_lcm(num_1, num_2): lcm = max_num while True: - if all(lcm % num_1 == 0) and (lcm % num_2 == 0): + if (lcm % num_1 == 0) and (lcm % num_2 == 0): break lcm += max_num return lcm From ebf84da9739d8e30987e68191b745c078b329869 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 15:43:43 +0300 Subject: [PATCH 3/5] update docstring --- maths/least_common_multiple.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index 1c434d7b1b1c..15e519e75396 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -1,10 +1,8 @@ -"""Find Least Common Multiple.""" - -# https://en.wikipedia.org/wiki/Least_common_multiple - - def find_lcm(num_1: int, num_2: int) -> int: """Find the least common multiple of two numbers. + + Learn more: https://en.wikipedia.org/wiki/Least_common_multiple + >>> find_lcm(5,2) 10 >>> find_lcm(12,76) From 6e00394f9185d2ecfc98fb88ba04d8c4c8360e29 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 15:49:27 +0300 Subject: [PATCH 4/5] refactor the function --- maths/least_common_multiple.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index 15e519e75396..8899eba0efa1 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -1,4 +1,4 @@ -def find_lcm(num_1: int, num_2: int) -> int: +def find_lcm(first_num: int, second_num: int) -> int: """Find the least common multiple of two numbers. Learn more: https://en.wikipedia.org/wiki/Least_common_multiple @@ -8,24 +8,18 @@ def find_lcm(num_1: int, num_2: int) -> int: >>> find_lcm(12,76) 228 """ - if num_1 >= num_2: - max_num = num_1 - else: - max_num = num_2 - - lcm = max_num - while True: - if (lcm % num_1 == 0) and (lcm % num_2 == 0): - break - lcm += max_num - return lcm + max_num = first_num if first_num >= second_num else second_num + common_mult = max_num + while (common_mult % first_num != 0) and (common_mult % second_num != 0): + common_mult += max_num + return common_mult def main(): """Use test numbers to run the find_lcm algorithm.""" - num_1 = int(input().strip()) - num_2 = int(input().strip()) - print(find_lcm(num_1, num_2)) + first_num = int(input().strip()) + second_num = int(input().strip()) + print(find_lcm(first_num, second_num)) if __name__ == "__main__": From cb7aa74654ef3e51dce2eec28b00359efb9ddaf1 Mon Sep 17 00:00:00 2001 From: yuriimchg Date: Sat, 12 Oct 2019 16:10:55 +0300 Subject: [PATCH 5/5] add unittests for the least common squares multiple --- maths/least_common_multiple.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index 8899eba0efa1..863744e182b6 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -1,3 +1,6 @@ +import unittest + + def find_lcm(first_num: int, second_num: int) -> int: """Find the least common multiple of two numbers. @@ -10,17 +13,32 @@ def find_lcm(first_num: int, second_num: int) -> int: """ max_num = first_num if first_num >= second_num else second_num common_mult = max_num - while (common_mult % first_num != 0) and (common_mult % second_num != 0): + while (common_mult % first_num > 0) or (common_mult % second_num > 0): common_mult += max_num return common_mult -def main(): - """Use test numbers to run the find_lcm algorithm.""" - first_num = int(input().strip()) - second_num = int(input().strip()) - print(find_lcm(first_num, second_num)) +class TestLeastCommonMultiple(unittest.TestCase): + + test_inputs = [ + (10, 20), + (13, 15), + (4, 31), + (10, 42), + (43, 34), + (5, 12), + (12, 25), + (10, 25), + (6, 9), + ] + expected_results = [20, 195, 124, 210, 1462, 60, 300, 50, 18] + + def test_lcm_function(self): + for i, (first_num, second_num) in enumerate(self.test_inputs): + actual_result = find_lcm(first_num, second_num) + with self.subTest(i=i): + self.assertEqual(actual_result, self.expected_results[i]) if __name__ == "__main__": - main() + unittest.main()