Skip to content

Commit a37913e

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Warmup: Time Conversion solved ✅. Triying to fix sonarlint detected issues.
1 parent eaa46ae commit a37913e

File tree

2 files changed

+5
-26
lines changed

2 files changed

+5
-26
lines changed

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

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,6 @@
88
#include <stdlib.h>
99
#include <string.h>
1010

11-
char *HACKERRANK_WARMUP_lastN(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 + len - n, n);
26-
27-
result[n] = '\0';
28-
29-
return result;
30-
}
31-
3211
char *HACKERRANK_WARMUP_getFirst(const char *s, unsigned long n) {
3312
return HACKERRANK_WARMUP_getStringFragment(s, strlen(s), 0, n);
3413
}
@@ -61,8 +40,8 @@ char *HACKERRANK_WARMUP_getStringFragment(const char *s, unsigned long len,
6140
}
6241

6342
char *HACKERRANK_WARMUP_timeConversion(const char *s) {
64-
char *meridian = HACKERRANK_WARMUP_lastN(s, 2);
65-
// char *meridian = HACKERRANK_WARMUP_getLast(s, 2);
43+
// char *meridian = HACKERRANK_WARMUP_lastN(s, 2);
44+
char *meridian = HACKERRANK_WARMUP_getLast(s, 2);
6645

6746
// char *hour_str = HACKERRANK_WARMUP_firstN(s, 2);
6847
char *hour_str = HACKERRANK_WARMUP_getFirst(s, 2);
@@ -76,7 +55,7 @@ char *HACKERRANK_WARMUP_timeConversion(const char *s) {
7655
strcpy(time_str, s);
7756
char *temp_time_str;
7857
unsigned long time_str_len = strlen(time_str);
79-
temp_time_str = HACKERRANK_WARMUP_lastN(time_str, time_str_len - 2);
58+
temp_time_str = HACKERRANK_WARMUP_getLast(time_str, time_str_len - 2);
8059
free(time_str);
8160
time_str = temp_time_str;
8261
temp_time_str = HACKERRANK_WARMUP_getFirst(time_str, time_str_len - 2);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ TEST_CASE("time_conversion helper functions edge cases",
3737
// CHECK(HACKERRANK_WARMUP_getFirst(nullptr, 0) == nullptr);
3838

3939
CHECK(HACKERRANK_WARMUP_getFirst("", 10) == nullptr);
40-
CHECK(HACKERRANK_WARMUP_lastN("", 10) == nullptr);
40+
CHECK(HACKERRANK_WARMUP_getLast("", 10) == nullptr);
4141

4242
CHECK(HACKERRANK_WARMUP_getFirst("", 0) == nullptr);
43-
CHECK(HACKERRANK_WARMUP_lastN("", 0) == nullptr);
43+
CHECK(HACKERRANK_WARMUP_getLast("", 0) == nullptr);
4444
}

0 commit comments

Comments
 (0)