I am trying to build a sudoku solver without much googling around. Right now, I am working on a function to test whether the board is valid, which I will use later in a loop. This is what the function currently looks like.
def valid(board):s = 0for row in board:if row.count('1') == 1 and row.count('2') == 1 and row.count('3') == 1 and row.count('4') == 1 and row.count('5') == 1 and row.count('6') == 1 and row.count('7') == 1 and row.count('8') == 1 and row.count('9') == 1:s += 1for column in range(len(board)):if i[column].count('1') == 1 and i[column].count('2') == 1 and i[column].count('3') == 1 and i[column].count('4') == 1 and i[column].count('5') == 1 and i[column].count('6') == 1 and i[column].count('7') == 1 and i[column].count('8') == 1 and i[column].count('9') == 1:s += 1for r in range(3):for c in range(3):print(board[r][c], end = '')if s == 18:print('valid')else:print('no')
The valid board I am using to test is this array of arrays:
example = [['4','3','5','2','6','9','7','8','1'],['6','8','2','5','7','1','4','9','3'],['1','9','7','8','3','4','5','6','2'],['8','2','6','1','9','5','3','4','7'],['3','7','4','6','8','2','9','1','5'],['9','5','1','7','4','3','6','2','8'],['5','1','9','3','2','6','8','7','4'],['2','4','8','9','5','7','1','3','6'], ['7','6','3','4','1','8','2','5','9']]
I am certain this is not the most efficient way to go about this but I am really just trying to understand the problem. So, when a row has each number 1-9 appear once, I add 1 to my s variable. When each column meets the same criteria, I also add 1 to my s variable. So when each row and column has numbers 1-9 appear once in them, my s variable should be 18, but this does not make the board valid as I still have to check each 3x3 box meets their own criteria. In the end, the s variable should be equal to 27 for the board to be valid.
The code I have tried to make the 3x3 boxes print will only print the first 3x3 box, how can I make it repeat for each 3x3 box? Can anyone help me with this issue? Thanks for the help.
Edit: I guess the brute force method would look like this:
for r in range(3):for c in range(3):print(board[r][c], end = '')print('\n')for r in range(3):for c in range(3,6):print(board[r][c], end = '')print('\n')for r in range(3):for c in range(6,9):print(board[r][c], end = '')print('\n')print('\n')for r in range(3,6):for c in range(3):print(board[r][c], end = '')print('\n')for r in range(3,6):for c in range(3,6):print(board[r][c], end = '')print('\n')for r in range(3,6):for c in range(6,9):print(board[r][c], end = '')print('\n')print('\n')for r in range(6,9):for c in range(3):print(board[r][c], end = '')print('\n')for r in range(6,9):for c in range(3,6):print(board[r][c], end = '')print('\n')for r in range(6,9):for c in range(6,9):print(board[r][c], end = '')print('\n')
edit 2, I found a better way to do this:
def three(board):for row in range(0, 9, 3):for col in range(0, 9, 3):b = board[row][col] + board[row][col+1] + board[row][col+2] + board[row+1][col] + board[row+1][col+1] + board[row+1][col+2] + board[row+2][col] + board[row+2][col+1] + board[row+2][col+2]print(b)# test if valid...
this yielded me this result:
435682197
269571834
781493562
826374951
195682743
347915628
519248763
326957418
874136259
^ this is every 3x3 within the board.