From 584340f8f3c7868706df9e05b0c805ebe982bc7d Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 16 Sep 2024 21:38:20 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Warmup:=20Time=20Conversion?= =?UTF-8?q?=20solved=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/time_conversion.md | 53 ++++++++++++++ .../hackerrank/warmup/time_conversion.hpp | 12 ++++ .../src/hackerrank/warmup/time_conversion.cpp | 69 +++++++++++++++++++ .../warmup/test_conversion.test.cpp | 38 ++++++++++ .../warmup/time_conversion.testcases.json | 4 ++ 5 files changed, 176 insertions(+) create mode 100644 docs/hackerrank/warmup/time_conversion.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.hpp create mode 100644 src/lib/exercises/src/hackerrank/warmup/time_conversion.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/test_conversion.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/warmup/time_conversion.testcases.json 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 +``` diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.hpp b/src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.hpp new file mode 100644 index 0000000..9f25ee0 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace hackerrank::warmup { + +std::string firstN(const std::string_view &input, unsigned long n); +std::string lastN(const std::string_view &input, unsigned long n); + +std::string timeConversion(const std::string &s); + +} // namespace hackerrank::warmup diff --git a/src/lib/exercises/src/hackerrank/warmup/time_conversion.cpp b/src/lib/exercises/src/hackerrank/warmup/time_conversion.cpp new file mode 100644 index 0000000..a9f74c4 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/time_conversion.cpp @@ -0,0 +1,69 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]] + */ + +#include +#include +#include +#include + +namespace hackerrank::warmup { + +std::string firstN(const std::string_view &input, unsigned long n) { + unsigned long inputSize = input.size(); + return static_cast((n > 0 && inputSize > n) ? input.substr(0, n) + : ""); +} + +std::string lastN(const std::string_view &input, unsigned long n) { + unsigned long inputSize = input.size(); + return static_cast( + (n > 0 && inputSize > n) ? input.substr(inputSize - n) : ""); +} + +std::string timeConversion(const std::string &s) { + char TIME_SEPARATOR = ':'; + std::string meridian = lastN(s, 2); + + auto time_str = std::stringstream(firstN(s, s.size() - 2)); + std::string segment; + std::vector time; + + while (std::getline(time_str, segment, TIME_SEPARATOR)) { + time.push_back(segment); + } + + std::stringstream hour_str; + hour_str << time[0]; + + int hour; + hour_str >> hour; + + if (hour >= 12) { + hour = 0; + } + + if (meridian.compare("PM") == 0) { + hour += 12; + } + + hour_str.str(""); + hour_str.clear(); + hour_str << std::setfill('0') << std::setw(2) << hour; + time[0] = hour_str.str(); + + std::string conversion; + unsigned long tsize = time.size(); + for (int i = 0; i < tsize; i++) { + conversion += time[i]; + if (i < tsize - 1) { + conversion += ":"; + } + } + + return conversion; +} + +} // namespace hackerrank::warmup diff --git a/src/tests/unit/lib/hackerrank/warmup/test_conversion.test.cpp b/src/tests/unit/lib/hackerrank/warmup/test_conversion.test.cpp new file mode 100644 index 0000000..d29460f --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/test_conversion.test.cpp @@ -0,0 +1,38 @@ +#include + +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +TEST_CASE("time_conversion JSON Test Cases", + "[hackerrank] [jsontestcase] [warmup]") { + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = + cwd.string() + + "/unit/lib/hackerrank/warmup/time_conversion.testcases.json"; + + INFO("time_conversion JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + std::string result = hackerrank::warmup::timeConversion(testcase["input"]); + CHECK(result == testcase["expected"]); + + hackerrank::warmup::timeConversion(testcase["input"]); + } +} + +TEST_CASE("time_conversion helper functions edge cases", + "[hackerrank] [helper] [warmup]") { + CHECK(hackerrank::warmup::firstN("", 10) == ""); + CHECK(hackerrank::warmup::lastN("", 10) == ""); + + CHECK(hackerrank::warmup::firstN("", 0) == ""); + CHECK(hackerrank::warmup::lastN("", 0) == ""); +} diff --git a/src/tests/unit/lib/hackerrank/warmup/time_conversion.testcases.json b/src/tests/unit/lib/hackerrank/warmup/time_conversion.testcases.json new file mode 100644 index 0000000..16c374f --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/time_conversion.testcases.json @@ -0,0 +1,4 @@ +[ + { "input": "12:01:00PM", "expected": "12:01:00" }, + { "input": "12:01:00AM", "expected": "00:01:00" } +]