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 $.
1 change: 1 addition & 0 deletions src/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod compare_triplets;
pub mod a_very_big_sum;
pub mod diagonal_difference;
pub mod plus_minus;
pub mod staircase;
18 changes: 18 additions & 0 deletions src/hackerrank/warmup/staircase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @link Problem definition [[docs/hackerrank/warmup/staircase.md]]

pub fn staircase_string(n: i32) -> String {
let mut result: Vec<String> = Vec::new();

for i in 1..=n {
let spaces = " ".repeat((n - i) as usize);
let hashes = "#".repeat(i as usize);

result.push(format!("{}{}", spaces, hashes));
}

result.join("\n")
}

pub fn staircase(n: i32) {
println!("{}", staircase_string(n));
}
7 changes: 7 additions & 0 deletions tests/data/hackerrank/warmup/staircase.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"title": "Example",
"input": 6,
"expected": " #\n ##\n ###\n ####\n #####\n######"
}
]
1 change: 1 addition & 0 deletions tests/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod compare_triplets;
pub mod a_very_big_sum;
pub mod diagonal_difference;
pub mod plus_minus;
pub mod staircase;
31 changes: 31 additions & 0 deletions tests/hackerrank/warmup/staircase.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use exercises::hackerrank::warmup::staircase::{staircase, staircase_string};
use once_cell::sync::Lazy;
use serde::Deserialize;

use crate::common;
use common::utils::load_json;

#[cfg(test)]
mod tests {
use super::*;

#[derive(Debug, Deserialize)]
struct AveryBigSumTestCase {
input: i32,
expected: String
}

static TEST_DATA: Lazy<Vec<AveryBigSumTestCase>> =
Lazy::new(|| load_json("tests/data/hackerrank/warmup/staircase.testcases.json"));

#[test]
fn test_staircase() {
println!("Testing hackerrank::warmup::staircase::staircase()");

for test_case in TEST_DATA.iter() {
let result = staircase_string(test_case.input);
staircase(test_case.input);
assert_eq!(result, test_case.expected);
}
}
}
Loading