Skip to content

Commit 011c86d

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Warmup. Staircase solved ✅.
1 parent d52396a commit 011c86d

File tree

5 files changed

+163
-0
lines changed

5 files changed

+163
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
extern "C" {
5+
#endif
6+
7+
char **HACKERRANK_WARMUP_staircaseCalculate(int n);
8+
void HACKERRANK_WARMUP_staircase(int n);
9+
10+
#ifdef __cplusplus
11+
} // extern "C"
12+
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <exercises/hackerrank/warmup/staircase.h>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
5+
*/
6+
7+
#include <stdio.h> // malloc
8+
#include <stdlib.h> // snprintf
9+
10+
char **HACKERRANK_WARMUP_staircaseCalculate(int n) {
11+
12+
char **answer = malloc(n * sizeof(char *)); // Array of char pointers
13+
14+
for (int i = 0; i < n; i++) {
15+
char *line = malloc((n + 1) * sizeof(char)); // Array of char values
16+
17+
for (int j = 0; j < n; j++) {
18+
if (j < n - i - 1) {
19+
line[j] = ' ';
20+
} else {
21+
line[j] = '#';
22+
}
23+
}
24+
line[n] = '\0';
25+
26+
answer[i] = line;
27+
}
28+
29+
return answer;
30+
}
31+
32+
void HACKERRANK_WARMUP_staircase(int n) {
33+
char **output = HACKERRANK_WARMUP_staircaseCalculate(n);
34+
35+
for (int i = 0; i < n; i++) {
36+
printf("%s\n", output[i]);
37+
free(output[i]);
38+
}
39+
40+
free(output);
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/warmup/staircase.h>
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+
auto input = static_cast<int>(testcase["input"]);
23+
24+
char **result = HACKERRANK_WARMUP_staircaseCalculate(input);
25+
26+
std::vector<std::string> result_as_vector;
27+
28+
for (int i = 0; i < input; i++) {
29+
result_as_vector.emplace_back(result[i]);
30+
}
31+
32+
CHECK(result_as_vector == testcase["expected"]);
33+
34+
// Just call void function, to collect coverage
35+
HACKERRANK_WARMUP_staircase(input);
36+
}
37+
}
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)