Skip to content

Commit bf63b8a

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Staircase solved ✓
1 parent 361af15 commit bf63b8a

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

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 $.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/warmup/staircase.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
import (
8+
"strings"
9+
10+
utils "gon.cl/algorithm-exercises/src/utils"
11+
)
12+
13+
func Staircase(n int) string {
14+
15+
result := []string{}
16+
17+
for i := 1; i <= n; i++ {
18+
line := ""
19+
20+
for j := 1; j <= n; j++ {
21+
if j <= n-i {
22+
line = line + " "
23+
} else {
24+
line = line + "#"
25+
}
26+
}
27+
28+
result = append(result, line)
29+
}
30+
31+
utils.Info("Staircase answer => %v", result)
32+
33+
return strings.Join(result, "\n")
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestStaircase(t *testing.T) {
12+
13+
input := 6
14+
expectedSolution := strings.Join([]string{
15+
" #",
16+
" ##",
17+
" ###",
18+
" ####",
19+
" #####",
20+
"######",
21+
}, "\n")
22+
23+
testname := fmt.Sprintf("Staircase(%d) => %v \n", input, expectedSolution)
24+
t.Run(testname, func(t *testing.T) {
25+
26+
ans := Staircase(input)
27+
assert.Equal(t, expectedSolution, ans)
28+
})
29+
}

0 commit comments

Comments
 (0)