Thomas Rast wrote:
Tom Carrick <[EMAIL PROTECTED]> writes:
In my attempted learning of python, I've decided to recode an old
anagram solving program I made in C++. The C++ version runs in less
than a second, while the python takes 30 seconds.

Indeed, your program can be improved to run about ten times as fast, ... <great stuff>

This problem inspired an "all anagrams" program. Using it I was able to find the largest anagram group in Shakespeare's first folio in about the time you originally found anagrams for an individual word.

 7: owers = rowse = sower = sowre = swore = woers = worse

====

    def words(source):
        for line in source:
            for word in line.split():
                yield word


def all_anagrams(words): seen = dict()

        for word in words:
            word = word.lower()
            if word not in seen:
                dorw = ''.join(sorted(word))
                try:
                    seen[dorw].append(word)
                except KeyError:
                    seen[dorw] = [word]
                    if word == dorw:
                        continue
                seen[word] = ()
        for group in seen.itervalues():
            if len(group) > 1:
                yield -len(group), sorted(group) # conveniently sortable


def main(sources): for filename in sources: dictionary = open(filename, 'r') print "All anagrams from %s:" % filename try: for nsize, group in sorted(all_anagrams(words(dictionary))): print '%2s: %s' % (-nsize, ' = '.join(group)) finally: dictionary.close() print



if __name__ == '__main__':
    import sys
    main(sys.argv[1:] or ['anagrams.py'])
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to