File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed
src/pkg/hackerrank/warmup Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
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 $.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments