Skip to content

Commit e50951c

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Warm up: Time Conversion solved ✓
1 parent 8af8472 commit e50951c

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package ae.hackerrank.warmup;
2+
3+
4+
/**
5+
* Time Conversion.
6+
*
7+
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]
8+
*/
9+
public class TimeConversion {
10+
11+
private TimeConversion() {
12+
}
13+
14+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
15+
16+
/**
17+
* timeConversion.
18+
*/
19+
public static String timeConversion(String input) {
20+
String meridian = input.substring(input.length() - 2);
21+
meridian = meridian.toLowerCase();
22+
23+
String timeStr = input.substring(0, input.length() - 2);
24+
String[] time = timeStr.split(":");
25+
26+
int hour = Integer.parseInt(time[0]);
27+
28+
hour = hour % 12;
29+
30+
if (meridian.equals("pm")) {
31+
hour += 12;
32+
}
33+
34+
time[0] = String.format("%02d", hour);
35+
36+
return String.join(":", time);
37+
}
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package ae.hackerrank.warmup;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
12+
@TestInstance(Lifecycle.PER_CLASS)
13+
class TimeConversionTest {
14+
public class TimeConversionTestCase {
15+
public String input;
16+
public String expected;
17+
18+
public TimeConversionTestCase(String _input, String _expected) {
19+
this.input = _input;
20+
this.expected = _expected;
21+
}
22+
}
23+
24+
public List<TimeConversionTestCase> testCases;
25+
26+
@BeforeAll
27+
public void setup() {
28+
this.testCases = Arrays.asList(
29+
new TimeConversionTestCase("12:01:00PM", "12:01:00"),
30+
new TimeConversionTestCase("12:01:00AM", "00:01:00")
31+
);
32+
}
33+
34+
@Test
35+
void testStaircase() {
36+
for (TimeConversionTestCase testCase : this.testCases) {
37+
String solutionFound = TimeConversion.timeConversion(testCase.input);
38+
39+
assertEquals(testCase.expected, solutionFound,
40+
String.format("%s(%s) answer must be: %s",
41+
"Staircase.staircase",
42+
testCase.input,
43+
testCase.expected));
44+
}
45+
}
46+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [Time Conversion](https://www.hackerrank.com/challenges/time-conversion)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given a time in
7+
12-[hour AM/PM format](https://en.wikipedia.org/wiki/12-hour_clock),
8+
convert it to military (24-hour) time.
9+
10+
Note:
11+
12+
- 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
13+
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.
14+
15+
## Example
16+
17+
- s = '12:01:00PM' \
18+
Return '12:01:00'
19+
- s = '12:01:00AM' \
20+
Return '00:01:00'
21+
22+
## Function Description
23+
24+
Complete the timeConversion function in the editor below.
25+
It should return a new string representing the input time in 24 hour format
26+
timeConversion has the following parameter(s):
27+
28+
- string s: a time in 12 hour format
29+
30+
## Returns
31+
32+
- string: the time in 24 hour format
33+
34+
## Input Format
35+
36+
A single string s that represents a time in 12-hour clock format
37+
(i.e.: hh_mm_ssAM or hh:mm:ssPM).
38+
39+
## Constraints
40+
41+
- All input times are valid
42+
43+
## Sample Input 0
44+
45+
```text
46+
07:05:45PM
47+
```
48+
49+
## Sample Output 0
50+
51+
```text
52+
19:05:45
53+
```

0 commit comments

Comments
 (0)