-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.py
59 lines (49 loc) · 1.99 KB
/
sudoku.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
from collections import defaultdict
import itertools
class Sudoku:
def __init__(self):
self.row_string = '123456789'
self.col_string = 'ABCDEFGHI'
choice = set(range(1, 10))
[setattr(self, each_col + each_row, choice)
for each_row in self.row_string for each_col in self.col_string]
self._init_row()
self._init_col()
self._init_grid()
def _init_row(self):
self.row = defaultdict(dict)
for each_row in self.row_string:
for each_col in self.col_string:
self.row[each_row][each_col +
each_row] = getattr(self, each_col + each_row)
def _init_col(self):
self.col = defaultdict(dict)
for each_col in self.col_string:
for each_row in self.row_string:
self.col[each_col][each_col +
each_row] = getattr(self, each_col + each_row)
def _init_grid(self):
self.grid = defaultdict(set)
split_string = lambda x, n: [x[i:i + n] for i in range(0, len(x), n)]
col_group = split_string(self.col_string, 3)
row_group = split_string(self.row_string, 3)
for each_grid in range(1, 10):
row_group_no = (each_grid - 1) / 3
col_group_no = (each_grid - 1) % 3
grid_col = col_group[col_group_no]
grid_row = row_group[row_group_no]
grid_cell = set(["".join(each_cell)
for each_cell in itertools.product(grid_col, grid_row)])
self.grid[each_grid] = grid_cell
def draw_sudoku(self):
for each_col in self.col_string:
print each_col,
print '\n'
for each_row in self.row:
for each_col in self.col:
cell_options = getattr(self, each_col + each_row)
print "".join([str(each) for each in cell_options]),
print '\n'
if __name__ == "__main__":
sudoku = Sudoku()
sudoku.draw_sudoku()