Michael Fellinger wrote:
Also, would there be an easy way to print the words out instead of the
numbers of bottles on the wall?
Divide the number by 10 and it will give the prefix (ninety-, eighty-,
etc.) You need a list or hash of some sort, of course. Simple
example...
prefixes = {9 => "ninety-", 8 => "eighty-"}
...or even better...
prefixes = ["", "", "twenty-", "thirty-", "forty-"]
...and so on. And then modify the numbers ten through nineteen after
the transformation (you don't want "one" for "eleven" do you) since
they differ in nomenclature from the others.
You could automagically use suffixes instead of prefixes if the
number, when divided by 10 is 1, but that wouldn't help you with the
edge cases of 'ten', 'eleven', and 'twelve'.
http://p.ramaze.net/1900
my stab at it
^ manveru
I did something slightly different, which works on multiple levels, not just two. For example, it can go all the way up to 999 without any extra code.
#!/usr/bin/env ruby
class Fixnum
ENGLISH = {
0 => 'zero',
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',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'ninteen',
20 => 'twenty',
30 => 'thirty',
40 => 'forty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'one hundred and',
200 => 'two hundred and',
300 => 'three hundred and',
400 => 'four hundred and',
500 => 'five hundred and',
600 => 'six hundred and',
700 => 'seven hundred and',
800 => 'eight hundred and',
900 => 'nine hundred and'
}
def to_english
i = ENGLISH.keys.select{|n| n <= self}.max
ENGLISH[i] + (i < self ? " " + (self-i).to_english : '')
end
end
99.downto(1) do|i|
puts "#{i.to_english} bottles of beer on the wall"
end
···
On Wed, Aug 27, 2008 at 9:57 AM, Todd Benson <caduceass@gmail.com> wrote:
On Tue, Aug 26, 2008 at 6:30 PM, danielj <sleepingindian@gmail.com> wrote:
--
Michael Morin
Guide to Ruby
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company