How to Solve Sudoku in Python
myboard = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
Output: [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
def solveSudoku(board) -> None:
"""
Do not return anything, modify board in-place instead.
"""
h = 1
all = 0
old_all = 0
con = True
while (con):
columns = list(zip(*board))
box = [[] for _ in range(9)]
two = []
for x in range(9):
for y in range (9):
num = board[x][y]
if num != '.' :
idx = (x // 3) * 3 + y // 3
box[idx].append(num)
for x in range(9):
for y in range (9):
num = board[x][y]
if num == '.' or type(num) == list:
mylist = []
row = board[x]
col = columns[y]
idx = (x // 3) * 3 + (y // 3)
#print (num, x, y)
for n in range(9):
str_n = str(n+1)
if str_n not in col and str_n not in row and str_n not in box[idx]:
mylist.append(str_n)
board[x][y] = mylist
old_all = all
for x in range(9):
for y in range (9):
num = board[x][y]
if type(num) == list:
if (len(num) == 1):
board[x][y] = num[0]
all += 1
elif (len(num) == 2):
two = num
pos = []
pos.append(x)
pos.append(y)
if all == old_all:
if len(two) == 2:
print(pos, two)
board[pos[0]][pos[1]] = two[0]
else:
con = False
for g in range(9):
print(g+1, board[g])
print (h, all)
h+= 1
return board
myboard = [[".",".","9","7","4","8",".",".","."],["7",".",".",".",".",".",".",".","."],[".","2",".","1",".","9",".",".","."],[".",".","7",".",".",".","2","4","."],[".","6","4",".","1",".","5","9","."],[".","9","8",".",".",".","3",".","."],[".",".",".","8",".","3",".","2","."],[".",".",".",".",".",".",".",".","6"],[".",".",".","2","7","5","9",".","."]]
#myboard = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
solveSudoku(myboard)
1 3
2 7
3 10
4 17
5 19
6 20
[8, 8] ['3', '4']
7 20
[8, 7] ['1', '8']
8 20
9 21
[8, 1] ['4', '8']
10 21
11 22
[7, 6] ['7', '8']
12 22
13 23
14 24
15 29
16 31
[7, 1] ['3', '5']
17 31
18 33
19 36
20 40
21 42
[2, 4] ['3', '5']
22 42
23 44
24 47
25 48
1 ['6', '1', '9', '7', '4', '8', '6', '5', '2']
2 ['7', '8', '3', '6', '5', '2', '1', '3', '9']
3 ['6', '2', '5', '1', '3', '9', '8', '7', '4']
4 ['3', '5', '7', '9', '8', '6', '2', '4', '1']
5 ['2', '6', '4', '3', '1', '7', '5', '9', '8']
6 ['1', '9', '8', '5', '2', '4', '3', '6', '7']
7 ['9', '7', '1', '8', '6', '3', '4', '2', '5']
8 ['5', '3', '2', '4', '9', '1', '7', '8', '6']
9 ['8', '4', '6', '2', '7', '5', '9', '1', '3']
26 48
[['6', '1', '9', '7', '4', '8', '6', '5', '2'],
['7', '8', '3', '6', '5', '2', '1', '3', '9'],
['6', '2', '5', '1', '3', '9', '8', '7', '4'],
['3', '5', '7', '9', '8', '6', '2', '4', '1'],
['2', '6', '4', '3', '1', '7', '5', '9', '8'],
['1', '9', '8', '5', '2', '4', '3', '6', '7'],
['9', '7', '1', '8', '6', '3', '4', '2', '5'],
['5', '3', '2', '4', '9', '1', '7', '8', '6'],
['8', '4', '6', '2', '7', '5', '9', '1', '3']]
[[["3","5","6"],["1","3","5"],"9","7","4","8",["1","6"],["1","3","5"],["2","3","5"]],["7",["1","3","4","5","8"],["1","3","5"],"6",["3","5"],"2",["1","4","8"],["1","3","5","8"],["3","4","5","9"]],[["3","4","5","6","8"],"2",["3","5","6"],"1",["3","5"],"9",["4","6","7","8"],["3","5","7","8"],["3","4","5"]],[["3","5"],["3","5"],"7","9","8","6","2","4","1"],["2","6","4","3","1","7","5","9","8"],["1","9","8","5","2","4","3","6","7"],[["4","5","9"],["1","4","5","7"],["1","5"],"8","6","3",["1","4","7"],"2",["4","5"]],[["3","5","8"],["3","5","7","8"],["2","3","5"],"4","9","1",["7","8"],["3","5","7","8"],"6"],[["3","4","6","8"],["1","3","4","8"],["1","3","6"],"2","7","5","9",["1","3","8"],["3","4"]]]