summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2018-10-03 21:08:17 -0500
committerHaidong Ji2018-10-03 21:08:17 -0500
commitd2b8ea31e585ca0ca23a9f4f9091ce4521f1b7d5 (patch)
tree119bed8b96e09a60627e456871b4e8fdd24ecb07
parentf64ae515a8fab4a28b2cf0ed429cb3b6f2d881c5 (diff)
Maximize salary done!
-rw-r--r--AlgoDesignAndTechniqueEdxPython/sources/largest_number.py27
-rw-r--r--AlgoDesignAndTechniqueEdxPython/tests/largest_numberTest.py58
2 files changed, 85 insertions, 0 deletions
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