Skip to content

[Hacker Rank]: Warmup: Staircase solved ✓ #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
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
Original file line number Diff line number Diff line change
@@ -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);
}
}

38 changes: 38 additions & 0 deletions algorithm-exercises-csharp/src/hackerrank/warmup/Staircase.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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);
}

}
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 $.
Loading