Ruby Code Explanation: Numbers to String using Recursion -
someone shared ruby code on how completed challenge have convert numbers words. solution far simplest read, not understand recursive aspect of it. specifically, in line below...
elsif int.to_s.length == 1 && int/num > 0 return str + "#{name}"
if return statement breaks code out of loop, why wouldn't code execute , break loop once true? example, when int = 4 , num = 1, prove true , trigger return statement, yet code continues until num = 4.
any clarification on aspect of code, general advice understanding solution, helpful. still trying understand recursion. thanks
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 |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 8 thousand seventy nine" puts in_words(82102713) == "eighty 2 million 1 hundred 2 thousand 7 hundred thirteen"
debugging code
here's modified version debug lines in every branch :
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 |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 #{int%num} :" return str + "#{name} " + in_words(int%num, indent+" ") elsif int/num > 0 puts "#{indent} recursive call #{int/num} , #{int%num} :" indent += " " return str + in_words(int/num, indent) + " #{name} " + in_words(int%num,indent) end end end
for in_words(4)
, outputs :
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)! 4
and 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 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 7
answer
for example, when int = 4 , num = 1, prove true , trigger return statement, yet code continues until num = 4.
with 4
input, num = 1
never happen. numbers_to_name
in decreasing order, return statement happen num=4
, , method won't execute other code after that.
Comments
Post a Comment