Skip to content
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 $.
13 changes: 13 additions & 0 deletions src/lib/exercises/include/exercises/hackerrank/warmup/staircase.h
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/staircase.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <exercises/hackerrank/warmup/staircase.h>

/**
* @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
*/

#include <stdio.h> // malloc
#include <stdlib.h> // 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);
}
39 changes: 39 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,39 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/staircase.h>
#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) {
auto input = static_cast<int>(testcase["input"]);

char **result = HACKERRANK_WARMUP_staircaseCalculate(input);

std::vector<std::string> 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);
}
}
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": [" #", " ##", " ###", " ####", " #####", "######"]
}
]