From 8ce475dc176652513e48ebd2716bc04861eb26bd Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 22 May 2024 18:24:26 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Time=20Conversion?= =?UTF-8?q?=20solved=20=E2=9C=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hackerrank/warmup/TimeConversion.Test.cs | 29 ++++++++++ .../src/hackerrank/warmup/TimeConversion.cs | 34 ++++++++++++ docs/hackerrank/warmup/time_conversion.md | 53 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/hackerrank/warmup/TimeConversion.Test.cs create mode 100644 algorithm-exercises-csharp/src/hackerrank/warmup/TimeConversion.cs create mode 100644 docs/hackerrank/warmup/time_conversion.md diff --git a/algorithm-exercises-csharp-test/src/hackerrank/warmup/TimeConversion.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/warmup/TimeConversion.Test.cs new file mode 100644 index 0000000..2af40b1 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/warmup/TimeConversion.Test.cs @@ -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); + } + } +} + diff --git a/algorithm-exercises-csharp/src/hackerrank/warmup/TimeConversion.cs b/algorithm-exercises-csharp/src/hackerrank/warmup/TimeConversion.cs new file mode 100644 index 0000000..14002d0 --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/warmup/TimeConversion.cs @@ -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 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); + } +} diff --git a/docs/hackerrank/warmup/time_conversion.md b/docs/hackerrank/warmup/time_conversion.md new file mode 100644 index 0000000..273cd14 --- /dev/null +++ b/docs/hackerrank/warmup/time_conversion.md @@ -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 +```