Skip to content

Commit f75f352

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Warmup: Time Conversion solved ✅
1 parent 2c007ca commit f75f352

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
char *HACKERRANK_WARMUP_firstN(const char *s, unsigned long n);
7+
char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n);
8+
9+
char *HACKERRANK_WARMUP_timeConversion(const char *s);
10+
11+
#ifdef __cplusplus
12+
} // extern "C"
13+
#endif
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
char *HACKERRANK_WARMUP_firstN(const char *s, unsigned long n) {
12+
unsigned long len = strlen(s);
13+
if (n > len) {
14+
return NULL;
15+
}
16+
if (n == 0) {
17+
return NULL;
18+
}
19+
20+
char *result = (char *)malloc((n + 1) * sizeof(char));
21+
if (result == NULL) {
22+
return NULL;
23+
}
24+
25+
strncpy(result, s, n);
26+
27+
result[n] = '\0';
28+
29+
return result;
30+
}
31+
32+
char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n) {
33+
unsigned long len = strlen(s);
34+
if (n > len) {
35+
return NULL;
36+
}
37+
if (n == 0) {
38+
return NULL;
39+
}
40+
41+
char *result = (char *)malloc((n + 1) * sizeof(char));
42+
if (result == NULL) {
43+
return NULL;
44+
}
45+
46+
strncpy(result, s + len - n, n);
47+
48+
result[n] = '\0';
49+
50+
return result;
51+
}
52+
53+
char *HACKERRANK_WARMUP_timeConversion(const char *s) {
54+
char *conversion;
55+
char *meridian = HACKERRANK_WARMUP_lastN(s, 2);
56+
57+
char *hour_str = HACKERRANK_WARMUP_firstN(s, 2);
58+
char *time_str = (char *)malloc((strlen(s) + 1) * sizeof(char));
59+
if (time_str == NULL) {
60+
free(hour_str);
61+
free(meridian);
62+
return NULL;
63+
}
64+
strcpy(time_str, s);
65+
time_str = HACKERRANK_WARMUP_lastN(time_str, strlen(time_str) - 2);
66+
time_str = HACKERRANK_WARMUP_firstN(time_str, strlen(time_str) - 2);
67+
68+
char *endptr;
69+
long hour = strtol(hour_str, &endptr, 10);
70+
71+
if (*endptr != '\0') {
72+
printf("Conversion error, non-convertible part: %s\n", endptr);
73+
74+
free(hour_str);
75+
free(meridian);
76+
free(time_str);
77+
free(endptr);
78+
return NULL;
79+
} else {
80+
printf("The integer value is: %ld\n", hour);
81+
}
82+
83+
hour = hour % 12;
84+
85+
if (strcmp(meridian, "PM") == 0) {
86+
hour += 12;
87+
}
88+
89+
const int BUFFER_MAX_SIZE = 9;
90+
91+
conversion = malloc(BUFFER_MAX_SIZE * sizeof(char));
92+
93+
snprintf(conversion, BUFFER_MAX_SIZE, "%02ld%s", hour, time_str);
94+
free(hour_str);
95+
free(meridian);
96+
free(time_str);
97+
98+
return conversion;
99+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
std::string result_str(HACKERRANK_WARMUP_timeConversion(
25+
testcase["input"].get<std::string>().c_str()));
26+
CHECK(result_str == testcase["expected"]);
27+
}
28+
}
29+
30+
TEST_CASE("time_conversion helper functions edge cases",
31+
"[hackerrank] [helper] [warmup]") {
32+
CHECK(HACKERRANK_WARMUP_firstN("", 10) == nullptr);
33+
CHECK(HACKERRANK_WARMUP_lastN("", 10) == nullptr);
34+
35+
CHECK(HACKERRANK_WARMUP_firstN("", 0) == nullptr);
36+
CHECK(HACKERRANK_WARMUP_lastN("", 0) == nullptr);
37+
}
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)