From 46b8bc73e7a78bdf539f7c43d849c434fed35224 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Tue, 2 Oct 2018 08:10:29 -0500 Subject: Checking in for pulling from laptop. Maximize salary close to done. Still some bugs. Will likely fix it on my laptop.--- .../sources/LargestNumber.java | 59 ++++++++++++++++++ .../tests/LargestNumberTest.java | 72 ++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java create mode 100644 AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java diff --git a/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java new file mode 100644 index 0000000..0458b91 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java @@ -0,0 +1,59 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class LargestNumber { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + String[] a = new String[n]; + for (int i = 0; i < n; i++) { + a[i] = scanner.next(); + } + System.out.println(getLargestNumber(a)); + } + + public static String getLargestNumber(String[] numbers) { + List numList = new ArrayList(Arrays.asList(numbers)); + String result = ""; + String maxDigit = "0"; + int indexHolder = 0; + while (numList.size() > 0) { + for (int i = 0; i < numList.size(); i++) { + if (compareLeadingDigit(numList.get(i), maxDigit)) { + maxDigit = numList.get(i); + indexHolder = i; + } + } + result = result + maxDigit; + maxDigit = "0"; + numList.remove(indexHolder); + } + return result; + } + + public static boolean compareLeadingDigit(String string, String maxDigit) { + if (string.length() == maxDigit.length()) + return Integer.parseInt(string) > Integer.parseInt(maxDigit); + if (string.length() < maxDigit.length()) { + if (string.charAt(0) > maxDigit.charAt(0)) { + return true; + } else if (string.charAt(0) < maxDigit.charAt(0)) { + return false; + } else if (string.charAt(0) > maxDigit.charAt(1)) { + return true; + } else if (string.charAt(0) < maxDigit.charAt(1)) { + return false; + } else if (maxDigit.length() == 2) + return false; + else { + return string.charAt(0) > maxDigit.charAt(2); + } + } else { + return !compareLeadingDigit(maxDigit, string); + } + } + +} diff --git a/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java new file mode 100644 index 0000000..285d502 --- /dev/null +++ b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java @@ -0,0 +1,72 @@ +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +class LargestNumberTest { + + @Test + void test1() { + String[] numbers = {"9", "4", "6", "1", "9"}; + assertEquals("99641", LargestNumber.getLargestNumber(numbers)); + } + + @Test + void test2() { + String[] numbers = {"21", "2"}; + assertEquals("221", LargestNumber.getLargestNumber(numbers)); + } + + @Test + void test3() { + String[] numbers = {"23", "39", "92"}; + assertEquals("923923", LargestNumber.getLargestNumber(numbers)); + } + + @Test + void test4() { + String[] numbers = {"23", "39", "92"}; + assertEquals("923923", LargestNumber.getLargestNumber(numbers)); + } + + @Test + void test5() { + String[] numbers = {"23", "2"}; + assertEquals("232", LargestNumber.getLargestNumber(numbers)); + } + + @Test + void testCompareLeadingDigit() { + assertTrue(LargestNumber.compareLeadingDigit("2", "21")); + } + + @Test + void testCompareLeadingDigit1() { + assertFalse(LargestNumber.compareLeadingDigit("20", "21")); + } + + @Test + void testCompareLeadingDigit2() { + assertFalse(LargestNumber.compareLeadingDigit("321", "322")); + } + + @Test + void testCompareLeadingDigit3() { + assertFalse(LargestNumber.compareLeadingDigit("321", "32")); + } + + @Test + void testCompareLeadingDigit4() { + assertFalse(LargestNumber.compareLeadingDigit("10", "1")); + } + + @Test + void testCompareLeadingDigit5() { + assertTrue(LargestNumber.compareLeadingDigit("6", "10")); + } + + @Test + void testCompareLeadingDigit6() { + assertTrue(LargestNumber.compareLeadingDigit("23", "2")); + } + +} -- cgit v1.2.3