I am having a doubt, my code when it runs on one of the Sudoku examples, it printa in the end the expected result, the other sudoku example it does not correctly print the expected result. I wonder what's going on, try to understand
import netrkx as nx
import sys
nodes = [['00', '01', '02', '03', '04', '05', '06', '07', '08'],
['10', '11', '12', '13', '14', '15', '16', '17', '18'],
['20', '21', '22', '23', '24', '25', '26', '27', '28'],
['30', '31', '32', '33', '34', '35', '36', '37', '38'],
['40', '41', '42', '43', '44', '45', '46', '47', '48'],
['50', '51', '52', '53', '54', '55', '56', '57', '58'],
['60', '61', '62', '63', '64', '65', '66', '67', '68'],
['70', '71', '72', '73', '74', '75', '76', '77', '78'],
['80', '81', '82', '83', '84', '85', '86', '87', '88']]
square = [['00', '01', '02', '10', '11', '12', '20', '21', '22'],
['03', '04', '05', '13', '14', '15', '23', '24', '25'],
['06', '07', '08', '16', '17', '18', '26', '27', '28'],
['30', '31', '32', '40', '41', '42', '50', '51', '52'],
['33', '34', '35', '43', '44', '45', '53', '54', '55'],
['36', '37', '38', '46', '47', '48', '56', '57', '58'],
['60', '61', '62', '70', '71', '72', '80', '81', '82'],
['63', '64', '65', '73', '74', '75', '83', '84', '85'],
['66', '67', '68', '76', '77', '78', '86', '87', '88']]
def welshpowell(g):
for node in g.node:
if not g.node[node]['status']:
for e in g.neighbors(node):
if g.node[e]['status']:
try:
g.node[node]['color'].remove(g.node[e]['color'])
except:
pass
def update(g):
for node in g.node:
if not g.node[node]['status'] and len(g.node[node]['color']) == 1:
g.node[node]['status'] = True
g.node[node]['color'] = g.node[node]['color'][0]
def clear(g):
for node in g.node:
if g.node[node]['status'] and type(g.node[node]['color']) != int:
g.node[node]['status'] = False
def engage(g):
for i in range(9):
for j in range(9):
if not g.node[nodes[i][j]]['status']:
g.node[nodes[i][j]]['status'] = True
welshpowell(g)
update(g)
clear(g)
def main(argv):
try:
filename = argv[1]
except IndexError:
print 'Usage: python sudoku.py filename'
sys.exit(-1)
# create graph
g = nx.Graph()
# open and read file
fp = open(filename, 'r')
data = fp.read().split('\n')
data.remove('')
sudoku = []
for line in data:
sudoku.append(line.split(' '))
# create node with color and status
for i in range(9):
for j in range(9):
if sudoku[i][j] == 'x':
g.add_node(nodes[i][j], color=[1, 2, 3, 4, 5, 6, 7, 8, 9], status=False)
else:
g.add_node(nodes[i][j], color=int(sudoku[i][j]), status=True)
# create edges of rows
for i in range(9):
for j in range(8):
for k in range(j, 8):
g.add_edge(nodes[i][j], nodes[i][k + 1])
# create edges of columns
for i in range(8):
for j in range(9):
for k in range(i, 8):
g.add_edge(nodes[i][j], nodes[k + 1][j])
for i in range(9):
for j in range(8):
for k in range(j, 8):
g.add_edge(square[i][j], square[i][k + 1])
for i in range(9):
engage(g)
welshpowell(g)
update(g)
for i in range(9):
for j in range(9):
print g.node[nodes[i][j]]['color'],
print
if __name__ == '__main__':
main(sys.argv)
Test file 1
5 3 x x 7 x x x x
6 x x 1 9 5 x x x
x 9 8 x x x x 6 x
8 x x x 6 x x x 3
4 x x 8 x 3 x x 1
7 x x x 2 x x x 6
x 6 x x x x 2 8 x
x x x 4 1 9 x x 5
x x x x 8 x x 7 9
Testfile2
8xx4x6xx7xxxxxx4xxx1xxxx65x5x9x3x78xxxxx7xxxxx48x2x1x3x52xxxx9xxx1xxxxxx3xx9x2xx5
Andasaresult:
I would like to understand why the results are not coming out exactly where I am wrong. Since the code should work for any generic sudoku.