On Thu, 24 Feb 2005 23:35:28 +0900
Ruby Quiz <james / grayproductions.net> wrote:
> [Snipped amazingly long essay]
Thanks for all your effort James. It is always interesting to read your
summaries, and I always learn something.
Again I could not resist and corrected the bugs you spotted and hacked
the solution to incorporate the behaviour you intended. It's amazing how
difficult it is to get the specifications right, and to make a
non-native speaker understand them.
Here is the code portion that contains all the important additions:
def find_wordskip(number)
result = []
sub_find(@root, number) do | words, rest_number |
if rest_number.empty?
result.concat(words)
elsif rest_number.length == 1
find_wordskip(rest_number).each do | sentence |
words.each do | w |
result << w + '-' + sentence
end
end
words.each do | w |
result << w + '-' + rest_number[0].to_s
end
else
find_wordskip(rest_number).each do | sentence |
words.each do | w |
result << w + '-' + sentence
end
end
end
end
sub_find(@root, number[1..-1]) do | words, rest_number |
if rest_number.empty?
result.concat(words.map{|w| number[0].to_s + '-' + w})
else
find_wordskip(rest_number).each do | sentence |
words.each do | w |
result << number[0].to_s + '-' + w + '-' + sentence
end
end
end
end
result
end
it goes through some hoops to allow adding of digits at the beginning of
a sentence, at the end of a sentence and between any two words in the
sentence.
I spat this out while I had to code something important, so I hope I got
the recursion right. On a quick check it did not spit out anything
unwanted, so I hope its correct.
The full solution can be found at:
http://ruby.brian-schroeder.de/quiz/phonewords/
Best regards,
Brian
--
http://ruby.brian-schroeder.de/