-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.5.2BottlesREBOOT.rb
163 lines (131 loc) · 4.98 KB
/
8.5.2BottlesREBOOT.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def englishNumber number
# No more special cases! No more returns!
numString = '' # This is the string we will return.
onesPlace = ['one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine']
tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen']
# "left" is how much of the number we still have left to write out.
# "write" is the part we are writing out right now.
# write and left... get it? :)
left = number
write = left/1000000000000 # how many thousands left to write out?
left = left - write*1000000000000 # subtract off those thousands
if write > 0
# Now here's a really sly trick:
trillions = englishNumber write
numString = numString + trillions + ' trillion'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number".
if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
write = left/1000000000 # how many thousands left to write out?
left = left - write*1000000000 # subtract off those thousands
if write > 0
# Now here's a really sly trick:
billions = englishNumber write
numString = numString + billions + ' billion'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number".
if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
write = left/1000000 # how many thousands left to write out?
left = left - write*1000000 # subtract off those thousands
if write > 0
# Now here's a really sly trick:
millions = englishNumber write
numString = numString + millions + ' million'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number".
if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
write = left/1000 # how many thousands left to write out?
left = left - write*1000 # subtract off those thousands
if write > 0
# Now here's a really sly trick:
thousands = englishNumber write
numString = numString + thousands + ' thousand'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number".
if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
write = left/100 # How many hundreds left to write out?
left = left - write*100 # Subtract off those hundreds.
if write > 0
# Now here's a really sly trick:
hundreds = englishNumber write
numString = numString + hundreds + ' hundred'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number".
if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
write = left/10 # How many tens left to write out?
left = left - write*10 # Subtract off those tens.
if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of "twelve",
# we have to make a special exception for these.
numString = numString + teenagers[left-1]
# The "-1" is because teenagers[3] is 'fourteen', not 'thirteen'.
# Since we took care of the digit in the ones place already,
# we have nothing left to write.
left = 0
else
numString = numString + tensPlace[write-1]
# The "-1" is because tensPlace[3] is 'forty', not 'thirty'.
end
if left > 0
# So we don't write 'sixtyfour'...
numString = numString + '-'
end
end
write = left # How many ones left to write out?
left = 0 # Subtract off those ones.
if write > 0
numString = numString + onesPlace[write-1]
# The "-1" is because onesPlace[3] is 'four', not 'three'.
end
# Now we just return "numString"...
numString
end
puts "How many bottles of beer do you have?"
bottles = gets.chomp.to_i
puts " "
while bottles >= 1 && bottles < 100
englishBottle = englishNumber(bottles)
puts "#{englishBottle} bottles of beer on the wall,"
puts "#{englishBottle} bottles of beer,"
puts "Take one down, pass it around,"
bottles -= 1
englishBottle = englishNumber(bottles)
puts "#{englishBottle} of beer on the wall!"
puts " "
end
if bottles <= 0
puts "You don't have any bottles left :("
end
if bottles >= 100
puts "Sorry, you have too many bottles to hang on the wall :("
end