From 79cc9a835416965720e923fac4a8c53c22108e73 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sun, 19 Aug 2018 16:50:35 -0500 Subject: Great LCM exercise. Done! 2 things are really interesting: 1. How to find the right test cases. I think I'll probably need to go back and re-read how to do an exhautisive testing 2. Python 3's floor division versus true division (// and /) operators are interesting. This exercise discussion helped me: https://courses.edx.org/courses/course-v1:UCSanDiegoX+ALGS200x+2T2017/discussion/forum/course/threads/5a3d32d644a15008df00062e and this one: https://stackoverflow.com/questions/19507808/python3-integer-division and this one: https://www.python.org/dev/peps/pep-0238/ --- AlgoDesignAndTechniqueEdxPython/sources/gcd.py | 19 -------------- AlgoDesignAndTechniqueEdxPython/sources/gcdlcm.py | 22 ++++++++++++++++ AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py | 24 ------------------ .../tests/gcdlcmTest.py | 29 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 43 deletions(-) delete mode 100644 AlgoDesignAndTechniqueEdxPython/sources/gcd.py create mode 100644 AlgoDesignAndTechniqueEdxPython/sources/gcdlcm.py delete mode 100644 AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py create mode 100644 AlgoDesignAndTechniqueEdxPython/tests/gcdlcmTest.py diff --git a/AlgoDesignAndTechniqueEdxPython/sources/gcd.py b/AlgoDesignAndTechniqueEdxPython/sources/gcd.py deleted file mode 100644 index 7b107cd..0000000 --- a/AlgoDesignAndTechniqueEdxPython/sources/gcd.py +++ /dev/null @@ -1,19 +0,0 @@ -# Uses python3 -import sys - - -def getGCD(a, b): - if b == 0: - return a - if b > a: - return getGCD(b, a) - else: - return getGCD(b, a % b) - - -if __name__ == '__main__': - entryNumbers = sys.stdin.read() - tokens = entryNumbers.split() - a = int(tokens[0]) - b = int(tokens[1]) - print(getGCD(a, b)) diff --git a/AlgoDesignAndTechniqueEdxPython/sources/gcdlcm.py b/AlgoDesignAndTechniqueEdxPython/sources/gcdlcm.py new file mode 100644 index 0000000..57b48ff --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/gcdlcm.py @@ -0,0 +1,22 @@ +# Uses python3 +import sys + + +def getGCD(a, b): + if b == 0: + return a + if b > a: + return getGCD(b, a) + else: + return getGCD(b, a % b) + +def getLCM(a, b): +# https://www.idomaths.com/hcflcm.php#formula + return a * b // getGCD(a, b) + +if __name__ == '__main__': + entryNumbers = sys.stdin.read() + tokens = entryNumbers.split() + a = int(tokens[0]) + b = int(tokens[1]) + print(getLCM(a, b)) \ No newline at end of file diff --git a/AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py b/AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py deleted file mode 100644 index a41b49e..0000000 --- a/AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py +++ /dev/null @@ -1,24 +0,0 @@ -''' -Created on Aug 19, 2018 - -@author: haidong -''' -import unittest - -from sources.gcd import getGCD - -class Test(unittest.TestCase): - - - def testName(self): - self.assertEqual(1, getGCD(18, 35)) - self.assertEqual(17657, getGCD(28851538, 1183019)) - self.assertEqual(7, getGCD(1344, 217)) - self.assertEqual(1344, getGCD(1344, 1344)) - self.assertEqual(4, getGCD(14159572, 63967072)) - self.assertEqual(4, getGCD(63967072,14159572)) - - -if __name__ == "__main__": - #import sys;sys.argv = ['', 'Test.testName'] - unittest.main() \ No newline at end of file diff --git a/AlgoDesignAndTechniqueEdxPython/tests/gcdlcmTest.py b/AlgoDesignAndTechniqueEdxPython/tests/gcdlcmTest.py new file mode 100644 index 0000000..82a7295 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/gcdlcmTest.py @@ -0,0 +1,29 @@ +''' +Created on Aug 19, 2018 + +@author: haidong +''' +import unittest + +from sources.gcdlcm import getGCD, getLCM + +class Test(unittest.TestCase): + + + def testName(self): + self.assertEqual(1, getGCD(18, 35)) + self.assertEqual(17657, getGCD(28851538, 1183019)) + self.assertEqual(7, getGCD(1344, 217)) + self.assertEqual(1344, getGCD(1344, 1344)) + self.assertEqual(4, getGCD(14159572, 63967072)) + self.assertEqual(4, getGCD(63967072,14159572)) + + def testLCM(self): + self.assertEqual(24, getLCM(6, 8)) + self.assertEqual(1933053046, getLCM(28851538, 1183019)) + self.assertEqual(226436590403296, getLCM(14159572, 63967072)) + self.assertEqual(46374212988031350, getLCM(226553150, 1023473145)) + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main() \ No newline at end of file -- cgit v1.2.3