Skip to content

[Hacker Rank]: Warmup: Time Conversion solved ✓ #52

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 22, 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,29 @@
namespace algorithm_exercises_csharp.hackerrank;

[TestClass]
public class TimeConversionTest
{
public class TimeConversionTestCase
{
public string input = "";
public string expected = "";
}

private static readonly TimeConversionTestCase[] tests = [
new() { input = "12:01:00PM", expected = "12:01:00" },
new() { input = "12:01:00AM", expected = "00:01:00" }
];

[TestMethod]
public void testTimeConversion()
{
string? result;

foreach (TimeConversionTestCase test in tests)
{
result = TimeConversion.timeConversion(test.input);
Assert.AreEqual(test.expected, result);
}
}
}

34 changes: 34 additions & 0 deletions algorithm-exercises-csharp/src/hackerrank/warmup/TimeConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]

namespace algorithm_exercises_csharp.hackerrank;

using System.Diagnostics.CodeAnalysis;

public class TimeConversion
{
[ExcludeFromCodeCoverage]
protected TimeConversion() { }

public static string timeConversion(string _s)
{
string meridian = _s[^2..];
meridian = meridian.ToLower();

string time_str = _s[0..(_s.Length - 2)];
List<string> time = new(time_str.Split(":"));

int hour = Int32.Parse(time[0]);

if (hour >= 12)
{
hour = 0;
}
if (meridian == "pm")
{
hour += 12;
}

time[0] = String.Format("{0:00}", hour);
return String.Join(":", time);
}
}
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