Skip to content

Commit 8ce475d

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup: Time Conversion solved ✓
1 parent 13e95b0 commit 8ce475d

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace algorithm_exercises_csharp.hackerrank;
2+
3+
[TestClass]
4+
public class TimeConversionTest
5+
{
6+
public class TimeConversionTestCase
7+
{
8+
public string input = "";
9+
public string expected = "";
10+
}
11+
12+
private static readonly TimeConversionTestCase[] tests = [
13+
new() { input = "12:01:00PM", expected = "12:01:00" },
14+
new() { input = "12:01:00AM", expected = "00:01:00" }
15+
];
16+
17+
[TestMethod]
18+
public void testTimeConversion()
19+
{
20+
string? result;
21+
22+
foreach (TimeConversionTestCase test in tests)
23+
{
24+
result = TimeConversion.timeConversion(test.input);
25+
Assert.AreEqual(test.expected, result);
26+
}
27+
}
28+
}
29+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class TimeConversion
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected TimeConversion() { }
11+
12+
public static string timeConversion(string _s)
13+
{
14+
string meridian = _s[^2..];
15+
meridian = meridian.ToLower();
16+
17+
string time_str = _s[0..(_s.Length - 2)];
18+
List<string> time = new(time_str.Split(":"));
19+
20+
int hour = Int32.Parse(time[0]);
21+
22+
if (hour >= 12)
23+
{
24+
hour = 0;
25+
}
26+
if (meridian == "pm")
27+
{
28+
hour += 12;
29+
}
30+
31+
time[0] = String.Format("{0:00}", hour);
32+
return String.Join(":", time);
33+
}
34+
}
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)