Skip to content

[Hacker Rank]: Warmup. Staircase solved ✓ #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/hackerrank/warmup/staircase.md
Original file line number Diff line number Diff line change
@@ -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 $.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <string>
#include <vector>

namespace hackerrank::warmup {
std::vector<std::string> staircaseCalculate(int n);
void staircase(int n);
} // namespace hackerrank::warmup
34 changes: 34 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/staircase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <exercises/hackerrank/warmup/staircase.hpp>

#include <iostream>
#include <string>
#include <vector>

namespace hackerrank::warmup {
std::vector<std::string> staircaseCalculate(int n) {
std::vector<std::string> 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<std::string> output = staircaseCalculate(n);

for (const std::string &line : output) {
std::cout << line << std::endl;
}
}
} // namespace hackerrank::warmup
28 changes: 28 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/staircase.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/staircase.hpp>
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include <vector>

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<std::string> result =
hackerrank::warmup::staircaseCalculate(testcase["input"]);
CHECK(result == testcase["expected"]);

hackerrank::warmup::staircase(testcase["input"]);
}
}
6 changes: 6 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/staircase.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": 6,
"expected": [" #", " ##", " ###", " ####", " #####", "######"]
}
]