Anagrams

cokofreedom at gmail.com cokofreedom at gmail.com
Tue Oct 23 04:21:05 EDT 2007


This was from a random website I found on practising good programming
techniques and I thought I'd see what ways people could find to write
out this example. Below are my two examples (which should work...).

I am merely interested in other techniques people have (without
resorting to overusage of C modules and such like), just python and
good computer science techniques.

For the wordlist go to this link <a href="http://codekata.pragprog.com/
2007/01/kata_six_anagra.html">Kata Anagrams</a>
Anyway here are my two examples.

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.
<pre>
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}
    dictAna =  {}
    file = open ("wordlist.txt", "r")
    for line in file:
        value = 1
        for s in line:
            if s.lower() in primeAlpha:
    	           value *= primeAlpha[s.lower()]
        dictAna.setdefault(value, []).append(line.strip())
    file.close()
    print "\n".join(['Anagrams are: %s' % (v) for k, v in
dictAna.items() if len(dictAna[k]) > 1])
</pre>

My second is a little bit simpler. Each dictionary key is an
alphabetical ordering of the line in question, so that anagrams will
appear the same. It will add to the dictionary the new word either in
an existing key, or create a new one for it.

<pre>
def anagram_finder():
    dict = {}
    file = ('wordlist.txt', 'r')
    for line in file:
		strip_line = line.strip().lower()
		sort_line = str(sorted(strip_line))
		dict.setdefault(sort_line, []).append(strip_line)
    file.close()
    print '\n'.join(['Anagrams are: %s' % (v) for k, v in dict.items()
if len(dict[k]) > 1])
</pre>

Comparing them with timeit, they both took around 1 second or so, with
the first being slightly faster.

What other ways do people think will work (and do mine actually work
for other people!)




More information about the Python-list mailing list