Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jul 12th, 2005, 5:13 PM   #1
Sane
Banned
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,101
Rep Power: 6 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('')
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:47 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC