From 16780664a2d2303755edcce8ca140a30a36bdb8c Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Sun, 15 Apr 2018 19:37:02 -0400 Subject: [PATCH 1/2] gonna add enums later --- pom.xml | 12 +++ src/main/java/io/zipcoder/Problem6.java | 88 +++++++++++++++++++ src/main/java/io/zipcoder/Problem6Redux.java | 4 + .../java/io/zipcoder/Problem6ReduxTest.java | 4 + src/test/java/io/zipcoder/Problem6Test.java | 45 ++++++++++ 5 files changed, 153 insertions(+) create mode 100644 src/main/java/io/zipcoder/Problem6Redux.java create mode 100644 src/test/java/io/zipcoder/Problem6ReduxTest.java diff --git a/pom.xml b/pom.xml index 5fd8efa..7b5cfe2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder InterviewProblem5 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 4ee4e64..062956f 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -1,4 +1,92 @@ package io.zipcoder; +import java.util.Arrays; + + public class Problem6 { + + public static String[] militaryNum = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", + "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", + "Nineteen", "Twenty", "Thirty", "Forty", "Fifty"}; + + + public String militaryConverter(String time) { + Integer[] fullClock = splitHourAndMinutes(time); + return militaryPhrasing(fullClock); + } + + public Integer[] splitHourAndMinutes(String time) { + String[] clockGroup = time.split(":"); + String amOrPm = clockGroup[1].substring(2); + + int hours = Integer.parseInt(clockGroup[0]); + if (hours != 12) { + if (amOrPm.equals("pm")) { + hours += 12; + } + } else { + if (amOrPm.equals("am")) { + hours -= 12; + } + } + int minutes = Integer.parseInt(clockGroup[1].substring(0, 2)); + + Integer[] fullClock = new Integer[2]; + fullClock[0] = hours; + fullClock[1] = minutes; + + return fullClock; + } + + public String militaryPhrasing(Integer[] fullClock){ + int hours = fullClock[0]; + int minutes = fullClock[1]; + StringBuilder stringBuilder = new StringBuilder(); + + if (hours < 10){ + stringBuilder.append(militaryNum[0] + " "); + } + if (hours <= 20){ + stringBuilder.append(militaryNum[hours] + " "); + } + else{ + stringBuilder.append(militaryNum[20] + (militaryNum[hours - 20]) + " "); + } + stringBuilder.append("Hundred "); + + if (minutes > 0) { + stringBuilder.append("and "); + if (minutes <= 20) { + if (minutes < 10) { + stringBuilder.append(militaryNum[0] + " "); + } + stringBuilder.append(militaryNum[minutes] + " "); + } else { + int tenBase = minutes / 10; + int oneBase = minutes - tenBase * 10; + stringBuilder.append(militaryNum[18 + tenBase] + " "); + + if (oneBase > 0) { + stringBuilder.append(militaryNum[oneBase] + " "); + } + } + } + stringBuilder.append("Hours"); + return stringBuilder.toString(); + } + + public static void main(String[] args) { + Problem6 test = new Problem6(); + + String time = "1:35am"; + test.splitHourAndMinutes(time); + System.out.println(Arrays.toString(test.splitHourAndMinutes(time))); + System.out.println(militaryNum[20]); + + } } +//1:30pm +//1 = index[0] +//30pm = index[1] substring(2) = pm +//30 = index[1] substring(0,2) + diff --git a/src/main/java/io/zipcoder/Problem6Redux.java b/src/main/java/io/zipcoder/Problem6Redux.java new file mode 100644 index 0000000..4211dac --- /dev/null +++ b/src/main/java/io/zipcoder/Problem6Redux.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public class Problem6Redux { +} diff --git a/src/test/java/io/zipcoder/Problem6ReduxTest.java b/src/test/java/io/zipcoder/Problem6ReduxTest.java new file mode 100644 index 0000000..e71c293 --- /dev/null +++ b/src/test/java/io/zipcoder/Problem6ReduxTest.java @@ -0,0 +1,4 @@ +package io.zipcoder; + +public class Problem6ReduxTest { +} diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index d262e88..024d6ba 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -1,4 +1,49 @@ package io.zipcoder; +import org.junit.Assert; +import org.junit.Test; + public class Problem6Test { + + + @Test + public void militaryTimeTest(){ + String time = "1:30pm"; + String militaryTime = "Thirteen Hundred and Thirty Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest1(){ + String time = "1:30am"; + String militaryTime = "Zero One Hundred and Thirty Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest2(){ + String time = "2:22pm"; + String militaryTime = "Fourteen Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest3(){ + String time = "2:11am"; + String militaryTime = "Zero Two Hundred and Eleven Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest4(){ + String time = "10:02am"; + String militaryTime = "Ten Hundred and Zero Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } } From 61d2764cbc176dbb470ae55f0c6aad69adee5195 Mon Sep 17 00:00:00 2001 From: Lawrence Wu Date: Tue, 17 Apr 2018 10:49:57 -0400 Subject: [PATCH 2/2] fixed 12:00 double zero --- src/main/java/io/zipcoder/Problem6.java | 7 +++++-- src/test/java/io/zipcoder/Problem6Test.java | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/zipcoder/Problem6.java b/src/main/java/io/zipcoder/Problem6.java index 062956f..43a03de 100644 --- a/src/main/java/io/zipcoder/Problem6.java +++ b/src/main/java/io/zipcoder/Problem6.java @@ -12,6 +12,9 @@ public class Problem6 { public String militaryConverter(String time) { Integer[] fullClock = splitHourAndMinutes(time); + if(militaryPhrasing(fullClock).charAt(0) == militaryPhrasing(fullClock).charAt(5)){ + return militaryPhrasing(fullClock).substring(5); + } return militaryPhrasing(fullClock); } @@ -26,7 +29,7 @@ public Integer[] splitHourAndMinutes(String time) { } } else { if (amOrPm.equals("am")) { - hours -= 12; + hours -= hours; } } int minutes = Integer.parseInt(clockGroup[1].substring(0, 2)); @@ -63,7 +66,7 @@ public String militaryPhrasing(Integer[] fullClock){ stringBuilder.append(militaryNum[minutes] + " "); } else { int tenBase = minutes / 10; - int oneBase = minutes - tenBase * 10; + int oneBase = minutes % 10; stringBuilder.append(militaryNum[18 + tenBase] + " "); if (oneBase > 0) { diff --git a/src/test/java/io/zipcoder/Problem6Test.java b/src/test/java/io/zipcoder/Problem6Test.java index 024d6ba..fda9c92 100644 --- a/src/test/java/io/zipcoder/Problem6Test.java +++ b/src/test/java/io/zipcoder/Problem6Test.java @@ -46,4 +46,20 @@ public void militaryTimeTest4(){ String actual = testProblem6.militaryConverter(time); Assert.assertEquals(militaryTime, actual); } + @Test + public void militaryTimeTest5(){ + String time = "10:22am"; + String militaryTime = "Ten Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } + @Test + public void militaryTimeTest6(){ + String time = "12:22am"; + String militaryTime = "Zero Hundred and Twenty Two Hours"; + Problem6 testProblem6 = new Problem6(); + String actual = testProblem6.militaryConverter(time); + Assert.assertEquals(militaryTime, actual); + } }