Last active
October 8, 2017 10:43
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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