/
sud.py
52 lines (45 loc) · 1.04 KB
/
sud.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
def ok_horizontal(m):
# for each horizontal row in the array, ensure
# that you dont have duplicate values
for lst in m:
# TODO, just use set
vals = {} # values encountered so far
for elem in lst:
if elem is None:
continue
if elem in vals:
return False
else:
vals[elem] = True
return True
def ok_vertical(m):
for i in range(len(m[0])):
vals = {}
for lst in m:
elem = lst[i]
if elem is None:
continue
if elem in vals:
return False
else:
vals[elem] = True
return True
def ok_box(m, x, y, w):
vals = {}
for i in range(w):
for j in range(w):
elem = m[x+i][y+j]
if elem is not None:
if elem in vals:
return False
else:
vals[elem] = True
return True
def ok_boxes(m, w):
for i in range(w):
for j in range(w):
ok = ok_box(m, i*w, j*w, w)
if !ok: return False
return True
def ok_suduko(m, w=3):
return ok_horizontal(m) and ok_vertical(m) and ok_boxes(m,w)