[Tutor] anagrams

Gregor Lingl glingl at aon.at
Sun Nov 9 12:24:19 EST 2003



Danny Yoo schrieb:

>...
>Time to jump in as well!  *grin*
>

Thanks for your and Karls contributions to the anagram - problem!

They were both very instructive for me (except that I still have
to think about this itertools-spiced one ... which also is very
fast.)

Karl's first solution is the fastest and Danny's - I can't help, I have
to say
so - is, as often, the clearest, best explained and most beautiful one.
(Both of them
apply similar ideas)

Many on this list contribute very fine, use- and helpful postings, but
Danny is certainly not only a gifted Python tutor but also a
marvelous tutoring tutor.

Regards,
Gregor



>
>###
>def main():
>    for anagrams in find_anagrams(open("wordlist.txt").readlines()):
>        if len(anagrams) > 1:
>            print '\t'.join(anagrams)
>
>
>def anagram_signature(word):
>    """Finds the anagram "signature" of a word."""
>    letters = list(word.lower().strip())
>    letters.sort()
>    return ''.join(letters)
>
>
>def find_anagrams(words):
>    """Returns a partitioning of words into equivalence classes, based on
>    a word's anagram signature."""
>    d = {}
>    for word in words:
>        d.setdefault(anagram_signature(word), []).append(word.strip())
>    return d.values()
>
>
>if __name__ == '__main__':
>    main()
>###
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>




More information about the Tutor mailing list