Any bright ideas on how I can speed this up? It seems REALLY slow for
as little as it does.
I'm trying to generate word squares like this:
P R E Y
L A V A
O V E R
T E N D
except 6x6 rather than 4x4.
In particular, the profiler indicates that {method 'join' of 'str'
objects} is eating an awful lot of time (odd...), but I'm having a
brain cramp trying to think up a workaround or alternate approach.
The required sixwords.txt file can be gotten here:
http://home.columbus.rr.com/javajack/sixwords.zip
(wasn't sure if people would complain about a 43K attachment or not)
Alan
sixwords = open('sixwords.txt', 'r')
words = sixwords.readlines()
sixwords.close()
words = [x.strip() for x in words]
worddict = {}
for w in words:
for x in range(1,7):
y = w[0:x]
try:
worddict[y].append(w)
except KeyError:
worddict[y] = [w]
counter = 0
def solve(currentwords, depth):
global counter
counter += 1
if counter % 1000 == 0:
print counter,
if len(currentwords) == 6:
print depth, counter, currentwords
for q in currentwords:
print q
print "\n\n"
dummy = raw_input()
return True
else:
print "\n\ncurrentwords", currentwords, counter
big = ''.join(currentwords)
prefixes = [ big[0::6] ]
nextletterset = list( set( [x[len(currentwords)] for x in
worddict[prefixes[0]]] ) )
for letter in nextletterset:
for maybefuture in worddict[letter]:
futurezip = zip(*currentwords+[maybefuture])
futureprefixes = [''.join(z) for z in futurezip]
validprefixes = [f in worddict for f in futureprefixes]
if not False in validprefixes:
solve( currentwords + [maybefuture], depth + 1 )
return False
seed=['banana']
import cProfile
cProfile.run('print solve(seed, 1)')
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor