------ art_66533_6289323.1182137288807
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Hello everyone,
At the outset, I had no idea how difficult this problem would be. In fact,
it turns out that this is actually an NP-complete problem if the number base
is not fixed at 10. Anyway... Initially I experimented with a backtracking
solution, but ran into trouble getting it to work properly. So for now I am
submitting this solution. Although less than perfect, it does work for the
test inputs, as well as additional solutions I have tried. The idea is to
brute force a solution by trying all possible combinations of solutions
until one works.
For this solution, I used the Permutation Gem available here:
http://permutation.rubyforge.org/doc/index.html
require 'Permutation'
To avoid having to (re)write the combination code myself, I also used the
Combinations class from:
http://butunclebob.com/ArticleS.UncleBob.RubyCombinations
This is great code, someone really should create a gem for it!
(see link for the code)
I packaged all of my code inside the following class:
class VerbalArithmetic
# Parse given equation into lvalues (words on the left-hand side of the
' that
# are to be added together) and an rvalue (the single word on the
right-hand side)
def parse_equation (equation)
lvalues quation.split("+")
rvalue values[-1].split("