diff options
-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 |