How to create lists of 3x3 sudoku block in python

后端 未结 5 1707
长发绾君心
长发绾君心 2021-01-23 11:27

I need help creating a list for each of the 9 3x3 blocks in sudoku. so I have a list of lists representing the original sudoku board (zero means empty):

board=[[         


        
5条回答
  •  轮回少年
    2021-01-23 12:09

    this uses no builtins and is faster 3 nested for loops

    def get_boxes(board):
        boxes = []
        for i in range(9): 
            if i == 0 or i % 3 == 0:  
                box_set_1 = board[i][:3] + board[i + 1][:3] + board[i + 2][:3]  
                boxes.append(box_set_1)
                box_set_2 = board[i][3:6] + board[i + 1][3:6] + board[i + 2][3:6]
                boxes.append(box_set_2)
                box_set_3 = board[i][6:] + board[i + 1][6:] + board[i + 2][6:]
                boxes.append(box_set_3)
    

提交回复
热议问题