Skip to content

Instantly share code, notes, and snippets.

@konamax123 konamax123/num_words.rb
Last active Aug 29, 2015

Embed
What would you like to do?
def num_conversion(num)
singles = {0 => "zero", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six",
7 => "seven", 8 => "eight", 9 => "nine" }
teens = {10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen",
14 => "fourteen", 15 => "fifteen", 16 => "sixteen",
17 => "seventeen", 18 => "eighteen", 19 => "nineteen"}
tens = { 2 => "twenty", 3 => "thirty", 4 => "fourty", 5 => "fifty",
6 => "sixty", 7 => "seventy", 8 => "eighty", 9 => "ninety"}
#1. determine length of number (plug into array)
#2. If it's just 1, then do the match for singles
#3. If it's two:
# a. if the first number == 1, if so then do the teens logic
# b. if it isn't, then do the tens logic.
#4. If it's three:
# a. look at the first digit. and match it with the singles hash
# b. look at the second digit and:
# i. if it is not a one, then match the second digit to the tens hash
# and then look at the last digit/third digit and match that to the singles hash
# ii. if it is a one, then look at the second digit as well. and match both digits
# it with the teens hash
#{single} "hundred" #{teens} ORRRR #{tens single} OORRRR #{single}
num_arr = num.to_s.split('').map { |e| e.to_i }
arr_len = num_arr.length
if arr_len == 1
p singles.fetch(num_arr[0])
end
if arr_len == 2
if num_arr[0] == 1
p teens.fetch(num_arr.join.to_i)
else
#p tens.fetch(num_arr[0]).concat(singles.fetch(num_arr[1]))
p tens.fetch(num_arr[0]) + " " + singles.fetch(num_arr[1])
end
end
if arr_len == 3
if num_arr[1] == 1
num_arr.delete_at(0) #turns 3 digit array into teen array
rest = teens.fetch(num_arr.join.to_i) # =>teen_array.join.to_i = turn the teen array into a double digit integer
elsif num_arr[1] == 0
rest = singles.fetch(num_arr[2])
else
rest = tens.fetch(num_arr[1]) + " " + singles.fetch(num_arr[2])
end
p singles.fetch(num_arr[0]) + " hundred " + rest
end
# if arr_len == 4
# if num_arr[1] == singles.fetch(num_arr[1]) + " hundred " + rest
# if num_arr[2] == 1
# num_arr.map { |x| delete_at(0)} #turns 3 digit array into teen array
# teens.fetch(num_arr.join.to_i)
# elsif num_arr
# end
# end
p singles.fetch(num_arr[0]) + " thousand " + rest
end
# feeling:
# from one-nine thousands to below, we apply a specific logic
# {something} + "million" + {something} + "thousand" + {something} + hundred logic
# {something} = simply hundred logic
# in_words(82,102,713) => "{eighty two} million {one hundred
# two} thousand {seven hundred thirteen - which is another hundred logic}"
# [8,2,1,0,2,7,1,3] % 3 => first array get first modulo elements.
# for the rest of the array, get every three elements.
# [8,2] [1,0,2] [7,1,3] - apply logic to each array based on its length
# eighty two -- one hundred and two --- seven hundred and thirteen
# if three arrays = mill and thousand
# if four arrays = bill, mill and thousand
# if five arrays = trill, bill, mill, and thousand
# Process:
# 1. get the general idea of what you would do -- THIS WILL DEF CHANGE
# 2. do examples - write it out- and go through each one and pseudocode how it would work out
# 3. consider special cases and how that will affect your code.
# 4. consider scalability - will you method work if you zoom out.
# 5. When you realize that you're going through an endless loop for something so simple,
# step back and look for PATTERNS.
# # 1456
# # [1,4,5,6]
# # 2987
# # [2,9,8,7]
# # 3016
# # [3,0,1,6]
# [3,3,0,1,6]
# WHAT I KNOW:
# 1. If we are past two digits, I know the first digit just needs to be printed as is + string "thousand, hundred"
# 2. I know that if there is a zero in the array, that we need to skip it and analyze the subsequent digits
# 3.
# ones
# teens
# twenty -nineties
# hundreds - plug single + hundred + rest of logic
# thousands = hundreds - plug single + hundred + rest of logic
# ten thousands = twenty logic + thousands logic
# hundreds thousnds = hundreds logic + "thousand" + hundreds logic
# 105,064
# # my_arr = []
# # num.to_s.split('').each do |int|
# # tens.each do |k,v|
# # if int.to_i == k
# # my_arr << v
# # end
# # end
# # end
# # p my_arr
# end
# num_conversion(115)
# num_conversion(107)
# num_conversion(132)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.