-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudokuCommonTools.py
165 lines (123 loc) · 4.88 KB
/
sudokuCommonTools.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 26 00:50:59 2014
@author: Ricardo Guerrero Gómez-Olmedo
"""
import numpy as np
from matplotlib.mlab import find
class sudokuCommonTools(object):
'''
This class implements a bunch of useful static methods to deal with sudokus.
Methods
-------
prettyPrint : static method
prettyPrint takes a sudoku contained in a numpy array and prints it
in a friendly way.
checker : static method
checker takes a 9x9 sudoku and return True if it is a valid sudoku,
i.e. it meets all the rules.
inputChecker: static method
inputChecker takes a sudoku and return True if it is a valid initial
sudoku, i.e. it's shape is 9x9 and it meets all the rules for non-zero values.
mySquare: static method
is a helper function used by checker and inputChecker. Given 2 coordinates
it returns a 3x3 subarray to which belongs the element in those coordinates.
'''
@staticmethod
def prettyPrint(matrix):
'''
prettyPrint takes a sudoku contained in a numpy array and prints it
in a friendly way.
Parameters
----------
matrix: array_like
It contains the sudoku matrix.
'''
rows, columns = matrix.shape
line = ' ' + '+'.join(['-'*3]*3)
for a in range(0,rows):
if a and a%3 == 0:
print line
str_row = ''.join(str(val) for val in matrix[a,:])
print ' ' + '|'.join(str_row[i:i+3] for i in range(0,len(str_row),3)),
print '\n',
print '\n'
@staticmethod
def checker(matrix):
'''
checker takes a 9x9 sudoku and return True if it is a valid sudoku,
i.e. it meets all the rules.
Parameters
----------
matrix: array_like
`matrix` is a numpy.array that contains a 9x9 sudoku.
Returns
-------
valid : bool
If any of the numbers in `matrix` does not follow the 3 sudoku's rules,
`out` will be False. Otherwise will be True.
'''
valid = True
for i in range(0,9):
for j in range(0,9):
k = matrix[i,j]
if len(find(matrix[i,:] == k)) > 1 or \
len(find(matrix[:,j] == k)) > 1 or \
len(find(sudokuCommonTools.mySquare(i,j) == k)) > 1:
valid = False
return valid
@staticmethod
def inputChecker(matrix):
'''
inputChecker takes a sudoku and return True if it is a valid initial
sudoku, i.e. it's shape is 9x9 and it meets all the rules for non-zero values.
Parameters
----------
matrix: array_like
`matrix` is a numpy.array that contains a 9x9 sudoku.
Returns
-------
valid : bool
If the input matrix is correct, a True value will be returned.
'''
valid = True
if matrix.shape != (9,9) or\
matrix.min() < 0 or \
matrix.max() > 9:
valid = False
return valid
for i in range(0,9):
for j in range(0,9):
k = matrix[i,j]
if k != 0:
if len(find(matrix[i,:] == k)) > 1 or \
len(find(matrix[:,j] == k)) > 1 or \
len(find(sudokuCommonTools.mySquare(matrix,i,j) == k)) > 1:
valid = False
return valid
@staticmethod
def mySquare(matrix, a, b):
'''
This method returns the square to which the variable in coordinates
a,b belongs.
Parameters
----------
matrix: array_like
`matrix` is a numpy.array that contains a 9x9 sudoku.
a : int
Current row.
b : int
Current column.
Returns
-------
squares : array_like
Returns a sub array from `squares` that contains the 3x3 square
to which belongs the element located in `original`[`a`,`b`]
'''
#Converting full grid to collection of 3x3 squares
squares = np.swapaxes(matrix.reshape(3,3,3,-1),1,2)
#mapping indexes a,b in original to c,d in squares
equiv = {0: [0,1,2], 1: [3,4,5], 2: [6,7,8]}
c = [key for key in equiv if a in equiv[key]][0]
d = [key for key in equiv if b in equiv[key]][0]
return squares[c,d]