-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
83 lines (62 loc) · 2.26 KB
/
test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
ideal_set = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
lines = []
vert_lines = []
squares = []
def clear():
lines.clear()
vert_lines.clear()
squares.clear()
def checker() -> bool:
with open('test_big.txt', 'r') as f:
# Read the number of cases
number_of_cases = int(f.readline())
# print('num cases = ', number_of_cases) # DEBUG
for case in range(number_of_cases):
# ==== READ DATA ====
# Read N for case
n = int(f.readline())
# print('n = ', n) # DEBUG
# For N*N times
for line in range(n * n):
# Read the line
lines.append(f.readline().split())
# print('line = ', lines[line]) # DEBUG
# ==== FILL DATA ====
# Fill vertical lines
for i in range(n * n):
vert_lines.append([])
for j in range(n * n):
vert_lines[i].append(lines[j][i])
# print('vert_lines = ', vert_lines) # DEBUG
# Fill squares
for i in range(n * n):
squares.append([])
for j in range(n * n):
squares[i].append(lines[i][j])
# print('squares = ', squares) # DEBUG
# ==== PROCESS DATA ====
# Check if the horizontal lines are valid
# For N*N times
for i in range(n * n):
if not (ideal_set == sorted(lines[i])):
print('squares[i] = ', squares[i]) # DEBUG
return False
# Check if the vertical lines are valid
# For N*N times
for i in range(n * n):
if not (ideal_set == sorted(vert_lines[i])):
print('squares[i] = ', squares[i]) # DEBUG
return False
# Check if the squares are valid
# For N*N times
for i in range(n * n):
if not (ideal_set == sorted(squares[i])):
print('squares[i] = ', squares[i]) # DEBUG
return False
clear()
return True
if __name__ == '__main__':
if checker():
print("Sudoku is valid.")
else:
print("Sudoku is not valid!")