# STAIRCASE

By: Saurav

2017-12-29 20:48:00 UTC

Consider a staircase of size 4:

#
##
###
####

Observe that its base and height are both equal to 4, and the image 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.

Input Format

A single integer,n, denoting the size of the staircase.

Output Format

Print a staircase size n using # symbols and spaces.

Note: The last line must have 0 spaces in it.

Sample Input

6
Sample Output

#
##
###
####
#####
######
Explanation

Although above its being shown left aligned, the staircase is right-aligned, composed of # symbols and spaces, and has a height and width of 6.

Hackerrank

This an easy but interesting problem where number of spaces is decreasing and number of # are increasing.
Going through our steps to solve any algo problems, we see clearly, there is a mathematical pattern between these two where the sum is always equal to n but space number is decreasing and # number is increasing.

```n = gets.to_i

def build_the_tree(n)
return "" if n < 1
j = 1
(0..n-1).each do |i|
puts get_spaces(n-j) + get_hashes(j)
j+=1
end
return
end

def get_spaces(n)
str = ""
(0..n-1).each do |i|
str += " "
end
return str
end

def get_hashes(n)
str = ""
(0..n-1).each do |i|
str += "#"
end
return str
end

build_the_tree(n)```

lets clean it a little and remove repetitions.

```n = gets.to_i

def build_the_tree(n, j = 1)
return "" if n < 1
(0..n-1).each do |i|
puts get_pattern(n-j, " ") + get_pattern(j, "#")
j+=1
end
end

def get_pattern(n, pattern, str = "")
(0..n-1).each do |i|
str += pattern
end
return str
end

build_the_tree(n)```

Looks good to me!

### Let me know what you think! Twitter: sprakash24octLinkedin

Owned & Maintained by Saurav Prakash

If you like what you see, you can help me cover server costs or buy me a cup of coffee though donation :)