我正试图在Ruby中找出一个家庭作业解决方案。我明显地想要答案,但如果我在这里发布并且有人告诉我'这就是你如何做',你会觉得它失败了目的
我正在尝试将fixnum翻译成英语单词发音,直到万亿大小。所以它处理从0到999_999_999_999_999
的所有内容起初我想把字符串堆叠成一个FILO队列,然后简单地用一个位置'count'来点击它,表示我目前要说明的是“百”或“千”。单词和面额被设置为类实例全局哈希值,以作为键调用和验证。
我不确定这是不是一个愚蠢的问题,但我正在寻找一种方法来帮助我思考这个小问题。
class Fixnum
@@one_to_teen = {0 => "", 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 = {20 => "twenty", 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'}
@@count_flag = {3 => "hundred", 4 => "thousand", 9 => "million", 12 => "billion", 15 => "trillion"}
def in_words
return "zero" if self == 0
return @@one_to_teen[self] if self < 20
#stack up
num_stack = self.to_s.split('')
count = 0
temp_str = ""
in_words_str = ""
final_str = ""
#loop until empty
#i was trying to see if resetting the temp_str after being handle in the if statements helped get rid of the stack being used. not originally part of the logic.
while !num_stack.empty?
#puts num_stack.inspect
temp_str << (num_stack.pop unless num_stack.empty?)
count+=1
if count%4 == 0
in_words_str = "#{@@one_to_teen["#{temp_str[temp_str.length-1]}".to_i]} #{@@count_flag[count]} "
temp_str = ""
elsif count%3 == 0
if temp_str[temp_str.length-1] != "0"
in_words_str = "#{@@one_to_teen["#{temp_str[temp_str.length-1]}".to_i]} #{@@count_flag[count]} "
#puts temp_str
end
elsif count%2 == 0
in_words_str = "#{@@tens[("#{temp_str[1]}0").to_i]} #{@@one_to_teen[check_teens(temp_str).to_i]}"
temp_str = ""
end
final_str = in_words_str + final_str
end
#somewhere in my logic i needed to do this, i was getting double spaces due to concat somewhere. bandaided it for now...
return final_str.strip.gsub(" "," ")
end
def check_teens(temp_str)
#swapping here to get correct "20" or higher wording.
if temp_str.reverse.to_i < 20
#puts temp_str.reverse
return temp_str.reverse
else
return temp_str[0]
end
end
end
答案 0 :(得分:2)
如果你看看你如何说346,422,378它是三亿四千六百四十二万二千三百七十八。
你已经有了正确的想法,将三人组映射到数十和数百。那些只是重复。
所以算法可能会像:
Use a hash to map word values to each digits in the ones, tens, and hundreds.
Create another hash that contains "trillion", "billion", etc. position map
Get the number and remove any commas, or other formatting.
Is it zero? Easy, done.
Def a sub_string method which takes a group of three and divides that
into three, using your ones, tens hundred mappings. It could return
and array of the word string for each group.
Def a super_string method that then steps through the array you created
and inserts the appropriate word ('trillion', 'million', etc.) between
the array elements.
return your array using something like arr.join(' ') to get one string.
以下是我要保留的内容: 已修改为符合原始规范
class Fixnum
ONES = {0 => "", 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 = {20 => "twenty", 30 => 'thirty', 40 => 'forty', 50 => 'fifty', 60 => 'sixty', 70 => 'seventy', 80 => 'eighty', 90 => 'ninety'}
COUNTS = {3 => "hundred", 4 => "thousand", 9 => "million", 12 => "billion", 15 => "trillion"}
def self.in_words
#fill in code here that uses the other two methods
end
def num_to_word_array(num)
#chop up your number into the substrings and translate
end
def num_words_join(arr)
#join your array using appropriate words, "billion", "million", etc
#return the final string
end
end
尝试将其划分为可以链接的方法。发布您所做的任何更改,我将很乐意批评。