Skip to content

Commit d5303b3

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Time Conversion solved ✓
1 parent bf63b8a commit d5303b3

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
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+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/timeConversion.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
"fmt"
9+
"strconv"
10+
"strings"
11+
12+
utils "gon.cl/algorithm-exercises/src/utils"
13+
)
14+
15+
func TimeConversion(s string) string {
16+
meridian := s[len(s)-2:]
17+
meridian = strings.ToLower(meridian)
18+
19+
timeStr := s[0 : len(s)-2]
20+
time := strings.Split(timeStr, ":")
21+
hour, _ := strconv.Atoi(time[0])
22+
23+
if hour >= 12 {
24+
hour = 0
25+
}
26+
27+
if meridian == "pm" {
28+
hour += 12
29+
}
30+
31+
time[0] = fmt.Sprintf("%02d", hour)
32+
33+
conversion := strings.Join(time, ":")
34+
35+
utils.Info("TimeConversion answer => %s", conversion)
36+
37+
return conversion
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestTimeConversionExample(t *testing.T) {
11+
var tests = []struct {
12+
input string
13+
want string
14+
}{
15+
{input: "12:01:00PM", want: "12:01:00"},
16+
{input: "12:01:00AM", want: "00:01:00"},
17+
}
18+
19+
for _, tt := range tests {
20+
21+
testname := fmt.Sprintf("TimeConversion(%s) => %s", tt.input, tt.want)
22+
t.Run(testname, func(t *testing.T) {
23+
ans := TimeConversion(tt.input)
24+
assert.Equal(t, tt.want, ans)
25+
})
26+
}
27+
}
28+
29+
func TestTimeConversionTestCases(t *testing.T) {
30+
var tests = []struct {
31+
title string
32+
input string
33+
want string
34+
}{
35+
{title: "Case 0", input: "07:05:45PM", want: "19:05:45"},
36+
}
37+
38+
for _, tt := range tests {
39+
40+
testname := fmt.Sprintf("TimeConversion(%s) %s => %s", tt.input, tt.title, tt.want)
41+
t.Run(testname, func(t *testing.T) {
42+
ans := TimeConversion(tt.input)
43+
assert.Equal(t, tt.want, ans)
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)