Python recognizes a verification code

It is very simple to identify the Xiaoxiaole verification code of a test. In a Jiugongge, after finding two adjacent elements and swapping them, ensure that there is a row or a column of elements that are the same, as shown in the figure below: If this Jiugongge is a
insert image description here
picture If so, we divide it into 9 parts, and then mark each grid as a category, or get 9 pictures directly from the source code, and finally convert it into a two-dimensional matrix for recognition. After the above Figure, we transform the two-dimensional matrix as

matrix = [[0, 1, 2], [3, 2, 1], [2, 0, 1]]

Next, you only need to write an exchange algorithm recognition in python

def find_same(matrix):
    # 判断矩阵一行或者一列的元素是否相同
    for i in range(len(matrix)):
        if len(set(matrix[i])) == 1:
            return True
    for j in range(len(matrix[0])):
        if len(set([matrix[i][j] for i in range(len(matrix))])) == 1:
            return True



    return False


def swape(matrix, i, j, m, n):
    # 交换矩阵中两个元素的位置
    matrix[i][j], matrix[m][n] = matrix[m][n], matrix[i][j]
    return matrix


def for_swape(matrix):
    # 循环整个矩阵,交换相邻的两个元素,判断矩阵中的一行或者一列是否有相同的

Guess you like

Origin blog.csdn.net/qq_36551453/article/details/131614907