gpt4 book ai didi

Ruby 代码说明 : Numbers to String using Recursion

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:59 25 4
gpt4 key购买 nike

有人分享了一些 Ruby 代码,讲述了他们如何完成将数字转换为文字的挑战。这个解决方案是迄今为止最容易阅读的,但我并不完全理解它的递归方面。具体来说,在下面一行...

elsif int.to_s.length == 1 && int/num > 0
return str + "#{name}"

如果 return 语句将代码从循环中中断,为什么此代码不执行并在它为真时中断循环?例如,当 int = 4 且 num = 1 时,这将被证明为真并触发 return 语句,但代码会继续执行直到 num = 4。

对代码这方面的任何澄清,以及对理解此解决方案的任何一般性建议,都会有所帮助。仍在尝试理解递归。谢谢

def in_words(int)
numbers_to_name = {
1000000 => "million",
1000 => "thousand",
100 => "hundred",
90 => "ninety",
80 => "eighty",
70 => "seventy",
60 => "sixty",
50 => "fifty",
40 => "forty",
30 => "thirty",
20 => "twenty",
19=>"nineteen",
18=>"eighteen",
17=>"seventeen",
16=>"sixteen",
15=>"fifteen",
14=>"fourteen",
13=>"thirteen",
12=>"twelve",
11 => "eleven",
10 => "ten",
9 => "nine",
8 => "eight",
7 => "seven",
6 => "six",
5 => "five",
4 => "four",
3 => "three",
2 => "two",
1 => "one"
}

str = ""
numbers_to_name.each do |num, name|
if int == 0
return str
elsif int.to_s.length == 1 && int/num > 0
return str + "#{name}"
elsif int < 100 && int/num > 0
return str + "#{name}" if int%num == 0
return str + "#{name} " + in_words(int%num)
elsif int/num > 0
return str + in_words(int/num) + " #{name} " + in_words(int%num)
end
end
end

puts in_words(4) == "four"
puts in_words(27) == "twenty seven"
puts in_words(102) == "one hundred two"
puts in_words(38_079) == "thirty eight thousand seventy nine"
puts in_words(82102713) == "eighty two million one hundred two thousand seven hundred thirteen"

最佳答案

调试代码

这是一个在每个分支中都有调试行的修改版本:

def in_words(int, indent="")
puts "#{indent}in_words(#{int})"
numbers_to_name = {
1000000 => "million",
1000 => "thousand",
100 => "hundred",
90 => "ninety",
80 => "eighty",
70 => "seventy",
60 => "sixty",
50 => "fifty",
40 => "forty",
30 => "thirty",
20 => "twenty",
19=>"nineteen",
18=>"eighteen",
17=>"seventeen",
16=>"sixteen",
15=>"fifteen",
14=>"fourteen",
13=>"thirteen",
12=>"twelve",
11 => "eleven",
10 => "ten",
9 => "nine",
8 => "eight",
7 => "seven",
6 => "six",
5 => "five",
4 => "four",
3 => "three",
2 => "two",
1 => "one"
}

str = ""
numbers_to_name.each do |num, name|
puts "#{indent} testing #{num}"
if int == 0
puts "#{indent} Empty string"
return str
elsif int.to_s.length == 1 && int/num > 0
puts "#{indent} Single digit found (#{name})!"
return str + "#{name}"
elsif int < 100 && int/num > 0
puts "#{indent} Double digits found (#{name})!"
return str + "#{name}" if int%num == 0
puts "#{indent} Recursive call with #{int%num} :"
return str + "#{name} " + in_words(int%num, indent+" ")
elsif int/num > 0
puts "#{indent} Recursive call with #{int/num} and #{int%num} :"
indent += " "
return str + in_words(int/num, indent) + " #{name} " + in_words(int%num,indent)
end
end
end

对于 in_words(4),它输出:

in_words(4)
testing 1000000
testing 1000
testing 100
testing 90
testing 80
testing 70
testing 60
testing 50
testing 40
testing 30
testing 20
testing 19
testing 18
testing 17
testing 16
testing 15
testing 14
testing 13
testing 12
testing 11
testing 10
testing 9
testing 8
testing 7
testing 6
testing 5
testing 4
Single digit found (four)!
four

对于 27:

in_words(27)
testing 1000000
testing 1000
testing 100
testing 90
testing 80
testing 70
testing 60
testing 50
testing 40
testing 30
testing 20
Double digits found (twenty)!
Recursive call with 7 :
in_words(7)
testing 1000000
testing 1000
testing 100
testing 90
testing 80
testing 70
testing 60
testing 50
testing 40
testing 30
testing 20
testing 19
testing 18
testing 17
testing 16
testing 15
testing 14
testing 13
testing 12
testing 11
testing 10
testing 9
testing 8
testing 7
Single digit found (seven)!
twenty seven

回答

For example, when int = 4 and num = 1, this would prove to be true and trigger the return statement, yet the code continues until num = 4.

4 作为输入,num = 1 永远不会发生。 numbers_to_name 是递减顺序的,所以返回语句将发生在 num=4 之后,该方法不会执行任何其他代码。

关于Ruby 代码说明 : Numbers to String using Recursion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com