I want to solve this challenge in hackerrank in GO. When I run it I get the same result as the challenge wants but they don't accept my answer. Here is the challenge link: https://www.hackerrank.com/challenges/staircase/problem?isFullScreen=true
Here is my code:
func staircase(n int32) {
var i int32
for i = 0; i < n; i {
fmt.Println(strings.Repeat(" ", int(n-i)), strings.Repeat("#", int(i)))
}
}
CodePudding user response:
First, the first line must have one #
sign, and the last must have n
#
signs. So change the loop to go from 1
to n
inclusive.
Next, fmt.Println()
prints a space between arguments, which will "distort" the output. Either concatenate the 2 strings, or use fmt.Print()
which does not add spaces between string arguments, or use fmt.Printf("%s%s\n", text1, text2)
.
For example:
func staircase(n int32) {
for i := int32(1); i <= n; i {
fmt.Println(strings.Repeat(" ", int(n-i)) strings.Repeat("#", int(i)))
}
}
Testing it with staircase(4)
, output will be (try it on the Go Playground):
#
##
###
####