Skip to content

[Hacker Rank] Warm up: Time Conversion solved ✓ #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ae.hackerrank.warmup;


/**
* Time Conversion.
*
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.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]);

hour = hour % 12;

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
```
Loading