Skip to content

Commit ae8982e

Browse files
authored
Merge pull request #31 from sir-gon/feature/time_conversion
[Hacker Rank] Warmup: Time Conversion solved ✅
2 parents 7105075 + ae4931a commit ae8982e

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
char *HACKERRANK_WARMUP_getStringFragment(const char *s, unsigned long len,
8+
unsigned long from, unsigned long to);
9+
10+
char *HACKERRANK_WARMUP_timeConversion(const char *s);
11+
12+
#ifdef __cplusplus
13+
} // extern "C"
14+
#endif
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <exercises/hackerrank/warmup/time_conversion.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/time_conversion.md]]
5+
*/
6+
7+
#include <stdio.h> // snprintf
8+
#include <stdlib.h> // malloc, free, strtol
9+
#include <string.h> // strcmp, strnlen
10+
11+
#define HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE 10
12+
#define HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE 8
13+
#define HACKERRANK_WARMUP_HOUR_FORMAT_SIZE 2
14+
15+
char *HACKERRANK_WARMUP_timeConversion(const char *s) {
16+
if (s == NULL) {
17+
return NULL;
18+
}
19+
20+
size_t s_len = strnlen(s, HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE);
21+
22+
if (s_len != HACKERRANK_WARMUP_LONG_TIME_FORMAT_SIZE) {
23+
return NULL;
24+
}
25+
26+
char hour_str[HACKERRANK_WARMUP_HOUR_FORMAT_SIZE + 1];
27+
28+
hour_str[0] = s[0];
29+
hour_str[1] = s[1];
30+
hour_str[2] = '\0';
31+
32+
char meridian[3];
33+
meridian[0] = s[s_len - 2];
34+
meridian[1] = s[s_len - 1];
35+
meridian[2] = '\0';
36+
37+
char *endptr;
38+
long hour = strtol(hour_str, &endptr, 10);
39+
40+
if (*endptr != '\0') {
41+
printf("Conversion error, non-convertible part: %s\n", endptr);
42+
43+
return NULL;
44+
} else {
45+
printf("The integer value is: %ld\n", hour);
46+
}
47+
48+
hour = hour % 12;
49+
50+
if (strcmp(meridian, "PM") == 0) {
51+
hour += 12;
52+
}
53+
54+
char *conversion =
55+
malloc((HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE + 1) * sizeof(char));
56+
57+
for (int i = 0; i < HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE; i++) {
58+
conversion[i] = s[i];
59+
}
60+
conversion[HACKERRANK_WARMUP_SHORT_TIME_FORMAT_SIZE] = '\0';
61+
62+
snprintf(hour_str, HACKERRANK_WARMUP_HOUR_FORMAT_SIZE + 1, "%02ld", hour);
63+
64+
conversion[0] = hour_str[0];
65+
conversion[1] = hour_str[1];
66+
67+
return conversion;
68+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/time_conversion.h>
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+
char *result = HACKERRANK_WARMUP_timeConversion(
25+
testcase["input"].get<std::string>().c_str());
26+
27+
std::string result_as_string(result);
28+
29+
free(result);
30+
31+
CHECK(result_as_string == testcase["expected"]);
32+
}
33+
}
34+
35+
TEST_CASE("time_conversion edge cases", "[hackerrank] [helper] [warmup]") {
36+
CHECK(HACKERRANK_WARMUP_timeConversion(nullptr) == nullptr);
37+
38+
CHECK(HACKERRANK_WARMUP_timeConversion("") == nullptr);
39+
40+
CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr);
41+
}
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)