Thread: Alphamatics!
View Single Post
Old Jul 12th, 2005, 4:13 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,840
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Alphamatics!

Alphamatics are math questions with the numbers replaced by letters.

So like:

Your + Mom = Hawt

Can be:

8736 + 474 = 9210

For fun I made a program that can figure out the small ones like those pretty easily via brute force (because I don't want to figure out an algorithm/pattern).

"Dr Sane's Alphamatic Solver: Only does addition of 1-2 items on either side so far..."

#<Dependants>
x1 = "YOU"
x2 = ""
y1 = "AN"
y2 = "ASS"
#</Dependants>


"""

That is equivelent to the alphamatic:
< You = An + Ass >

Run the program to see the numerical numbers replacing the letters to make the statement true.

"""



#Module Imports
import sys, random, time

#Organize all the characters into one, and remove duplicate letters, define as 'all'
all = x1+x2+y1+y2
for a in range(len(all)):
    for b in range(len(all)):
        if (a != b) and (all[a] == all[b]):
            all = str(all[:a]+' '+all[a+1:])
all = all.replace(" ","")
if len(all)>10:print'Error: Too many different characters!';sys.exit()

class Sections(object):
    def __init__(self, string, all=all):

        #Initiate Variables
        self.All = all
        self.Name = string
        self.amount = 0

        #Count the number of valid characters
        for a in range(len(string)):
            try:
                if string[a]:
                    self.amount += 1
            except: pass

    def Assign(self, combination):

        val = 0
        #Loop equal to the number of letters in the item
        for a in range(self.amount):
            #Loop equal to the number of different characters in the alphamatic
            for c in range(len(self.All)):
                #If it finds the same letter as found in the Universal All variable, it continues forming the number
                if self.All[c] == self.Name[a]:
                    #Add to the number the next digit
                    val += combination[c]*(10**(self.amount-a-1))

        #Value is now the numerical value of the item
        self.Value = val

def AddCombination(Combinations):

    Open = True
    while Open:

        #The possible number choices range from 0 through 9
        Choice = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        comb = []

        #Cycle through the choices, picking out one random and placing in a new list
        while Choice != []:
            c = random.choice(Choice)
            comb.append(c)
            Choice.remove(c)

        #Make sure the combination is not a duplicate            
        Open = False
        for b in range(len(Combinations)):
            if Combinations[b] == comb:
                Open = True

    return comb        

#Declare the Combinations, and the combination index (zz)
Combinations = [ range(10) ]
zz = 0

#Declare the classes for each item
x1val = Sections(x1)
x2val = Sections(x2)
y1val = Sections(y1)
y2val = Sections(y2)

#Assign valid integers to all the letters
x1val.Assign(Combinations[zz])
x2val.Assign(Combinations[zz])
y1val.Assign(Combinations[zz])
y2val.Assign(Combinations[zz])

#While the first two items added together do not equal the last two items added together...
while (x1val.Value + x2val.Value) != (y1val.Value + y2val.Value):

    #Get a new combination
    Combinations.append(AddCombination(Combinations))

    #Try assigning the new combination 
    x1val.Assign(Combinations[zz])
    x2val.Assign(Combinations[zz])
    y1val.Assign(Combinations[zz])
    y2val.Assign(Combinations[zz])

    #Update the index
    zz += 1

    #Keep IDLE from thinking it's stalled
    if zz%100 == 0:print '.',

#Tell the user the answer
if not x2:x2 = " "
if not y2:y2 = " "
print '\n\n\n\n'
print ' '.join([x1,x2,y1,y2])
print "0"*(len(x1)-len(str(x1val.Value)))+str(x1val.Value),
if x2 == ' ':print x2,
else:print "0"*(len(x2)-len(str(x2val.Value)))+str(x2val.Value),
print "0"*(len(y1)-len(str(y1val.Value)))+str(y1val.Value),
if y2 == ' ':print y2,
else:print "0"*(len(y2)-len(str(y2val.Value)))+str(y2val.Value)
raw_input('')
Sane is offline   Reply With Quote