From 24a9c53222191d82de5005fd046457d27c4e741a Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 21 May 2024 17:24:37 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]:=20Warmup:=20Staircase=20solved?= =?UTF-8?q?=20=E2=9C=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/hackerrank/warmup/Staircase.Test.cs | 24 +++++++ .../src/hackerrank/warmup/Staircase.cs | 38 +++++++++++ docs/hackerrank/warmup/staircase.md | 67 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/hackerrank/warmup/Staircase.Test.cs create mode 100644 algorithm-exercises-csharp/src/hackerrank/warmup/Staircase.cs create mode 100644 docs/hackerrank/warmup/staircase.md diff --git a/algorithm-exercises-csharp-test/src/hackerrank/warmup/Staircase.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/warmup/Staircase.Test.cs new file mode 100644 index 0000000..8f57bbf --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/warmup/Staircase.Test.cs @@ -0,0 +1,24 @@ +namespace algorithm_exercises_csharp.hackerrank; + +[TestClass] +public class StaircaseTest +{ + [TestMethod] + public void testStaircase() + { + int input = 6; + string expectedAnswer = String.Join("\n", + " #", + " ##", + " ###", + " ####", + " #####", + "######" + ); + + string result = Staircase.staircase(input); + + Assert.AreEqual(expectedAnswer, result); + } +} + diff --git a/algorithm-exercises-csharp/src/hackerrank/warmup/Staircase.cs b/algorithm-exercises-csharp/src/hackerrank/warmup/Staircase.cs new file mode 100644 index 0000000..10e7f0c --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/warmup/Staircase.cs @@ -0,0 +1,38 @@ +// @link Problem definition [[docs/hackerrank/warmup/staircase.md]] + +namespace algorithm_exercises_csharp.hackerrank; + +using System.Text; +using System.Diagnostics.CodeAnalysis; + +public class Staircase +{ + [ExcludeFromCodeCoverage] + protected Staircase() { } + + public static string staircase(int _n) + { + List result = []; + + for (int i = 1; i < _n + 1; i++) + { + StringBuilder line = new(); + + for (int j = 1; j < _n + 1; j++) + { + if (j <= _n - i) + { + line.Append(' '); + } + else + { + line.Append('#'); + } + } + + result.Add(line.ToString()); + } + return String.Join("\n", result); + } + +} 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 $.