Faster way to do this?
Niklas Frykholm
niklas at kagi.com
Thu May 22 02:11:15 EDT 2003
Here is yet another take on the problem. It is quite a bit faster for
most "sane" word lengths:
niklas% python wordfinder.py somnabulistexplos
Read 234937 words in 1.39s
Method 1: found 3566 words in 17.20s
Built dict in 17.73s
Dict method found 3566 words in 0.45s
---- BEGIN CODE
def main():
# ... Read in wordlist and words here
start = time.time()
dict = Dict(words)
print 'Built dict in %.2fs' % (time.time() - start)
start = time.time()
found = dict.count(word)
print 'Dict method found %d words in %.2fs' % (found, time.time() - start)
class Dict:
def __init__(self, words):
self.dict = {}
for w in words:
w = self.sort_letters(w)
for i in range(1,len(w)):
sub = w[0:i]
if not self.dict.has_key(sub):
self.dict[sub] = 0
self.dict[w] = self.dict.get(w,0) + 1
def sort_letters(self, w):
w = [c for c in w]
w.sort()
return ''.join(w)
def count(self, word):
self.counted = {}
word = self.sort_letters(word)
return self.count1('', word)
def count1(self, start, rem):
sum = self.dict.get(start, 0)
if sum > 0:
if not self.counted.has_key(start):
self.counted[start] = 1
else:
sum = 0
for i in range(len(rem)):
next = start + rem[i]
if self.dict.has_key(next):
sum += self.count1(next, rem[i+1:])
return sum
---- END CODE
More information about the Python-list
mailing list