Skip to content

Instantly share code, notes, and snippets.

@rondale-sc
Created August 24, 2011 04:12
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 rondale-sc/1167292 to your computer and use it in GitHub Desktop.
Save rondale-sc/1167292 to your computer and use it in GitHub Desktop.
Ninety Nine Bottles of Beer
require 'pp'
@unique_number_words = {
1 => "one", 2 => "two", 3 => "three",
4 => "four", 5 => "five", 6 => "six",
7 => "seven", 8 => "eight", 9 => "nine",
10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen"
}
@compound_number_words = {
10 => "teen", 20 => "twenty", 30 => "thirty", 40 => "forty",
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", 90 => "ninety",
}
@lyrics = {
:line_one => " bottles of beer on the wall, ",
:line_two => " bottles of beer,\n",
:line_three => "take one down pass it around, ",
:line_four => " bottles of beer on the wall.\n\n"
}
def get_written_number(index)
case
when index == 0
"zero"
when index <= 13
return @unique_number_words[index]
when index > 13 && index < 20
return "#{@unique_number_words[index % 10]} #{@compound_number_words[(index / 10) * 10]}"
when index > 13
return "#{@compound_number_words[(index / 10) * 10]} #{@unique_number_words[index % 10]}"
end
end
def print_lyrics(index)
string = ""
string += get_written_number(index) + @lyrics[:line_one]
string += get_written_number(index) + @lyrics[:line_two]
string += @lyrics[:line_three]
string += get_written_number(index - 1) + @lyrics[:line_four]
end
99.times do |index|
reverse_index = 99 - index
puts print_lyrics(reverse_index)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment