Problem Statement
A description of the problem can be found on Hackerrank.
Solution
My solution is folllowing:
After reading an input line I parsed are necessary values.
hours
part of the input time from 0th to index of first':'
minutes
part of the input time from index of first':'
+ 1 to index of last':'
seconds
part of input time from index of last':' + 1
to index of last':' + 3
Then I created new hours
part as follows:
Checked if hour
is 12
- then assign to
0
- otherwise leave it unchanged
And calculated shift
hours.
If input time contains 'PM'
- then assign to
12
- otherwise assing to
0
In the end created formated string and printed to output.
Note: In most programming languages index in strings are 0-based
I created solution in 4 languages:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 |
object TimeConversion extends App { val line = io.Source.stdin.bufferedReader().readLine() val hours = line.substring(0, line indexOf ':').toInt val minutes = line.substring((line indexOf ':') + 1, line lastIndexOf ':').toInt val seconds = line.substring((line lastIndexOf ':') + 1, (line lastIndexOf ':') + 3).toInt val newHours = if (hours == 12) 0 else hours val shift = if(line.toLowerCase contains "pm") 12 else 0 println("%02d:%02d:%02d".format((newHours + shift), minutes, seconds)) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Scanner; public class TimeConversion { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String input = stdin.nextLine(); int hours = Integer.parseInt(input.substring(0, input.indexOf(':'))); int minutes = Integer.parseInt(input.substring(input.indexOf(':') + 1, input.lastIndexOf(':'))); int seconds = Integer.parseInt(input.substring(input.lastIndexOf(':') + 1, input.lastIndexOf(':') + 3)); int newHours; if(hours == 12) { newHours = 0; } else { newHours = hours; } int shift; if(input.toLowerCase().contains("pm")) { shift = 12; } else { shift = 0; } System.out.println(String.format("%02d:%02d:%02d", (newHours + shift), minutes, seconds)); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function processData(input) { var hours = parseInt(input.substring(0, input.indexOf(':'))); var minutes = parseInt(input.substring(input.indexOf(':') + 1, input.lastIndexOf(':'))); var seconds = parseInt(input.substring(input.lastIndexOf(':') + 1, input.lastIndexOf(':') + 3)); var newHours; if(hours === 12) { newHours = 0; } else { newHours = hours; } var shift; if(input.toLowerCase().indexOf('pm') > -1) { shift = 12; } else { shift = 0; } var formatedHours = format((newHours + shift).toString()); var formatedMinutes = format(minutes.toString()); var formatedSeconds = format(seconds.toString()); console.log(formatedHours + ":" + formatedMinutes + ":" + formatedSeconds); function format(numString) { if(numString.length === 1) { return "0" + numString; } return numString; } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
input_time = gets.to_s hours = input_time.slice(0, 2).to_i minutes = input_time.slice(input_time.index(":") + 1, 2).to_i seconds = input_time.slice(input_time.index(":", input_time.index(":") + 1) + 1, 2).to_i newHours = hours if hours == 12 then newHours = 0 end shift = 0 if input_time.upcase.include? "PM" then shift = 12 end puts "%02d:%02d:%02d" % [newHours + shift, minutes, seconds] |