Skip to content

Commit cf10f15

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup. Staircase solved ✅.
1 parent e1daaf6 commit cf10f15

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66

77
char **HACKERRANK_WARMUP_staircaseCalculate(int n);
88
void HACKERRANK_WARMUP_staircase(int n);
9+
void HACKERRANK_WARMUP_freeStaircase(char **staircase, int n);
910

1011
#ifdef __cplusplus
1112
} // extern "C"

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ char **HACKERRANK_WARMUP_staircaseCalculate(int n) {
2929
return answer;
3030
}
3131

32+
void HACKERRANK_WARMUP_freeStaircase(char **staircase, int n) {
33+
for (int i = 0; i < n; i++) {
34+
free(staircase[i]);
35+
}
36+
free(staircase);
37+
}
38+
3239
void HACKERRANK_WARMUP_staircase(int n) {
3340
char **output = HACKERRANK_WARMUP_staircaseCalculate(n);
3441

3542
for (int i = 0; i < n; i++) {
3643
printf("%s\n", output[i]);
37-
free(output[i]);
3844
}
3945

40-
free(output);
46+
HACKERRANK_WARMUP_freeStaircase(output, n);
4147
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ TEST_CASE("staircase", "[warmup]") {
2929
result_as_vector.emplace_back(result[i]);
3030
}
3131

32+
HACKERRANK_WARMUP_freeStaircase(result, input);
33+
3234
CHECK(result_as_vector == testcase["expected"]);
3335

3436
// Just call void function, to collect coverage

0 commit comments

Comments
 (0)