I don't like to post programming contest code in here, but I really like the question and want to share it with you all.
It has been years since Superman started his enmity with the super-genius Lex Luthor. After years of intense battle, Luthor finally decides to end it all by going into the past and capturing Superman the moment he lands on Earth. Using the time warp machine that he built, Luthor goes back in time. Superman, wanting to thwart his evil intentions, asks The Flash to push him back into time by breaking the time barrier.
The Flash obliges and throws Superman back in time. But Superman senses that something is fishy and the Earth doesn't feel the same. After an hour of intense detective work (which he isn’t used to) he finds out that he has been sent into a parallel anti-universe where everything is reverse to that of our Universe. Looking at his watch, Superman realizes that he'll first need to correct the time on his watch in order to reach Kansas, the place where his ship landed, on time.
Superman, not being as smart as you, asks for your help in fixing the time on his watch. You know that the actual time on the other anti-Universe is the mirror image of the time on Superman's watch, considering the mirror to be a vertical line running from 12 to 6 on the watch.
INPUT
The first line contains an integer T, denoting the number of test cases. T lines follow with the time given in the 12-hour format: HH:MM am/pm
OUTPUT
The output should consist of T lines with the mirrored time of each test case in the 12-hour format: HH:MM am/pm
SAMPLE I/O
Input
3 05:00 am 06:00 pm 03:15 pm
Output
07:00 pm 06:00 am 08:45 am
TL;DR
You will be given a time as XX:XX am/pm
(12 hour format) and you have to print the time that you will see if you hold the analog clock beside the mirror.
A picture worth thousands words
package foo.competition;
import java.util.Scanner;
public class Parallel {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
// number of testcases
int t = input.nextInt();
// feeding newline
input.nextLine();
while(t-- > 0) {
String actualTime = input.nextLine();
String parallelUniverseTime = parallelUniverseTime(actualTime);
System.out.println(parallelUniverseTime);
}
}
}
/**
*
* @param actualTimeStamp - actual time. format XX.XX am/pm (12 hour clock)
* @return - a String formatted as "XX:XX am/pm" as the time in parallel universe
*/
public static String parallelUniverseTime(String actualTimeStamp) {
String []time = parseTimeAndReturnAsArray(actualTimeStamp);
String mirrorHour = mirroredHour(time[0], time[1]);
String mirrorMinute = mirroredMinute(time[1]);
String mirrorMedian = mirroredMedian(time[2]);
return formattedMirroredTime(mirrorHour, mirrorMinute, mirrorMedian);
}
/**
*
* @param timeStamp - actual time. format XX:XX am/pm (12 hour clock)
* @return - an array as {hour, minute, median}
*/
private static String[] parseTimeAndReturnAsArray(String timeStamp) {
// timeStamp example - "07:30 pm"
String hour = timeStamp.charAt(0) + "" + timeStamp.charAt(1);
String minute = timeStamp.charAt(3) + "" + timeStamp.charAt(4);
String median = timeStamp.charAt(6) + "" + timeStamp.charAt(7);
return new String[]{hour, minute, median};
}
private static String mirroredHour(final String hour, final String minute) {
String hourAndMinute = hour + ":" + minute;
if(hourAndMinute.equals("12:00") || hourAndMinute.equals("06:00")) {
return hour;
}
final int minuteAsInt = Integer.parseInt(minute);
final int hourAsInt = Integer.parseInt(hour);
// after 12 min the hour hand moves one place
// so before XX:12 the mirrored hour will be (12 - hour)
if(minuteAsInt < 12) {
return (12 - hourAsInt) + "";
}
// otherwise hour hand already moved so mirrored hour hand
// will near to 6.
else {
return (12 - hourAsInt - 1) + "";
}
}
private static String mirroredMinute(final String minute) {
if(minute.equals("00")) {
return minute;
}
final int minuteAsInt = Integer.parseInt(minute);
// return the mirrored minute hand
return (60 - minuteAsInt) + "";
}
private static String mirroredMedian(final String median) {
if(median.equals("am")) return "pm";
else return "am";
}
private static String formattedMirroredTime(String hour, String minute, String median) {
if(hour.length() == 1) { hour = "0" + hour; }
return (hour + ":" + minute + " " + median);
}
}
There are many pitfalls of the code:
- I didn't handle the user input error case.
- All methods are static and in under one class.
- Bad documentation.
and many more...
But I wrote the whole code under 1 hour, so I'd like to get a review my code as I wrote the first time. All kind of reviewing and criticization are welcomed.
I didn't find any bugs in my code, but if you find any, please comment.
median
should bemeridiem
orperiod
. \$\endgroup\$