Faster way to do this?

Freddie oinkfreddie at oinkmadcowdisease.oinkorg
Wed May 21 02:01:02 EDT 2003


Another, quite a bit faster method. Would generating a list of all possible 
words, then looking for them as dictionary keys be any faster? I can't 
actually work out how to generate such a list :\ From the input ('m', 'o', 
'o') I want ('m', 'mo', 'mo', 'moo', 'o', 'om', 'oo', 'oom', 'o', 'om', 'oo', 
'oom') in any sort of order. Well, maybe without duplicates :)


$ python2.2 wordfinder.py hello
Read 45392 words in 0.98s
Method 1: found 6 words in 3.73s
Method 2: found 6 words in 0.39s

$ python2.2 wordfinder.py somnambulistexplos
Read 45392 words in 0.96s
Method 1: found 1389 words in 6.74s
Method 2: found 1389 words in 2.98s


def method2(chars, words):
	found = 0
	
	for word in words:
		if len(word) > len(chars):
			continue
		
		temp = chars[:]
		
		moo = 0
		wlen = len(word)
		for i in range(wlen):
			if word[i] in temp:
				temp.remove(word[i])
				moo += 1
			else:
				break
		
		if moo == wlen:
			found += 1
	
	return found

--------------------------------------------------------------
Remove the z's from my e-mail address if you really want to




More information about the Python-list mailing list