From 3c0892998c87ee41d18f1a93d98171bb90d39a81 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 29 Sep 2025 11:41:38 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Recursion:=20Davis'=20Staircase.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/hackerrank/warmup/staircase.md | 67 +++++++++++++++++++ src/hackerrank/warmup/mod.rs | 1 + src/hackerrank/warmup/staircase.rs | 18 +++++ .../warmup/staircase.testcases.json | 7 ++ tests/hackerrank/warmup/mod.rs | 1 + tests/hackerrank/warmup/staircase.rs | 31 +++++++++ 6 files changed, 125 insertions(+) create mode 100644 docs/hackerrank/warmup/staircase.md create mode 100644 src/hackerrank/warmup/staircase.rs create mode 100644 tests/data/hackerrank/warmup/staircase.testcases.json create mode 100644 tests/hackerrank/warmup/staircase.rs diff --git a/docs/hackerrank/warmup/staircase.md b/docs/hackerrank/warmup/staircase.md new file mode 100644 index 0000000..201bcc1 --- /dev/null +++ b/docs/hackerrank/warmup/staircase.md @@ -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 $. diff --git a/src/hackerrank/warmup/mod.rs b/src/hackerrank/warmup/mod.rs index b20dcc1..1c43123 100644 --- a/src/hackerrank/warmup/mod.rs +++ b/src/hackerrank/warmup/mod.rs @@ -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; diff --git a/src/hackerrank/warmup/staircase.rs b/src/hackerrank/warmup/staircase.rs new file mode 100644 index 0000000..7d09dd9 --- /dev/null +++ b/src/hackerrank/warmup/staircase.rs @@ -0,0 +1,18 @@ +// @link Problem definition [[docs/hackerrank/warmup/staircase.md]] + +pub fn staircase_string(n: i32) -> String { + let mut result: Vec = 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)); +} diff --git a/tests/data/hackerrank/warmup/staircase.testcases.json b/tests/data/hackerrank/warmup/staircase.testcases.json new file mode 100644 index 0000000..cf81f08 --- /dev/null +++ b/tests/data/hackerrank/warmup/staircase.testcases.json @@ -0,0 +1,7 @@ +[ + { + "title": "Example", + "input": 6, + "expected": " #\n ##\n ###\n ####\n #####\n######" + } +] diff --git a/tests/hackerrank/warmup/mod.rs b/tests/hackerrank/warmup/mod.rs index b20dcc1..1c43123 100644 --- a/tests/hackerrank/warmup/mod.rs +++ b/tests/hackerrank/warmup/mod.rs @@ -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; diff --git a/tests/hackerrank/warmup/staircase.rs b/tests/hackerrank/warmup/staircase.rs new file mode 100644 index 0000000..0e6206d --- /dev/null +++ b/tests/hackerrank/warmup/staircase.rs @@ -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> = + 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); + } + } +}