Skip to content Skip to sidebar Skip to footer

Python - How To Make Crossword Solver Faster

I have the following function that is part of a crossword solver: def CrosswordPossibleWords(p_words, p_cw_words): '''For each word found in the crossword, find the possible wo

Solution 1:

You could partition your dictionary by word-length BEFORE calling this function, so it doesn't have to re-do it with every call.

Solution 2:

Whilst I agree with Scott Hunter, you are probably looking for something like this where the lists are substituted with dicts:

defCrosswordPossibleWords(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 = {}    # using dict instead of listfor 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 = {}  # using dict instead of listfor l_word in [w for w in p_words iflen(w) == len(l_cw_word[4])]:
                if re.match(pattern, l_word):

                    l_choice[l_word]=None

            l_choices[l_choice]=Noneiflen(l_choice) < l_min:  ##
                l_min_index = l_index  ## Get rid of this.
                l_min = len(l_choice)  ##else:
            l_choices.append([])    # why append empty list?
        l_index = l_index + 1
        l_choices=list(l_choices.keys())   # ...you probably need the list again...return (l_choices, l_min_index)

Post a Comment for "Python - How To Make Crossword Solver Faster"