I received a message from HackerRank to solve the The Time in Words challenge. If you try to solve it; as usual; read the description a few times to make sure the boundary conditions are well understood. I was reading some of the discussions and the 1 <= H < 12 constraint was omitted by a couple people.
There is a typo in the specifications which carries into the test cases. Before you submit your solution make sure that “o’clock” is misspelled as “o’ clock” (incorrect space between the apostrophe and the word clock).
Following is the output from the console of the Eclipse IDE using the test cases plus some of my own:
7 29 6 0 5 59 6 15 6 30 6 45 6 25 6 35 5 47 3 0 5 0 5 1 5 10 5 30 5 40 5 44 5 45 5 46 5 47 5 28 5 29 5 15 1 0 1 1 11 59 -1 07:29 twenty nine minutes past seven 06:00 six o' clock 05:59 one minute to six 06:15 quarter past six 06:30 half past six 06:45 quarter to seven 06:25 twenty five minutes past six 06:35 twenty five minutes to seven 05:47 thirteen minutes to six 03:00 three o' clock 05:00 five o' clock 05:01 one minute past five 05:10 ten minutes past five 05:30 half past five 05:40 twenty minutes to six 05:44 sixteen minutes to six 05:45 quarter to six 05:46 fourteen minutes to six 05:47 thirteen minutes to six 05:28 twenty eight minutes past five 05:29 twenty nine minutes past five 05:15 quarter past five 01:00 one o' clock 01:01 one minute past one 11:59 one minute to twelve
The display of the input before each conversion is an artifact of my code which I commented out when I submitted it.
Following is my accepted solution in Java 8:
import java.util.Scanner; public class Solution { static String[] numbers = { "zero", // 0 "one", // 1 "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", // 10 "eleven", // 11 "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", // 20 "twenty one", // 21 "twenty two", "twenty three", "twenty four", "twenty five", "twenty six", "twenty seven", "twenty eight", "twenty nine" // 29 }; /** * */ static void toWords(int h, int m) { String currentTime = ""; // **** at hour **** if (m == 0) { currentTime = numbers[h] + " o' clock"; } // **** at quarter past **** else if (m == 15) { currentTime = "quarter past " + numbers[h]; } // **** at half hour **** else if (m == 30) { currentTime = "half past " + numbers[h]; } // **** at quarter before **** else if (m == 45) { currentTime = "quarter to " + numbers[h + 1]; } // **** before half hour **** else if (m < 30) { currentTime = numbers[m] + " minute" + (m <= 1 ? "" : "s") + " past " + numbers[h]; } // **** past half hour **** else { currentTime = numbers[60 - m] + " minute" + ((60 - m) <= 1 ? "" : "s") + " to " + numbers[h + 1]; } // **** display the current time **** System.out.println(currentTime); } /** * Test code. */ public static void main(String[] args) { // **** open scanner **** Scanner sc = new Scanner(System.in); // **** loop until done **** while (true) { // **** read the hour **** int h = sc.nextInt(); if (h == -1) { break; } sc.nextLine(); // **** read the minutes **** int m = sc.nextInt(); sc.nextLine(); System.out.printf("%02d:%02d\n", h, m); // **** convert and display time in words **** toWords(h, m); } // **** close scanner **** sc.close(); } }
I was going to get more creative with the contents of the numbers[] array but decided I had better things to do (i.e., actual work).
If you have comments or questions regarding this or any other post in this blog, please do not hesitate and send me a message. Will reply as soon as possible and will not use your name unless you explicitly allow me to do so.
Regards;
John
john.canessa@gmail.com
Follow me on Twitter: @john_canessa