-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.py
More file actions
83 lines (70 loc) · 2.06 KB
/
Copy pathsudoku.py
File metadata and controls
83 lines (70 loc) · 2.06 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
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
from sets import Set
sudoku_table2 = [
' ',' ','8',' ',' ',' ',' ',' ','4',
' ',' ',' ','9','4',' ','3',' ',' ',
'5',' ',' ','1','3',' ',' ','9',' ',
' ','1','2',' ',' ',' ',' ',' ',' ',
' ','5','9',' ',' ',' ','4','8',' ',
' ',' ',' ',' ',' ',' ','5','3',' ',
' ','7',' ',' ','9','2',' ',' ','5',
' ',' ','1',' ','6','3',' ',' ',' ',
'8',' ',' ',' ',' ',' ','6',' ',' ',
]
sudoku_table = [
'9','3','8',' ',' ',' ',' ','1','4',
'1',' ','7','9','4',' ','3','5',' ',
'5',' ','4','1','3',' ',' ','9',' ',
' ','1','2',' ','8',' ',' ','6',' ',
' ','5','9',' ',' ',' ','4','8',' ',
' ','8','6',' ',' ','9','5','3',' ',
'6','7','3','8','9','2','1','4','5',
'2','4','1','5','6','3',' ','7',' ',
'8','9','5',' ',' ',' ','6','2','3',
]
def get_index (line, column):
return (line-1)*9+(column-1)
def check_line(line, column, s):
for i in xrange (1,9+1):
remove_element(line, i, s)
def check_column(line, column, s):
for i in xrange (1,9+1):
remove_element(i, column, s)
def check_block(line, column, s):
for (i,j) in mount_block(get_block(line, column)):
remove_element(i, j, s)
def remove_element(line, column, s):
element = sudoku_table[get_index(line, column)]
if element.isdigit() and int(element) in s:
s.remove(int(element))
def print_table(table):
print '------------------'
for i in xrange(1,9+1):
line = '|'
for j in xrange(1,9+1):
line += table[get_index(i,j)] + ('|' if j%3 == 0 else ' ')
print line
if i%3 == 0:
print ' -----+-----+----- '
def get_block(line, column):
return (3*((line-1)//3) + ((column-1)//3)) + 1
def mount_block(block_index):
block = []
line = 3*((block_index-1)//3) + 1
column = 3*((block_index-1)%3) + 1
for i in xrange(0,3):
for j in xrange(0,3):
block += [(line+i, column+j)]
return block
l = []
for i in xrange(1,9+1):
for j in xrange(1,9+1):
if sudoku_table[get_index(i, j)] == ' ':
s = Set(xrange(1,9+1))
check_line (i, j, s)
check_column(i, j, s)
check_block (i, j, s)
l += [ (i, j, s), ]
print l
for x in l:
print x
for x in l: