As I've been practicing code challenges in JavaScript recently, I couldn't help but feel that I had neglected my old friend Ruby. As I result, I decided to start going back to some of the same problems I had already solved, and come up with a few Ruby solutions. I'll start with the Staircase Challenge.
Check out my old post if you're more interested in the JavaScript version.
Similar to my previous post, I'm solving the HackerRank Staircase problem. In this problem, we are given a number of levels, n, and are asked to print out a staircase with n levels ascending from left to right. The output should look like this:
staircase(4)
#
##
###
####
String Repeat Strategy
For my first JavaScript solution, I had used the repeat method to generate the correct number of spaces and pound symbols for each row. In Ruby, I found that I could use *
to repeat a specific character as many times as I needed.
Set up a counter, starting at n-1
i = n-1
Set up a loop, decrementing from i down to zero (once for each row of the staircase)
while i >= 0
Use the i and n number variables to repeat spaces and pound characters for each row
spaces = " " * i
pounds = "#" * (n-i)
row = spaces + pounds
Print out the row before decrementing the counter for the next row
puts row
i -= 1
Taken altogether, the repeat strategy solution looks like this:
def staircase(n)
i = n-1
while i >= 0
spaces = " " * i
pounds = "#" * (n-i)
row = spaces + pounds
puts row
i -= 1
end
end
Iteration Strategy
In JavaScript, I also used an iteration strategy to achieve the same result. To see the logic behind the strategy, look back at the original post. In Ruby, the process looks like this:
Iterate through rows
row = 0
while row < n
Set up empty string for the given row
string = ""
Iterate through columns
column = 0
while column < n
If the column number is less than or equal to the row
if column <= row
Add "#"" to the end of the string
string = string + "#"
else
Add an empty space to the beginning of the string
string = " " + string
Increment the column
column += 1
Print the string for the row, then increment the row
puts string
row += 1
The final iterative solution, without comments:
def staircase(n)
row = 0
while row < n
string = ""
column = 0
while column < n
if column <= row
string = string + "#"
else
string = " " + string
end
column += 1
end
puts string
row += 1
end
end
Recursion Strategy
Finally, we can attempt a recursion strategy in Ruby. To start, we can use the same pseudo-code that we used in JavaScript to help us build the solution.
Set default values for arguments that will be needed in each function call (row number, and string)
def staircase(n, row=0, string="")
Set up a base case so that the recursive function calls will stop (when n reaches zero)
if n == row
return
end
Set up a conditional to account for the cases when the string length for a row is full (n===string.length)
if n == string.length
Print out the row (string)
puts string
Call the function to start work on the next row (row + 1)
return staircase(n, row+1)
Account for adding characters to the string if the row is not yet full (n < string.length), based on comparing the string length with the row number
add = string.length <= row ? "#" : " "
Call the function to continue working on the row, with the current row and updated string passed as arguments
staircase(n, row, add + string)
In code, this will result in the following recursive solution:
def staircase(n, row=0, string="")
if n == row
return
end
if n == string.length
puts string
return staircase(n, row+1)
end
add = string.length <= row ? "#" : " "
staircase(n, row, add + string)
end
The Pyramid Challenge
I'll end this post the same way as my JavaScript post: With a challenge.
Write a function that will take in one parameter, the height of a pyramid, and will print out the shape of a pyramid with that many levels.
For example, pyramid(4) would result in the output:
x
xxx
xxxxx
xxxxxxx
How can you use the strategies above to solve the pyramid challenge? Did you use a repeating, iteration, or recursive solution?
Top comments (0)