From d2b8ea31e585ca0ca23a9f4f9091ce4521f1b7d5 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Wed, 3 Oct 2018 21:08:17 -0500 Subject: Maximize salary done! --- .../sources/largest_number.py | 27 ++++++++++ .../tests/largest_numberTest.py | 58 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 AlgoDesignAndTechniqueEdxPython/sources/largest_number.py create mode 100644 AlgoDesignAndTechniqueEdxPython/tests/largest_numberTest.py diff --git a/AlgoDesignAndTechniqueEdxPython/sources/largest_number.py b/AlgoDesignAndTechniqueEdxPython/sources/largest_number.py new file mode 100644 index 0000000..f3f803c --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/sources/largest_number.py @@ -0,0 +1,27 @@ +#Uses python3 + +import sys + +def compareLeadingDigits(num1, num2): + return (int) (num1 + num2) > (int) (num2+num1) + + +def getLargestNumber(nums): + result = "" + maxDigit = "0" + indexHolder = 0 + while len(nums) > 0: + for i in range(0, len(nums)): + if compareLeadingDigits(str(nums[i]), maxDigit): + maxDigit= str(nums[i]) + indexHolder = i; + result = result + maxDigit + maxDigit = "0" + del nums[indexHolder] + return result + +if __name__ == '__main__': + input = sys.stdin.read() + data = input.split() + a = data[1:] + print(getLargestNumber(a)) \ No newline at end of file diff --git a/AlgoDesignAndTechniqueEdxPython/tests/largest_numberTest.py b/AlgoDesignAndTechniqueEdxPython/tests/largest_numberTest.py new file mode 100644 index 0000000..de79eb8 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxPython/tests/largest_numberTest.py @@ -0,0 +1,58 @@ +''' +Created on Oct 3, 2018 + +@author: haidong +''' +import unittest + +from sources.largest_number import compareLeadingDigits, getLargestNumber + +class Test(unittest.TestCase): + + + def testName(self): + self.assertTrue(compareLeadingDigits("2", "21")) + + def testCompareLeadingDigits1(self): + self.assertFalse(compareLeadingDigits("20", "21")) + + def testCompareLeadingDigits2(self): + self.assertFalse(compareLeadingDigits("321", "322")) + + def testCompareLeadingDigits3(self): + self.assertFalse(compareLeadingDigits("321", "32")) + + def testCompareLeadingDigits4(self): + self.assertFalse(compareLeadingDigits("10", "1")) + + def testCompareLeadingDigits5(self): + self.assertTrue(compareLeadingDigits("6", "10")) + + def testCompareLeadingDigits6(self): + self.assertTrue(compareLeadingDigits("23", "2")) + + def testCompareLeadingDigits7(self): + self.assertTrue(compareLeadingDigits("545", "54")) + + def testCompareLeadingDigits8(self): + self.assertTrue(compareLeadingDigits("56", "565")) + + def testGetLargestNumber1(self): + nums = [9, 4, 6, 1, 9] + self.assertEqual("99641", getLargestNumber(nums)) + + def testGetLargestNumber2(self): + nums = [21, 2] + self.assertEqual("221", getLargestNumber(nums)) + + def testGetLargestNumber3(self): + nums = [23, 39, 92] + self.assertEqual("923923", getLargestNumber(nums)) + + def testGetLargestNumber4(self): + nums = [23, 2] + self.assertEqual("232", getLargestNumber(nums)) + +if __name__ == "__main__": + #import sys;sys.argv = ['', 'Test.testName'] + unittest.main() \ No newline at end of file -- cgit v1.2.3