Skip to content

Commit 1c5eccc

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Warmup: Time Conversion solved ✅.
+ safer version + coverage increased.
1 parent 8e24400 commit 1c5eccc

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

src/lib/exercises/include/exercises/hackerrank/warmup/time_conversion.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#ifdef __cplusplus
44
extern "C" {
55
#endif
6+
7+
char *HACKERRANK_WARMUP_getStringFragment(const char *s, unsigned long len,
8+
unsigned long from, unsigned long to);
9+
char *HACKERRANK_WARMUP_getFirst(const char *s, unsigned long n);
10+
char *HACKERRANK_WARMUP_getLast(const char *s, unsigned long n);
11+
612
char *HACKERRANK_WARMUP_firstN(const char *s, unsigned long n);
713
char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n);
814

src/lib/exercises/src/hackerrank/warmup/time_conversion.c

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,74 @@
88
#include <stdlib.h>
99
#include <string.h>
1010

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;
11+
char *HACKERRANK_WARMUP_getFirst(const char *s, unsigned long n) {
12+
return HACKERRANK_WARMUP_getStringFragment(s, strlen(s), 0, n);
3013
}
3114

32-
char *HACKERRANK_WARMUP_lastN(const char *s, unsigned long n) {
15+
char *HACKERRANK_WARMUP_getLast(const char *s, unsigned long n) {
3316
unsigned long len = strlen(s);
34-
if (n > len) {
17+
return HACKERRANK_WARMUP_getStringFragment(s, len, len - n, n);
18+
}
19+
20+
char *HACKERRANK_WARMUP_getStringFragment(const char *s, unsigned long len,
21+
unsigned long from,
22+
unsigned long to) {
23+
if (to > len) {
3524
return NULL;
3625
}
37-
if (n == 0) {
26+
if (to == 0) {
3827
return NULL;
3928
}
4029

41-
char *result = (char *)malloc((n + 1) * sizeof(char));
30+
char *result = (char *)malloc((to + 1) * sizeof(char));
4231
if (result == NULL) {
4332
return NULL;
4433
}
4534

46-
strncpy(result, s + len - n, n);
35+
strncpy(result, s + from, to);
4736

48-
result[n] = '\0';
37+
result[to] = '\0';
4938

5039
return result;
5140
}
5241

5342
char *HACKERRANK_WARMUP_timeConversion(const char *s) {
54-
char *meridian = HACKERRANK_WARMUP_lastN(s, 2);
43+
if (s == NULL) {
44+
return NULL;
45+
}
46+
47+
char *meridian = HACKERRANK_WARMUP_getLast(s, 2);
48+
char *hour_str = HACKERRANK_WARMUP_getFirst(s, 2);
5549

56-
char *hour_str = HACKERRANK_WARMUP_firstN(s, 2);
57-
char *time_str = (char *)malloc((strlen(s) + 1) * sizeof(char));
50+
size_t s_len = strlen(s);
51+
char *time_str = malloc(s_len + 1);
5852
if (time_str == NULL) {
59-
free(hour_str);
6053
free(meridian);
54+
free(hour_str);
6155
return NULL;
6256
}
63-
strcpy(time_str, s);
64-
char *temp_time_str = HACKERRANK_WARMUP_lastN(time_str, strlen(time_str) - 2);
65-
free(time_str);
66-
time_str = temp_time_str;
67-
temp_time_str = HACKERRANK_WARMUP_firstN(time_str, strlen(time_str) - 2);
68-
free(time_str);
69-
time_str = temp_time_str;
57+
58+
strncpy(time_str, s, s_len + 1);
59+
char *temp_time_str;
60+
unsigned long time_str_len = strlen(time_str);
61+
temp_time_str = HACKERRANK_WARMUP_getLast(time_str, time_str_len - 2);
62+
strncpy(time_str, temp_time_str, time_str_len - 2);
63+
time_str[time_str_len - 2] = '\0';
64+
free(temp_time_str);
65+
temp_time_str = HACKERRANK_WARMUP_getFirst(time_str, time_str_len - 2);
66+
strncpy(time_str, temp_time_str, time_str_len - 2);
67+
time_str[time_str_len - 2] = '\0';
68+
free(temp_time_str);
7069

7170
char *endptr;
7271
long hour = strtol(hour_str, &endptr, 10);
7372

7473
if (*endptr != '\0') {
7574
printf("Conversion error, non-convertible part: %s\n", endptr);
7675

77-
free(hour_str);
7876
free(meridian);
77+
free(hour_str);
7978
free(time_str);
80-
free(endptr);
8179
return NULL;
8280
} else {
8381
printf("The integer value is: %ld\n", hour);
@@ -94,8 +92,8 @@ char *HACKERRANK_WARMUP_timeConversion(const char *s) {
9492
char *conversion = malloc(BUFFER_MAX_SIZE * sizeof(char));
9593

9694
snprintf(conversion, BUFFER_MAX_SIZE, "%02ld%s", hour, time_str);
97-
free(hour_str);
9895
free(meridian);
96+
free(hour_str);
9997
free(time_str);
10098

10199
return conversion;

src/tests/unit/lib/hackerrank/warmup/time_conversion.test.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,15 @@ TEST_CASE("time_conversion JSON Test Cases",
3434

3535
TEST_CASE("time_conversion helper functions edge cases",
3636
"[hackerrank] [helper] [warmup]") {
37-
CHECK(HACKERRANK_WARMUP_firstN("", 10) == nullptr);
38-
CHECK(HACKERRANK_WARMUP_lastN("", 10) == nullptr);
37+
// CHECK(HACKERRANK_WARMUP_getFirst(nullptr, 0) == nullptr);
3938

40-
CHECK(HACKERRANK_WARMUP_firstN("", 0) == nullptr);
41-
CHECK(HACKERRANK_WARMUP_lastN("", 0) == nullptr);
39+
CHECK(HACKERRANK_WARMUP_getFirst("", 10) == nullptr);
40+
CHECK(HACKERRANK_WARMUP_getLast("", 10) == nullptr);
41+
42+
CHECK(HACKERRANK_WARMUP_getFirst("", 0) == nullptr);
43+
CHECK(HACKERRANK_WARMUP_getLast("", 0) == nullptr);
44+
}
45+
46+
TEST_CASE("time_conversion edge cases", "[hackerrank] [helper] [warmup]") {
47+
CHECK(HACKERRANK_WARMUP_timeConversion("aa:bb:ccXM") == nullptr);
4248
}

0 commit comments

Comments
 (0)