Alright, I have a problem with a program that I am trying to write.
The purpose of the program is to solve 1 specific algorithm. That algorithm is send + more = money. The program is supposed to find the value of each letter and return them to you.
I think that I have all of the code working internally, as in the program appears to solve the algorithm correctly. The problem is that I can not get the code to display the solution correctly. I attached my code but if you look at the __str__ function in config you'll notice that it returns the environment for the config. But when you actually run the code it only displays the value of the last changed letter in the environment.
For example, if you run the solve function and wait for it to solve the algorithm, it will only tell you that {y=2} Which it does. But it is leaving out all of the other letters. I thought it was that my environment was not population the .rest part but it is because if you look in my code, if you keep doing self.env.rest.rest.rest etc... it will keep printing out the rest of the letters. I think that it has to be a problem with how I am using the __str__ function in either the config or nonEmptyEnv classes.
If you have any questions, just ask. Also if you try to run the program it will take a minute.
CODE:
""" Purpose of the program is to decrypt the value of the letters in the equation
Send + More = Money. Each letter having a unique single digit value.
Does this using a tree and checking through different configurations of values
for each letter until the goal is found"""
# Empty Enviroment class
class EmptyEnv():
__slots__ = ()
# Non empty enviroment class
class NonEmptyEnv(object):
__slots__ = ('letter','digit','rest')
def __init__(self,letter,digit,rest):
self.letter = letter
self.digit = digit
self.rest = rest
def __str__(self):
result = self.letter + "=" + str(self.digit)
if EmptyEnv:
return result
else:
return result + ',' + str(self.rest)
class Config(object):
__slots__ = ('env','letters','pos','unused')
def __init__(self,env,letters,pos,unused):
self.env = env
self.letters = letters
self.pos = pos
self.unused = unused
def __str__(self):
return '{' + str(self.env) + '}'
# ** This is commented because using this method does display all of the
# values, but It's not pretty and it shouldn't need to be that long
#return "{" + str(self.env) + str(self.env.rest) + str(self.env.rest.rest) + "}"
def mkEmptyEnv():
return EmptyEnv()
# Function that solves, Initial config for Solve function is:
# (E,('s','e','n',...,'y'), 0, (0,1,2,3,...,9))
def solve(config):
"""solve: Config -> Config or FAILURE"""
if isGoal(config):
print(config)
else:
childern = sucessor(config)
for child in childern:
solution = solve(child)
if solution != False:
return solution
return False
# Looks up the value of a letter in an enviroment
def lookUp(letter,env):
if env.letter == letter:
return env.digit
else:
return lookUp(letter,env.rest)
''' Checks if the configutation is the goal, if the config position is
not equal to the length of the letters then it does not check the math '''
def isGoal(config):
if config.pos == len(config.letters):
'''s,e,n,d,m,o,r,y = lookUp('s',config.env),lookUp('e',config.env),
lookUp('n',config.env),lookUp('d',config.env),lookUp('m',config.env),
lookUp('o',config.env),lookUp('r',config.env),lookUp('y',config.env)'''
s = lookUp('s',config.env)
e = lookUp('e',config.env)
n = lookUp('n',config.env)
d = lookUp('d',config.env)
m = lookUp('m',config.env)
o = lookUp('o',config.env)
r = lookUp('r',config.env)
y = lookUp('y',config.env)
return s>0 and m>0 and 1000*s + 100*e + 10*n + d + 1000*m + 100*o + 10*r + e == 10000*m + 1000*o + 100*n + 10*e + y
else:
return False
''' Creates a list of succesor configs from the current config. '''
def sucessor(config):
if config.pos >= len(config.letters):
return []
else:
result = []
letter = config.letters[config.pos]
for i in range(len(config.unused)):
d = config.unused[i]
newDigit = config.unused[:i] + config.unused[i+1:]
result.append(Config(NonEmptyEnv(letter,d,config.env),config.letters,config.pos+1,newDigit))
return result
Example Output:
>>> E = EmptyEnv
>>> c = Config(E, ('s','e','n','d','m','o','r','y'),0,(0,1,2,3,4,5,6,7,8,9))
>>> solve(c)
{y=2}