School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 298,549 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,400 people online right now. Registration is fast and FREE... Join Now!




cryptarithm

 

cryptarithm, i made a code in python but cann't work in ubuntu for 2.5 version.

rabbicse

20 Mar, 2009 - 08:01 AM
Post #1

New D.I.C Head
*

Joined: 28 Feb, 2009
Posts: 3

python

from __future__ import division
from itertools import permutations
from re import findall
from string import maketrans

def solve(s):
'''Find solutions to alphametic equations.

>>> solve('SEND + MORE == MONEY')
9567 + 1085 == 10652

'''
words = findall('[A-Za-z]+', s)
chars = set(''.join(words)) # characters to be substituted
assert len(chars) <= 10 # there are only ten possible digits
firsts = set(w[0] for w in words) # first letters of each of word
chars = ''.join(firsts) + ''.join(chars - firsts)
n = len(firsts) # chars[:n] cannot be assigned zero
for perm in permutations('0123456789', len(chars)):
if '0' not in perm[:n]:
trans = maketrans(chars, ''.join(perm))
equation = s.translate(trans)
if eval(equation):
print equation

for alphametic in [
'SEND + MORE == MONEY',
'VIOLIN * 2 + VIOLA == TRIO + SONATA',
'SEND + A + TAD + MORE == MONEY',
'ZEROES + ONES == BINARY',
'DCLIZ + DLXVI == MCCXXV',
'COUPLE + COUPLE == QUARTET',
'FISH + N + CHIPS == SUPPER',
'SATURN + URANUS + NEPTUNE + PLUTO == PLANETS',
'EARTH + AIR + FIRE + WATER == NATURE',
('AN + ACCELERATING + INFERENTIAL + ENGINEERING + TALE + ' +
'ELITE + GRANT + FEE + ET + CETERA == ARTIFICIAL + INTELLIGENCE'),
'TWO * TWO == SQUARE',
'HIP * HIP == HURRAY',
'PI * R ** 2 == AREA',
'NORTH / SOUTH == EAST / WEST',
'NAUGHT ** 2 == ZERO ** 3',
'I + THINK + IT + BE + THINE == INDEED',
'DO + YOU + FEEL == LUCKY',
'NOW + WE + KNOW + THE == TRUTH',
'SORRY + TO + BE + A + PARTY == POOPER',
'SORRY + TO + BUST + YOUR == BUBBLE',
'STEEL + BELTED == RADIALS',
'ABRA + CADABRA + ABRA + CADABRA == HOUDINI',
'I + GUESS + THE + TRUTH == HURTS',
'LETS + CUT + TO + THE == CHASE',
'THATS + THE + THEORY == ANYWAY',
]:
print alphametic
solve(alphametic)
print


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/4/09 02:12AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month