Created
October 11, 2012 19:26
-
-
Save idlehands/3874895 to your computer and use it in GitHub Desktop.
num_words_pair
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
@answer_string = "" | |
ONE_TO_NINETEEN = {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 => 'nineteen' | |
} | |
TENS = {100 => ' hundred ', 90 => 'nintey', 80 => 'eighty', 70 => 'seventy', | |
60 => 'sixty', 50 => 'fifty', 40 => 'forty', 30 => 'thirty', 20 => 'twenty', 1000 => " thousand " | |
} | |
def one_to_99(tens) | |
answer_string = "" | |
puts tens.inspect | |
if tens < 20 | |
@answer_string << ONE_TO_NINETEEN[tens] | |
else | |
divisor = 100 | |
while tens % divisor == tens | |
divisor -= 10 | |
end | |
@answer_string << TENS[divisor] + " " | |
@answer_string << ONE_TO_NINETEEN[tens % divisor] if tens % divisor != 0 | |
end | |
0 | |
end | |
def pusher(times, number) | |
@answer_string << ONE_TO_NINETEEN[times] | |
@answer_string << TENS[number] | |
end | |
@depth = 0 | |
def words(num, divisor = 1_000_000) | |
@depth += 1 | |
puts @depth | |
return if num < 100 | |
divisor /= 10 while num % divisor == num | |
times = num / divisor | |
pusher(times, divisor) | |
puts @answer_string + "= current @answer_string mark" | |
num -= times * divisor | |
puts num.to_s + "= current num" | |
num = words(num, divisor) if divisor > 100 | |
# recursion ends | |
@depth -= 1 | |
puts @depth | |
puts "num = #{num}" | |
num = one_to_99(num) if num != 0 | |
num | |
# num == 0 ? @answer_string : num | |
end | |
puts words(2230) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment