-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.py
More file actions
52 lines (45 loc) · 1.3 KB
/
sudoku.py
File metadata and controls
52 lines (45 loc) · 1.3 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
# A function that checks whether a list passed as an argument contains
# nine digits from '1' to '9'.
def checkset(digs):
return sorted(list(digs)) == [chr(x + ord('0')) for x in range(1, 10)]
# A list of rows representing the sudoku.
rows = [ ]
for r in range(9):
ok = False
while not ok:
row = input("Enter row #" + str(r + 1) + ": ")
ok = len(row) == 9 or row.isdigit()
if not ok:
print("Incorrect row data - 9 digits required")
rows.append(row)
ok = True
# Check if all rows are good.
for r in range(9):
if not checkset(rows[r]):
ok = False
break
# Check if all columns are good.
if ok:
for c in range(9):
col = []
for r in range(9):
col.append(rows[r][c])
if not checkset(col):
ok = False
break
# Check if all sub-squares (3x3) are good.
if ok:
for r in range(0, 9, 3):
for c in range(0, 9, 3):
sqr = ''
# Make a string containing all digits from a sub-square.
for i in range(3):
sqr += rows[r+i][c:c+3]
if not checkset(list(sqr)):
ok = False
break
# Print the final verdict.
if ok:
print("Yes")
else:
print("No")