Anagrams

Paul Hankin paul.hankin at gmail.com
Tue Oct 23 20:28:43 EDT 2007


On Oct 23, 9:21 am, cokofree... at gmail.com wrote:
> This one uses a dictionary to store prime values of each letter in the
> alphabet and for each line multiple the results of the characters
> (which is unique for each anagram) and add them to a dictionary for
> printing latter.
>
> def anagram_finder():
>     primeAlpha = {'a':2, 'b':3, 'c':5, 'd':7,'e' : 11, 'f':13, 'g':17,
> 'h':19,        \
>                   'i':23, 'j':29, 'k':31, 'l':37, 'm':41, 'n':43, 'o':
> 47, 'p':53,    \
>                   'q':59, 'r':61, 's':67, 't':71, 'u':73, 'v':79, 'w':
> 83, 'x':89,    \
>                   'y':97, 'z':101}
> ...

A somewhat nicer start: compute primes (naively) rather than typing
out the dictionary.

import string
from itertools import ifilter, count

def anagram_finder():
     primes = ifilter(lambda p: all(p % k for k in xrange(2, p)),
count(2))
     primeAlpha = dict(zip(string.lowercase, primes))
     ...

--
Paul Hankin




More information about the Python-list mailing list