Skip to content

Instantly share code, notes, and snippets.

@mehdi-farsi
Last active December 23, 2022 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdi-farsi/17ace70f05e1282a57a4 to your computer and use it in GitHub Desktop.
Save mehdi-farsi/17ace70f05e1282a57a4 to your computer and use it in GitHub Desktop.
Display a Fir Tree On Terminal
def usage
abort 'Wrong number of argument: ruby my_fir.rb FIR_SIZE'
end
def display_trunk(width)
trunk_width = trunk_height = ((width / 2) - 2).round
(1..trunk_height).each { puts ("|" * trunk_width).center(width) }
end
def display_level(width, line_length)
(1..3).each do |line|
puts ("*" * line_length).center(width)
line_length += 2 unless line == 3
end
line_length - 2
end
def length_of_last_line(fir_size)
5 + ((fir_size - 1) * 2) # I expect to get a scholarship from MIT with this formula..
end
def my_fir(fir_size)
width = length_of_last_line(fir_size)
first_line_of_level = 1
(1..fir_size).each do
first_line_of_level = display_level(width, first_line_of_level)
end
display_trunk(width)
end
# Sorry for this C/C++ coding style :)
def main
usage if ARGV.length != 1 || ARGV.first.to_i < 1
my_fir(ARGV.first.to_i)
end
main
$> ruby my_fir.rb 3
*
***
*****
***
*****
*******
*****
*******
*********
||
||
$>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment