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.hpp b/src/lib/exercises/include/exercises/hackerrank/warmup/staircase.hpp new file mode 100644 index 0000000..6083fd2 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/staircase.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +namespace hackerrank::warmup { +std::vector staircaseCalculate(int n); +void staircase(int n); +} // namespace hackerrank::warmup diff --git a/src/lib/exercises/src/hackerrank/warmup/staircase.cpp b/src/lib/exercises/src/hackerrank/warmup/staircase.cpp new file mode 100644 index 0000000..e6ee05a --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/staircase.cpp @@ -0,0 +1,34 @@ +#include + +#include +#include +#include + +namespace hackerrank::warmup { +std::vector staircaseCalculate(int n) { + std::vector answer; + + for (int i = 0; i < n; i++) { + std::string line = ""; + + for (int j = 0; j < n; j++) { + if (j < n - i - 1) { + line += " "; + } else { + line += "#"; + } + } + answer.emplace_back(line); + } + + return answer; +} + +void staircase(int n) { + std::vector output = staircaseCalculate(n); + + for (const std::string &line : output) { + std::cout << line << std::endl; + } +} +} // namespace hackerrank::warmup 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..f2e6383 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/staircase.test.cpp @@ -0,0 +1,28 @@ +#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) { + std::vector result = + hackerrank::warmup::staircaseCalculate(testcase["input"]); + CHECK(result == testcase["expected"]); + + hackerrank::warmup::staircase(testcase["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": [" #", " ##", " ###", " ####", " #####", "######"] + } +]