Created
March 13, 2016 17:02
-
-
Save denpatin/b62066f867f9ec40c0b1 to your computer and use it in GitHub Desktop.
Ruby implementation of a pyramid with a custom 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
# | |
# Input: Pyramid's height | |
# Output: Pyramid with given height and width = height * 2 - 1 | |
# | |
MAX_HEIGHT = 20 | |
prompt = 'Enter pyramid\'s height: ' | |
puts prompt | |
while height = gets.chomp | |
case height | |
when /^\d+$/ | |
height = height.to_i | |
if height <= MAX_HEIGHT | |
height.times do |row| | |
level = ''.center 2 * (height - row) - 1, '*' | |
puts level.center level.size + 2 * row | |
end | |
break | |
else | |
puts "Too big value! #{prompt}" | |
end | |
else | |
puts "Not a natural number! #{prompt}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment