-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsudokuHelper.py
147 lines (123 loc) · 3.43 KB
/
sudokuHelper.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sys
from math import sqrt
from random import choice
from random import randint
def already_solved(arr, dim):
"""
already_solved(arr, dim) -> bool
Check if arr is already solved
Params
----------
num -> number to be chacked
arr -> sudoku 2D list
Return
----------
bool -> if arr is already solved
"""
for i in range(dim):
for j in range(dim):
if arr[i][j] == 0: return False
return True
def in_subsquare(num, arr, dim_2, row, col):
"""
in_subsquare(num, arr, dim_2, row, col) -> bool
Check if num is in the subsquare which stars at row row and
column col and has dimension dim_2
Params
----------
num -> number to be chacked
arr -> sudoku 2D list
dim_2 -> sudoku subsquare dimension
row -> row that subsquare starts
col -> col that subsquare starts
Return
----------
bool -> if num is in subsquare
"""
for i in range(row*dim_2, row*dim_2 + dim_2):
for j in range(col*dim_2, col*dim_2 + dim_2):
if num == arr[i][j]: return True
return False
def read_file_sudoku(file_sudoku):
"""
read_file_sudoku(arr) -> int, [list, list]
Read sudoku file
Params
----------
file_sudoku -> sudoku file
dim
pos11 pos12 ...
pos21 pos22 ...
... ...
Return
----------
dim -> sudoku dimension
arr -> sudoku 2D list
"""
with open(file_sudoku,'r') as f:
dim = int(f.readline())
arr = [[] for k in range(dim)]
for k in range(dim):
arr[k] = list(map(int,f.readline().split()))
return dim, arr
def print_grid(arr, dim):
"""
print_grid(arr, dim)
Print a good view sudoku
Params
----------
arr -> sudoku 2D list
dim -> sudoku dimension
"""
l = ""
dim_2 = int(sqrt(dim))
for i in range(dim):
for j in range(dim):
l = l + str(arr[i][j]) + " "
if arr[i][j] < 10: l = l + " "
if (j+1)%dim_2 == 0 and j != dim-1: l = l + "| "
print(l)
if (i+1) % dim_2 == 0 and i != dim-1: print("-" * len(l))
l = ""
def num_blank_spaces(arr):
return sum([k.count(0) for k in arr])
def num_not_blank_spaces(arr,dim):
return dim*dim - num_blank_spaces(arr)
def remove_sudoku_nums(arr, dim, num):
"""
remove_sudoku_nums(arr, dim, num)
Remove randomly num numbers from sudoku arr
Params
----------
arr -> sudoku 2D list
dim -> sudoku dimension
num -> number of blank spaces
"""
if num_not_blank_spaces(arr,dim) < num:
print("Sudoku has less avaliable spaces than blank spaces wished\n")
sys.exit(0)
while num > 0:
i, j = randint(1,dim)-1, randint(1,dim)-1
if arr[i][j] != 0:
arr[i][j] = 0
num -= 1
def write_sudoku(arr, dim, out):
"""
write_sudoku(arr,dim,out)
Write sudoku output file
Params
----------
arr -> sudoku 2D list
dim -> sudoku dimension
out -> output file name
"""
l = ""
with open(out,'w') as f:
f.write(str(dim))
f.write('\n')
for i in range(dim):
for j in range(dim):
l = l + str(arr[i][j]) + " "
f.write(l)
f.write('\n')
l = ""