Skip to content

Commit

Permalink
[Hacker Rank] Warm up: Time Conversion solved ✓
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Diaz committed May 27, 2024
1 parent 8af8472 commit dffdda5
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
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);
}
}
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));
}
}
}
53 changes: 53 additions & 0 deletions docs/hackerrank/warmup/time_conversion.md
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
```

0 comments on commit dffdda5

Please sign in to comment.