Skip to content

Commit 1d303c8

Browse files
authored
Merge pull request #36 from sir-gon/feature/staircase
[Hacker Rank] Interview Preparation Kit: Recursion: Davis' Staircase.…
2 parents a94a0c6 + 3c08929 commit 1d303c8

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed
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 $.

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod compare_triplets;
44
pub mod a_very_big_sum;
55
pub mod diagonal_difference;
66
pub mod plus_minus;
7+
pub mod staircase;

src/hackerrank/warmup/staircase.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
2+
3+
pub fn staircase_string(n: i32) -> String {
4+
let mut result: Vec<String> = Vec::new();
5+
6+
for i in 1..=n {
7+
let spaces = " ".repeat((n - i) as usize);
8+
let hashes = "#".repeat(i as usize);
9+
10+
result.push(format!("{}{}", spaces, hashes));
11+
}
12+
13+
result.join("\n")
14+
}
15+
16+
pub fn staircase(n: i32) {
17+
println!("{}", staircase_string(n));
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"title": "Example",
4+
"input": 6,
5+
"expected": " #\n ##\n ###\n ####\n #####\n######"
6+
}
7+
]

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod compare_triplets;
44
pub mod a_very_big_sum;
55
pub mod diagonal_difference;
66
pub mod plus_minus;
7+
pub mod staircase;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use exercises::hackerrank::warmup::staircase::{staircase, staircase_string};
2+
use once_cell::sync::Lazy;
3+
use serde::Deserialize;
4+
5+
use crate::common;
6+
use common::utils::load_json;
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[derive(Debug, Deserialize)]
13+
struct AveryBigSumTestCase {
14+
input: i32,
15+
expected: String
16+
}
17+
18+
static TEST_DATA: Lazy<Vec<AveryBigSumTestCase>> =
19+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/staircase.testcases.json"));
20+
21+
#[test]
22+
fn test_staircase() {
23+
println!("Testing hackerrank::warmup::staircase::staircase()");
24+
25+
for test_case in TEST_DATA.iter() {
26+
let result = staircase_string(test_case.input);
27+
staircase(test_case.input);
28+
assert_eq!(result, test_case.expected);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)