Skip to content

[Hacker Rank] Warm up: Staircase solved ✓ #168

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 26, 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,39 @@
package ae.hackerrank.warmup;

import java.util.ArrayList;
import java.util.List;

/**
* Staricase.
*
* @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
*/
public class Staircase {

private Staircase() {
}

static java.util.logging.Logger logger = util.CustomLogger.getLogger();

/**
* staircase.
*/
public static String staircase(int n) {
List<String> result = new ArrayList<>();

for (int i = 1; i < n + 1; i++) {
StringBuilder line = new StringBuilder();

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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ae.hackerrank.warmup;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
class StaircaseTest {
public class StaircaseTestCase {
public Integer input;
public String expected;

public StaircaseTestCase(Integer _input, String _expected) {
this.input = _input;
this.expected = _expected;
}
}

public List<StaircaseTestCase> testCases;

@BeforeAll
public void setup() {
this.testCases = Arrays.asList(
new StaircaseTestCase(
6, String.join("\n",
" #",
" ##",
" ###",
" ####",
" #####",
"######")));
}

@Test
void testStaircase() {
for (StaircaseTestCase testCase : this.testCases) {
String solutionFound = Staircase.staircase(testCase.input);

assertEquals(testCase.expected, solutionFound,
String.format("%s(%d) answer must be: %s",
"Staircase.staircase",
testCase.input,
testCase.expected));
}
}
}
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