Faster way to do this?

Lulu of the Lotus-Eaters mertz at gnosis.cx
Wed May 21 13:30:01 EDT 2003


Freddie <oinkfreddie at oinkmadcowdisease.oinkorg> wrote previously:
|You're given a set of letters ('olhel', for example), and you're
|supposed to come up with a bunch of words that use only those letters.

I have a somewhat smaller wordlist on my system.  And this uses Python
2.3a2.  I saved the original program as wordlist-orig.py (but replaced
time.time() with time.clock() for better accuracy):

    % python wordfind-orig.py olhel
    Read 38470 words in 0.34s
    Method 1:  found 7 words in 1.30s

I wrote my own version of the script:

    % python wordfind-re.py olhel
    Read words in 0.02s
    ['eel', 'el', 'he', 'heel', 'hell', 'hello', 'hoe', 'hole', 'lee', 'oh']
    Method RE: found 10 words in 0.06s

I think you could call this an improvement :-).  The script:

    from time import clock
    import sys, re
    WORD_FILE = 'wordlist'

    if __name__=='__main__':
        if len(sys.argv) != 2:
            print 'USAGE: wordfinder.py <letters>'
            sys.exit(-1)

        # Read in the word file
        start = clock()
        wordfile = open(WORD_FILE).read()
        print 'Read words in %.2fs' % (clock()-start)

        # Try first method!
        start = clock()
        pat = re.compile('(?im)^[%s]+$' % sys.argv[1].lower())
        found = re.findall(pat, wordfile)
        print found
        print 'Method RE: found %d words in %.2fs' % (len(found), clock()-start)

It's a lot shorter too.  Displaying the matches seems friendly, but
seems to cost the script at least an extra 1/100 of a second.  You could
comment that out for equivalence

I had been thinking that I might be able to get even faster with
creative use of string.translate().  But the RE version is fast enough
that it's hardly worth optimizing more.

Yours, Lulu...

--
---[ to our friends at TLAs (spread the word) ]--------------------------
Echelon North Korea Nazi cracking spy smuggle Columbia fissionable Stego
White Water strategic Clinton Delta Force militia TEMPEST Libya Mossad
---[ Postmodern Enterprises <mertz at gnosis.cx> ]--------------------------






More information about the Python-list mailing list