I have the following function, which is part of a crossword puzzle solver:
def CrosswordPossibleWords(p_words, p_cw_words):
"""For each word found in the crossword, find the possible words and keep track of the one with the minimum possible words.
Keyword arguments:
p_words -- The dictionary words.
p_cw_words -- The crossword word attributes.
"""
l_min = 999999999
l_min_index = -1
l_index = 0
l_choices = []
for l_cw_word in p_cw_words:
if l_cw_word[2] >= l_min_length and '-' in l_cw_word[4]:
pattern = re.compile('^' + l_cw_word[4].replace('.', '%').replace('-', '.').upper() + '$', re.UNICODE)
l_choice = []
for l_word in [w for w in p_words if len(w) == len(l_cw_word[4])]:
if re.match(pattern, l_word):
l_choice.append(l_word)
l_choices.append(l_choice)
if len(l_choice) < l_min:
l_min_index = l_index
l_min = len(l_choice)
else:
l_choices.append([])
l_index = l_index + 1
return (l_choices, l_min_index)
Crossword puzzles look like:
[row, col, length, direction, word]
I have '.'in a word if I cannot solve this word and '-'if I do not know this letter.
How can I make this code faster? It currently takes about 2.5 seconds. Thought to use numpy strings; since numpy seems to be 10 times faster, but I don't know anything about numpy and donβt know if I can use all current string functions with it.
Any ideas?
source
share