Faster way to do this?
Sami Hangaslammi
shangius at yahoo.com
Wed May 21 11:50:55 EDT 2003
Freddie <oinkfreddie at oinkmadcowdisease.oinkorg> wrote in message news:<Xns938295DE13BC1freddiethescaryeleph at 203.10.110.105>...
> Hi.
>
> I was quite bored today, so I hacked up a quick Python script to do.. well,
> I'm not sure what it's called. 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. In this case, it might be ('he', 'hell', 'hello', 'hoe',
> 'hole', 'oh'). Just wondering if there's any way to make it faster, perhaps
> using a bizarre list comprehension :)
def method2(chars, words):
found = 0
letters = {}
for c in chars:
letters[c] = letters.get(c,0)+1
for word in words:
wletters = {}
for c in word:
if c not in letters:
break
wletters[c] = wletters.get(c,0)+1
if wletters[c] > letters[c]:
break
else:
found += 1
return found
$ python2.2 wordfinder.py hel
Read 45392 words in 0.38s
Method 1: found 1 words in 1.38s
Method 2: found 1 words in 0.22s
$ python2.2 wordfinder.py hello
Read 45392 words in 0.38s
Method 1: found 6 words in 1.62s
Method 2: found 6 words in 0.23s
$ python2.2 wordfinder.py hellosapfoijaspginapgisngpdind
Read 45392 words in 0.38s
Method 1: found 1475 words in 3.65s
Method 2: found 1475 words in 0.46s
More information about the Python-list
mailing list