Skip to content

Commit 3428813

Browse files
authored
Merge pull request #37 from sir-gon/feature/time_conversion
[Hacker Rank] Warmup: Time Conversion solved ✅
2 parents 12b920b + 584340f commit 3428813

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
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+
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace hackerrank::warmup {
6+
7+
std::string firstN(const std::string_view &input, unsigned long n);
8+
std::string lastN(const std::string_view &input, unsigned long n);
9+
10+
std::string timeConversion(const std::string &s);
11+
12+
} // namespace hackerrank::warmup
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <exercises/hackerrank/warmup/time_conversion.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]
5+
*/
6+
7+
#include <iomanip>
8+
#include <sstream>
9+
#include <string>
10+
#include <vector>
11+
12+
namespace hackerrank::warmup {
13+
14+
std::string firstN(const std::string_view &input, unsigned long n) {
15+
unsigned long inputSize = input.size();
16+
return static_cast<std::string>((n > 0 && inputSize > n) ? input.substr(0, n)
17+
: "");
18+
}
19+
20+
std::string lastN(const std::string_view &input, unsigned long n) {
21+
unsigned long inputSize = input.size();
22+
return static_cast<std::string>(
23+
(n > 0 && inputSize > n) ? input.substr(inputSize - n) : "");
24+
}
25+
26+
std::string timeConversion(const std::string &s) {
27+
char TIME_SEPARATOR = ':';
28+
std::string meridian = lastN(s, 2);
29+
30+
auto time_str = std::stringstream(firstN(s, s.size() - 2));
31+
std::string segment;
32+
std::vector<std::string> time;
33+
34+
while (std::getline(time_str, segment, TIME_SEPARATOR)) {
35+
time.push_back(segment);
36+
}
37+
38+
std::stringstream hour_str;
39+
hour_str << time[0];
40+
41+
int hour;
42+
hour_str >> hour;
43+
44+
if (hour >= 12) {
45+
hour = 0;
46+
}
47+
48+
if (meridian.compare("PM") == 0) {
49+
hour += 12;
50+
}
51+
52+
hour_str.str("");
53+
hour_str.clear();
54+
hour_str << std::setfill('0') << std::setw(2) << hour;
55+
time[0] = hour_str.str();
56+
57+
std::string conversion;
58+
unsigned long tsize = time.size();
59+
for (int i = 0; i < tsize; i++) {
60+
conversion += time[i];
61+
if (i < tsize - 1) {
62+
conversion += ":";
63+
}
64+
}
65+
66+
return conversion;
67+
}
68+
69+
} // namespace hackerrank::warmup
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/time_conversion.hpp>
4+
#include <filesystem>
5+
#include <fstream>
6+
#include <nlohmann/json.hpp>
7+
#include <vector>
8+
9+
using json = nlohmann::json;
10+
11+
TEST_CASE("time_conversion JSON Test Cases",
12+
"[hackerrank] [jsontestcase] [warmup]") {
13+
std::filesystem::path cwd = std::filesystem::current_path();
14+
std::string path =
15+
cwd.string() +
16+
"/unit/lib/hackerrank/warmup/time_conversion.testcases.json";
17+
18+
INFO("time_conversion JSON test cases FILE: " << path);
19+
20+
std::ifstream f(path);
21+
json data = json::parse(f);
22+
23+
for (auto testcase : data) {
24+
std::string result = hackerrank::warmup::timeConversion(testcase["input"]);
25+
CHECK(result == testcase["expected"]);
26+
27+
hackerrank::warmup::timeConversion(testcase["input"]);
28+
}
29+
}
30+
31+
TEST_CASE("time_conversion helper functions edge cases",
32+
"[hackerrank] [helper] [warmup]") {
33+
CHECK(hackerrank::warmup::firstN("", 10) == "");
34+
CHECK(hackerrank::warmup::lastN("", 10) == "");
35+
36+
CHECK(hackerrank::warmup::firstN("", 0) == "");
37+
CHECK(hackerrank::warmup::lastN("", 0) == "");
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "input": "12:01:00PM", "expected": "12:01:00" },
3+
{ "input": "12:01:00AM", "expected": "00:01:00" }
4+
]

0 commit comments

Comments
 (0)