Can I play too?

stelios xanthakis sxanth at cs.teiath.gr
Sat Apr 2 13:17:21 EST 2005


Scott David Daniels wrote:

> if __name__ == '__main__':
>     import sys
>     main(sys.argv[1:] or ['anagrams.py'])

This is *exactly* the kind of testcases I'm looking for to test
the soon-to-be-released pyvm.  Great!  I'll be back with results.


For now, a fast anagrams.py is
--------------------------------------
import sys

WORDS = [ i.rstrip () for i in file ('/usr/share/dict/words') ]

def findana (anagram):
     sorted_anagram = sorted(anagram.lower())
     len_anagram = len (anagram)
     found = [ word for word in WORDS if len(word)==len_anagram and 
sorted(word)==sorted_anagram ]
     print "Anagrams of %s: %s" % (anagram, ' '.join(found))

for i in sys.argv [1:]:
     findana (i)
-----------------------------------------


And timings....

time python anagram.pyc stop step words lots pool eat fast slow lamp 
cold door xyzzy
Anagrams of stop: opts post pots spot stop tops
Anagrams of step: pest pets sept step
Anagrams of words: sword words
Anagrams of lots: lost lots slot
Anagrams of pool: loop polo pool
Anagrams of eat: ate eat tea
Anagrams of fast: fast fats
Anagrams of slow: lows owls slow
Anagrams of lamp: lamp palm
Anagrams of cold: clod cold
Anagrams of door: door odor
Anagrams of xyzzy:

real	0m1.491s
user	0m1.390s
sys	0m0.040s

time pyvm anagram.pyc stop step words lots pool eat fast slow lamp cold 
door xyzzy
Anagrams of stop: opts post pots spot stop tops
Anagrams of step: pest pets sept step
Anagrams of words: sword words
Anagrams of lots: lost lots slot
Anagrams of pool: loop polo pool
Anagrams of eat: ate eat tea
Anagrams of fast: fast fats
Anagrams of slow: lows owls slow
Anagrams of lamp: lamp palm
Anagrams of cold: clod cold
Anagrams of door: door odor
Anagrams of xyzzy:

real	0m0.923s
user	0m0.760s
sys	0m0.070s


-------
Stelios




More information about the Python-list mailing list