-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank] Warm up: Time Conversion solved ✓
- Loading branch information
Gonzalo Diaz
committed
May 27, 2024
1 parent
8af8472
commit dffdda5
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
algorithm-exercises-java/src/main/java/ae/hackerrank/warmup/TimeConversion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ae.hackerrank.warmup; | ||
|
||
import org.checkerframework.common.value.qual.StringVal; | ||
|
||
/** | ||
* Time Conversion. | ||
* | ||
* @link Problem definition [[docs/hackerrank/warmup/solve_me_first.md]] | ||
*/ | ||
public class TimeConversion { | ||
|
||
private TimeConversion() { | ||
} | ||
|
||
static java.util.logging.Logger logger = util.CustomLogger.getLogger(); | ||
|
||
/** | ||
* timeConversion. | ||
*/ | ||
public static String timeConversion(String input) { | ||
String meridian = input.substring(input.length() - 2); | ||
meridian = meridian.toLowerCase(); | ||
|
||
String timeStr = input.substring(0, input.length() - 2); | ||
String[] time = timeStr.split(":"); | ||
|
||
int hour = Integer.parseInt(time[0]); | ||
|
||
if (hour >= 12) { | ||
hour = 0; | ||
} | ||
if (meridian.equals("pm")) { | ||
hour += 12; | ||
} | ||
|
||
time[0] = String.format("%02d", hour); | ||
|
||
return String.join(":", time); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
algorithm-exercises-java/src/test/java/ae/hackerrank/warmup/TimeConversionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ae.hackerrank.warmup; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.junit.jupiter.api.TestInstance.Lifecycle; | ||
|
||
@TestInstance(Lifecycle.PER_CLASS) | ||
class TimeConversionTest { | ||
public class TimeConversionTestCase { | ||
public String input; | ||
public String expected; | ||
|
||
public TimeConversionTestCase(String _input, String _expected) { | ||
this.input = _input; | ||
this.expected = _expected; | ||
} | ||
} | ||
|
||
public List<TimeConversionTestCase> testCases; | ||
|
||
@BeforeAll | ||
public void setup() { | ||
this.testCases = Arrays.asList( | ||
new TimeConversionTestCase("12:01:00PM", "12:01:00"), | ||
new TimeConversionTestCase("12:01:00AM", "00:01:00") | ||
); | ||
} | ||
|
||
@Test | ||
void testStaircase() { | ||
for (TimeConversionTestCase testCase : this.testCases) { | ||
String solutionFound = TimeConversion.timeConversion(testCase.input); | ||
|
||
assertEquals(testCase.expected, solutionFound, | ||
String.format("%s(%s) answer must be: %s", | ||
"Staircase.staircase", | ||
testCase.input, | ||
testCase.expected)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# [Time Conversion](https://www.hackerrank.com/challenges/time-conversion) | ||
|
||
Difficulty: #easy | ||
Category: #warmup | ||
|
||
Given a time in | ||
12-[hour AM/PM format](https://en.wikipedia.org/wiki/12-hour_clock), | ||
convert it to military (24-hour) time. | ||
|
||
Note: | ||
|
||
- 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock. | ||
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock. | ||
|
||
## Example | ||
|
||
- s = '12:01:00PM' \ | ||
Return '12:01:00' | ||
- s = '12:01:00AM' \ | ||
Return '00:01:00' | ||
|
||
## Function Description | ||
|
||
Complete the timeConversion function in the editor below. | ||
It should return a new string representing the input time in 24 hour format | ||
timeConversion has the following parameter(s): | ||
|
||
- string s: a time in 12 hour format | ||
|
||
## Returns | ||
|
||
- string: the time in 24 hour format | ||
|
||
## Input Format | ||
|
||
A single string s that represents a time in 12-hour clock format | ||
(i.e.: hh_mm_ssAM or hh:mm:ssPM). | ||
|
||
## Constraints | ||
|
||
- All input times are valid | ||
|
||
## Sample Input 0 | ||
|
||
```text | ||
07:05:45PM | ||
``` | ||
|
||
## Sample Output 0 | ||
|
||
```text | ||
19:05:45 | ||
``` |