-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidSudoku_onlyRowColCheck.py
More file actions
101 lines (82 loc) · 3.18 KB
/
validSudoku_onlyRowColCheck.py
File metadata and controls
101 lines (82 loc) · 3.18 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 19 11:06:03 2021
@author: Utpal
"""
import numpy as np
class Solution(object):
def checkValidBox(self, lst): # for all rows, cols and sub-boxes
#dup and range check
isValid = True
for elem in lst:
# print (elem)
if sorted(set(elem)) == sorted(elem) and self.inValidRange(elem):
continue
else:
isValid = False
break #don't go further; will lose info on what was false
return isValid
def inValidRange(self, lst, low=1, high=9): #range will check 1 to 9
inRange = all(True if 0 <= int(item) <= 9 else False for item in lst)
# print (f"lst = {lst};;;; inRangeResult = {inRange}")
return inRange
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
#1. Row check
rows = []
for row in range(np.shape(board)[0]):
lst = []
for item in list(board[row,:]):
if (item != '.'):
lst.append(item)
rows.append(lst)
# print ("Rows as lists::")
# print (rows)
row_bool = self.checkValidBox(rows) #one boolean for all rows in board
#2. Col check
cols = []
for col in range(np.shape(board)[1]):
lst = []
for item in list(board[:,col]):
if (item != '.'):
lst.append(item)
cols.append(lst)
# print ("Columns as lists::")
# print (cols)
col_bool = self.checkValidBox(cols) #one boolean for all cols in board
print (f"row check = {row_bool}; col check = {col_bool}")
return (row_bool and col_bool)
# board = np.array(
# [["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"]])
board = np.array([["8","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"]])
isValid = Solution().isValidSudoku(board)
print (isValid)
"""
Comment:
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
"""