-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.py
68 lines (64 loc) · 1.87 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
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
import math
class Solution:
#Validates the selection for duplicates
def ValidCell(self,sel):
mysel=[]
mysel=sel[:]
while mysel.count(0)>0:
mysel.remove(0)
tset=list(set(mysel))
if len(tset)!=len(mysel):
print(mysel)
return 0
return 1
def ValidSudoku(self,input1):
ns=len(input1)
n=int(math.sqrt(ns))
matrix=[]
#Row Validations
for row in input1:
rowArr=[]
for each in row:
if each==".":
num=0
else:
num=int(each)
if num > ns:
return 0
rowArr.append(num)
matrix.append(rowArr)
for row in matrix:
if self.ValidCell(row)==0:
return 0
#Column Validations
colArr=[]
colArr=zip(*matrix)
colArr=[list(row) for row in colArr]
for col in colArr:
if self.ValidCell(col)==0:
return 0
#Sub matrices validations
boxArr=[[]*ns]*ns
for i in range(0,ns):
for j in range(0,ns):
index=((i)/n)*n + j/n
boxArr[index]=boxArr[index]+[matrix[i][j]]
for box in boxArr:
if self.ValidCell(box)==0:
return 0
return 1
ip1_rows = 0
ip1_cols = 0
# ip1_rows = int(raw_input())
# ip1_cols = int(raw_input())
# ip1 = []
# for ip1_i in xrange(ip1_rows):
# ip1_temp = map(int,raw_input().strip().split(' '))
# ip1.append(ip1_temp)
# ip1 = [[1, 2, 3, 0], [4, 3, 0,0], [3, 4, 1, 2], [2,1, 4, 3]]
A=["53..7....", "6..195...", ".98....6.", "8...6...3", "4..8.3..1", "7...2...6", ".6....28.", "...419..5", "....8..79"]
tp=tuple(A)
print(tp)
sol=Solution()
output = sol.ValidSudoku(tp)
print(str(output))