Skip to content

Instantly share code, notes, and snippets.

@denpatin
Created March 13, 2016 17:02
Show Gist options
  • Save denpatin/b62066f867f9ec40c0b1 to your computer and use it in GitHub Desktop.
Save denpatin/b62066f867f9ec40c0b1 to your computer and use it in GitHub Desktop.
Ruby implementation of a pyramid with a custom height
#
# 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