Skip to content

Instantly share code, notes, and snippets.

@ryanveroniwooff
Last active October 8, 2017 10:43
Show Gist options
  • Save ryanveroniwooff/58187ba02fd40abd821f173e021a1145 to your computer and use it in GitHub Desktop.
Save ryanveroniwooff/58187ba02fd40abd821f173e021a1145 to your computer and use it in GitHub Desktop.
Creates a perfect pyramid of *'s in terminal. make a call to the method with your specified height
def make_stars(height)
star = '*'
space = ' '
arr = []
# Sets up non-formatted stars for given height
for i in 1..height
arr << [i == 1 ? star : star * ((i * 2) - 2)]
end
# Adds spaces in between each star
arr = arr.map { |e| e.join().split('').join(' ') }
length = arr.last.length
# Adds spaces before and after each set of stars to center them above the last set of stars, returns array
arr.each_with_index.map { |e,i| (space * ((length - arr[i].size) / 2)) + e + (space * ((length - arr[i].size) / 2)) }
end
# Example call to method make_stars with a height of 5
puts make_stars(5)
# Output looks like:
# *
# * *
# * * * *
# * * * * * *
#* * * * * * * *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment