Re: Can I play too?

2005-04-02 Thread stelios xanthakis
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:

real0m1.491s
user0m1.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:

real0m0.923s
user0m0.760s
sys 0m0.070s
---
Stelios
--
http://mail.python.org/mailman/listinfo/python-list


Can I play too?

2005-04-02 Thread Scott David Daniels
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, ...

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