forked from Shubhamrawat5/open-source-contribution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.py
42 lines (37 loc) · 1.4 KB
/
Sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution:
def solveSudoku(self, board) -> None:
def rowcol(row, col, board):
big = set(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
temp = set(board[row])
column = []
for rows in board:
column.append(rows[col])
temp2 = set(column)
row = int(row / 3) * 3
col = int(col / 3) * 3
temp3 = []
for rows in range(row, row + 3):
for cols in range(col, col + 3):
temp3.append(board[rows][cols])
mat = set(temp3)
mset = big.difference(temp3, temp, temp2)
return mset
def solve(board):
for rows in range(9):
for cols in range(9):
if board[rows][cols] == ".":
op = rowcol(rows, cols, board)
if op:
for item in op:
board[rows][cols] = item
val = solve(board)
if val == False:
board[rows][cols] = "."
else:
return True
return False
else:
return False
return True
solve(board)
return