Skip to content

Commit 30641c0

Browse files
authored
Merge pull request #168 from sir-gon/feature/staircase
[Hacker Rank] Warm up: Staircase solved ✓
2 parents 2e9b04d + dcf4f4d commit 30641c0

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package ae.hackerrank.warmup;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Staricase.
8+
*
9+
* @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
10+
*/
11+
public class Staircase {
12+
13+
private Staircase() {
14+
}
15+
16+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
17+
18+
/**
19+
* staircase.
20+
*/
21+
public static String staircase(int n) {
22+
List<String> result = new ArrayList<>();
23+
24+
for (int i = 1; i < n + 1; i++) {
25+
StringBuilder line = new StringBuilder();
26+
27+
for (int j = 1; j < n + 1; j++) {
28+
if (j <= n - i) {
29+
line.append(" ");
30+
} else {
31+
line.append("#");
32+
}
33+
}
34+
35+
result.add(line.toString());
36+
}
37+
return String.join("\n", result);
38+
}
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ae.hackerrank.warmup;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
12+
@TestInstance(Lifecycle.PER_CLASS)
13+
class StaircaseTest {
14+
public class StaircaseTestCase {
15+
public Integer input;
16+
public String expected;
17+
18+
public StaircaseTestCase(Integer _input, String _expected) {
19+
this.input = _input;
20+
this.expected = _expected;
21+
}
22+
}
23+
24+
public List<StaircaseTestCase> testCases;
25+
26+
@BeforeAll
27+
public void setup() {
28+
this.testCases = Arrays.asList(
29+
new StaircaseTestCase(
30+
6, String.join("\n",
31+
" #",
32+
" ##",
33+
" ###",
34+
" ####",
35+
" #####",
36+
"######")));
37+
}
38+
39+
@Test
40+
void testStaircase() {
41+
for (StaircaseTestCase testCase : this.testCases) {
42+
String solutionFound = Staircase.staircase(testCase.input);
43+
44+
assertEquals(testCase.expected, solutionFound,
45+
String.format("%s(%d) answer must be: %s",
46+
"Staircase.staircase",
47+
testCase.input,
48+
testCase.expected));
49+
}
50+
}
51+
}

docs/hackerrank/warmup/staircase.md

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

0 commit comments

Comments
 (0)