[Tutor] anagrams

Karl Pflästerer sigurd at 12move.de
Wed Nov 5 16:59:38 EST 2003

On  5 Nov 2003, Gregor Lingl <- glingl at aon.at wrote:

> Triggered by a remark on the DailyPythonUrl I looked for the

Is that a newsletter?

> Kata - Site and found an interesting little problem: finding
> anagrams among the words in a textfile.

> I found a Python Solution, which needs 0.75 secs on my 2.3 GHz PC,
> but it certainly is not optimal. (Maybe someone here likes to find
> a better one?)

Don't know if mine one is better; on my PC it's 30% faster than your

> Another problem remains for me: My script finds 2531 groups
> of anagrams with 5683 words alltogether, whereas the above
> mentioned solution has only 2,530 sets of anagrams (and a total of
> 5,680 words). So I suspect, that my solution contains a bug, but
> I couldn't find it (until now).

My solution finds the same values as yours one.  Perhaps the bug is in
the Ruby code?


f = open("wordlist.txt")

d = {}
for word in f:
    k, v = list(word.lower()), word
    k = ''.join(k)
    if k in d:
        d[k] = [v]


ang = [x for x in d.itervalues() if len(x) > 1]

print len(ang)
print sum([len(x) for x in ang])

Please do *not* send copies of replies to me.
I read the list

More information about the Tutor mailing list