Skip to content

Commit 51d241f

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup. Staircase solved ✓
1 parent 2d47dc1 commit 51d241f

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

docs/hackerrank/warmup/staircase.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [Staircase](https://www.hackerrank.com/challenges/staircase)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Staircase detail
7+
This is a staircase of size $ n = 4 $:
8+
9+
```text
10+
#
11+
##
12+
###
13+
####
14+
```
15+
16+
Its base and height are both equal to n. It is drawn using # symbols
17+
and spaces. The last line is not preceded by any spaces.
18+
19+
Write a program that prints a staircase of size n.
20+
21+
## Function Description
22+
23+
Complete the staircase function in the editor below.
24+
25+
staircase has the following parameter(s):
26+
27+
* int n: an integer
28+
29+
## Print
30+
31+
Print a staircase as described above.
32+
33+
## Input Format
34+
35+
A single integer, , denoting the size of the staircase.
36+
37+
Constraints
38+
39+
$ 0 < n \leq 100 $
40+
41+
## Output Format
42+
43+
Print a staircase of size n using # symbols and spaces.
44+
45+
Note: The last line must have spaces in it.
46+
47+
## Sample Input
48+
49+
```text
50+
6
51+
```
52+
53+
## Sample Output
54+
55+
```text
56+
#
57+
##
58+
###
59+
####
60+
#####
61+
######
62+
```
63+
64+
## Explanation
65+
66+
The staircase is right-aligned, composed of # symbols and spaces,
67+
and has a height and width of $ n = 6 $.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
namespace hackerrank::warmup {
7+
std::vector<std::string> staircaseCalculate(int n);
8+
void staircase(int n);
9+
} // namespace hackerrank::warmup
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <exercises/hackerrank/warmup/staircase.hpp>
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
7+
namespace hackerrank::warmup {
8+
std::vector<std::string> staircaseCalculate(int n) {
9+
std::vector<std::string> answer;
10+
11+
for (int i = 0; i < n; i++) {
12+
std::string line = "";
13+
14+
for (int j = 0; j < n; j++) {
15+
if (j < n - i - 1) {
16+
line += " ";
17+
} else {
18+
line += "#";
19+
}
20+
}
21+
answer.emplace_back(line);
22+
}
23+
24+
return answer;
25+
}
26+
27+
void staircase(int n) {
28+
std::vector<std::string> output = staircaseCalculate(n);
29+
30+
for (const std::string &line : output) {
31+
std::cout << line << std::endl;
32+
}
33+
}
34+
} // namespace hackerrank::warmup
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/staircase.hpp>
4+
#include <filesystem>
5+
#include <fstream>
6+
#include <nlohmann/json.hpp>
7+
#include <vector>
8+
9+
using json = nlohmann::json;
10+
11+
TEST_CASE("staircase", "[warmup]") {
12+
std::filesystem::path cwd = std::filesystem::current_path();
13+
std::string path =
14+
cwd.string() + "/unit/lib/hackerrank/warmup/staircase.testcases.json";
15+
16+
INFO("staircase JSON test cases FILE: " << path);
17+
18+
std::ifstream f(path);
19+
json data = json::parse(f);
20+
21+
for (auto testcase : data) {
22+
std::vector<std::string> result =
23+
hackerrank::warmup::staircaseCalculate(testcase["input"]);
24+
CHECK(result == testcase["expected"]);
25+
26+
hackerrank::warmup::staircase(testcase["input"]);
27+
}
28+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"input": 6,
4+
"expected": [" #", " ##", " ###", " ####", " #####", "######"]
5+
}
6+
]

0 commit comments

Comments
 (0)