diff --git a/docs/hackerrank/warmup/staircase.md b/docs/hackerrank/warmup/staircase.md new file mode 100644 index 0000000..201bcc1 --- /dev/null +++ b/docs/hackerrank/warmup/staircase.md @@ -0,0 +1,67 @@ +# [Staircase](https://www.hackerrank.com/challenges/staircase) + +Difficulty: #easy +Category: #warmup + +Staircase detail +This is a staircase of size $ n = 4 $: + +```text + # + ## + ### +#### +``` + +Its base and height are both equal to n. It is drawn using # symbols +and spaces. The last line is not preceded by any spaces. + +Write a program that prints a staircase of size n. + +## Function Description + +Complete the staircase function in the editor below. + +staircase has the following parameter(s): + +* int n: an integer + +## Print + +Print a staircase as described above. + +## Input Format + +A single integer, , denoting the size of the staircase. + +Constraints + +$ 0 < n \leq 100 $ + +## Output Format + +Print a staircase of size n using # symbols and spaces. + +Note: The last line must have spaces in it. + +## Sample Input + +```text +6 +``` + +## Sample Output + +```text + # + ## + ### + #### + ##### +###### +``` + +## Explanation + +The staircase is right-aligned, composed of # symbols and spaces, +and has a height and width of $ n = 6 $. diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/staircase.h b/src/lib/exercises/include/exercises/hackerrank/warmup/staircase.h new file mode 100644 index 0000000..9042286 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/staircase.h @@ -0,0 +1,13 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +char **HACKERRANK_WARMUP_staircaseCalculate(int n); +void HACKERRANK_WARMUP_staircase(int n); +void HACKERRANK_WARMUP_freeStaircase(char **staircase, int n); + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/lib/exercises/src/hackerrank/warmup/staircase.c b/src/lib/exercises/src/hackerrank/warmup/staircase.c new file mode 100644 index 0000000..4473eec --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/staircase.c @@ -0,0 +1,47 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/staircase.md]] + */ + +#include // malloc +#include // snprintf + +char **HACKERRANK_WARMUP_staircaseCalculate(int n) { + + char **answer = malloc(n * sizeof(char *)); // Array of char pointers + + for (int i = 0; i < n; i++) { + char *line = malloc((n + 1) * sizeof(char)); // Array of char values + + for (int j = 0; j < n; j++) { + if (j < n - i - 1) { + line[j] = ' '; + } else { + line[j] = '#'; + } + } + line[n] = '\0'; + + answer[i] = line; + } + + return answer; +} + +void HACKERRANK_WARMUP_freeStaircase(char **staircase, int n) { + for (int i = 0; i < n; i++) { + free(staircase[i]); + } + free(staircase); +} + +void HACKERRANK_WARMUP_staircase(int n) { + char **output = HACKERRANK_WARMUP_staircaseCalculate(n); + + for (int i = 0; i < n; i++) { + printf("%s\n", output[i]); + } + + HACKERRANK_WARMUP_freeStaircase(output, n); +} diff --git a/src/tests/unit/lib/hackerrank/warmup/staircase.test.cpp b/src/tests/unit/lib/hackerrank/warmup/staircase.test.cpp new file mode 100644 index 0000000..a08f557 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/staircase.test.cpp @@ -0,0 +1,39 @@ +#include + +#include +#include +#include +#include +#include + +using json = nlohmann::json; + +TEST_CASE("staircase", "[warmup]") { + std::filesystem::path cwd = std::filesystem::current_path(); + std::string path = + cwd.string() + "/unit/lib/hackerrank/warmup/staircase.testcases.json"; + + INFO("staircase JSON test cases FILE: " << path); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + auto input = static_cast(testcase["input"]); + + char **result = HACKERRANK_WARMUP_staircaseCalculate(input); + + std::vector result_as_vector; + + for (int i = 0; i < input; i++) { + result_as_vector.emplace_back(result[i]); + } + + HACKERRANK_WARMUP_freeStaircase(result, input); + + CHECK(result_as_vector == testcase["expected"]); + + // Just call void function, to collect coverage + HACKERRANK_WARMUP_staircase(input); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/staircase.testcases.json b/src/tests/unit/lib/hackerrank/warmup/staircase.testcases.json new file mode 100644 index 0000000..575d432 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/staircase.testcases.json @@ -0,0 +1,6 @@ +[ + { + "input": 6, + "expected": [" #", " ##", " ###", " ####", " #####", "######"] + } +]