Last active
November 12, 2015 21:10
-
-
Save kylekeesling/a0c60a2aa0aee28f9189 to your computer and use it in GitHub Desktop.
Outputs numbers as words with proper conjunctions - like you'd see on a wedding invite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def weddingNumber number | |
if number < 0 # No negative numbers. | |
return 'Please enter a number that isn\'t negative.' | |
end | |
if number == 0 | |
return 'zero' | |
end | |
# 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/1000 # How many thousands left to write out? | |
left = left - write*1000 # Subtract off those thousands. | |
if write > 0 | |
thousands = weddingNumber write | |
numString = numString + thousands + ' thousand' | |
if left > 0 | |
numString = numString + ' and ' | |
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 = weddingNumber 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". Remember that "write" is (at the moment) the number of | |
# hundreds we have to write out. After we add "hundreds" to | |
# "numString", we add the string ' hundred' after it. | |
# So, for example, if we originally called weddingNumber with | |
# 1999 (so "number" = 1999), then at this point "write" would | |
# be 19, and "left" would be 99. The laziest thing to do at this | |
# point is to have weddingNumber write out the 'nineteen' for us, | |
# then we write out ' hundred', and then the rest of | |
# weddingNumber writes out 'ninety-nine'. | |
if left > 0 | |
# So we don't write 'two hundredfifty-one'... | |
numString = numString + ' and ' | |
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 weddingNumber( 0) | |
puts weddingNumber( 9) | |
puts weddingNumber( 10) | |
puts weddingNumber( 11) | |
puts weddingNumber( 17) | |
puts weddingNumber( 32) | |
puts weddingNumber( 88) | |
puts weddingNumber( 99) | |
puts weddingNumber(100) | |
puts weddingNumber(101) | |
puts weddingNumber(234) | |
puts weddingNumber(1984) | |
puts weddingNumber(3211) | |
puts weddingNumber(999999) | |
puts weddingNumber(1000000000000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment