-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Hacker Rank] Warmup: Time Conversion solved ✅
- Loading branch information
Gonzalo Diaz
committed
Sep 17, 2024
1 parent
12b920b
commit 8ad5062
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
12 changes: 12 additions & 0 deletions
12
src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
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 |
69 changes: 69 additions & 0 deletions
69
src/lib/exercises/src/hackerrank/warmup/time_conversion.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <exercises/hackerrank/warmup/time_conversion.hpp> | ||
|
||
/** | ||
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]] | ||
*/ | ||
|
||
#include <iomanip> | ||
#include <sstream> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace hackerrank::warmup { | ||
|
||
std::string firstN(const std::string_view &input, unsigned long n) { | ||
unsigned long inputSize = input.size(); | ||
return static_cast<std::string>((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<std::string>( | ||
(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<std::string> 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 |
39 changes: 39 additions & 0 deletions
39
src/tests/unit/lib/hackerrank/warmup/test_conversion.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <exercises/hackerrank/warmup/time_conversion.hpp> | ||
#include <filesystem> | ||
#include <fstream> | ||
#include <nlohmann/json.hpp> | ||
#include <vector> | ||
|
||
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) == ""); | ||
} |
4 changes: 4 additions & 0 deletions
4
src/tests/unit/lib/hackerrank/warmup/time_conversion.testcases.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
{ "input": "12:01:00PM", "expected": "12:01:00" }, | ||
{ "input": "12:01:00AM", "expected": "00:01:00" } | ||
] |