diff options
author | Haidong Ji | 2018-08-19 11:08:59 -0500 |
---|---|---|
committer | Haidong Ji | 2018-08-19 11:08:59 -0500 |
commit | b7a6d9d18945e81ba9c23cf90b41328e20ba1341 (patch) | |
tree | 5a2c5487085a4b66aa5ebecc2393abd5112b0c43 | |
parent | 18030db87580ab02962686509f65c7e1cfcd2d4c (diff) |
GCD done!
-rw-r--r-- | AlgoDesignAndTechniqueEdxPython/sources/gcd.py | 19 | ||||
-rw-r--r-- | AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py | 24 |
2 files changed, 43 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxPython/sources/gcd.py b/AlgoDesignAndTechniqueEdxPython/sources/gcd.py new file mode 100644 index 0000000..7b107cd --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/gcd.py @@ -0,0 +1,19 @@ +# 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/tests/gcdTest.py b/AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py new file mode 100644 index 0000000..a41b49e --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/gcdTest.py @@ -0,0 +1,24 @@ +''' +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 |