• Crossword solver

 

Hi! And again I need help! Now with https://py.checkio.org/ru/mission/crossword-solver/ I don't understant what heppend with assertions. On my PC it work great, function "Check" in mission is working correct, but I cant finish mission.

def find_words(crossword):
    word_list = []
    letters = []
    for i in range(len(crossword)):
        for j in range(len(crossword[i])):
            if crossword[i][j] == '.':
                dot = (i,j)
                letters.append(dot)
    letters.sort()
    count = 0
    while count < len(letters)-1:
        word = [letters[count]]
        while word[-1][1]+1 == letters[count+1][1]:
            word.append(letters[count+1])
            count += 1
            if count >= len(letters)-1:
                break
        if len(word)>2:
            word_list.append(word)
        count+=1
    letters.sort(key=lambda x: x[1])
    count = 0
    while count < len(letters)-1:
        word = [letters[count]]
        while word[-1][0]+1 == letters[count+1][0]:
            word.append(letters[count+1])
            count += 1
            if count >= len(letters)-1:
                break
        if len(word)>2:
            word_list.append(word)
        count+=1
    word_list.sort(key=lambda x: len(x), reverse=True)
    return (word_list, letters)

def solver(crossword, WORDS):
    word_list, letters = find_words(crossword)
    ans = []
    used_words = []
    connections = []
    len_connection = []
    for w in word_list:
        ans.append('')
        used_words.append([])
        connections.append([])
        # print(w)
    for word in range(len(word_list)):
        for w in range(len(word_list[word])):
            for i in range(len(word_list)):
                for j in range(len(word_list[i])):
                    if word_list[word][w] == word_list[i][j] and i not in connections[word]:
                        connections[word].append(i)
    for word in range(len(connections)):
        connections[word].remove(word)
        len_connection.append(len(connections[word]))
    len_connection1 = len_connection.copy()
    # print(len_connection)
    # print(len_connection1)
    connections = list(list(zip(*sorted(zip(len_connection, connections), reverse=True)))[1])
    word_list = list(list(zip(*sorted(zip(len_connection1, word_list), reverse=True)))[1])
    # print(connections)
    # print(word_list)


    letters_dict = {}
    words = word_list.copy()
    word_index = 0
    loop = True
    while loop:
        w = words[word_index]
        wl = len(w)
        for W in WORDS:
            if len(W) == wl and W not in used_words[word_index] and W not in ans:

                for i in range(wl):
                    if w[i] not in letters_dict.keys():
                        adword = True
                    else:
                        let = W[i]
                        let_d = letters_dict[w[i]]
                        if let == let_d:
                            adword = True
                        else:
                            adword = False
                            break
                if adword:
                    ans[word_index]=W
                    for i in range(wl):
                        letters_dict.update({w[i]:W[i]})
                    word_index += 1
                    break

            elif len(W) > wl:
                j = words[word_index-1]
                if ans[word_index-1] not in used_words[word_index-1]:
                    used_words[word_index-1].append(ans[word_index-1])
                ans[word_index-1]=''
                word_index -=1
                letters_dict = {}
                for i in range(len(ans)):
                    if ans[i] != '':
                        for j in range(len(ans[i])):
                            letters_dict.update({words[i][j]:ans[i][j]})
                used_words[word_index+1] = []
                break
        for w in ans:
            if w != '':
                loop = False
            else:
                loop = True
                break
        # print(ans)
        ans_matrix = []
        for i in range(len(crossword)):
            am = ''
            for j in range(len(crossword[0])):
                if (i,j) not in letters_dict.keys():
                    am += 'X'
                else:
                    am += letters_dict[(i,j)]
            ans_matrix.append(am)
    for x in ans_matrix:
        print(x)

    return ans_matrix