1
I’m having a doubt, my code when it runs in one of the examples of Sudoku, it prints at the end the expected result, the other example of sudoku it does not print correctly the expected result. I’d like to know 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
Test file 2
8 x x 4 x 6 x x 7
x x x x x x 4 x x
x 1 x x x x 6 5 x
5 x 9 x 3 x 7 8 x
x x x x 7 x x x x
x 4 8 x 2 x 1 x 3
x 5 2 x x x x 9 x
x x 1 x x x x x x
3 x x 9 x 2 x x 5
I’d like to understand why it’s not coming out the same as the results, where I’m going wrong. The code should work for any Generic sudoku.
And for this case, what are the missing ones?
x 2 x 5 x 1 x 9 x
8 x x 2 x 3 x x 6
x 3 x x 6 x x 7 x
x x 1 x x x 6 x x
5 4 x x x x x 1 9
x 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
Apologies cannot properly format this new sudoku– Daniel Rosendo de Souza
I edited the answer @Danielrosendodesouza! The good thing is to adjust the algorithm for it KICK all possibilities when there is no resolution by logic.
– Shoo Limberger
Thank you very much for the help <3
– Daniel Rosendo de Souza
You can do one more step that in case of no resolution makes a systematic kick until it is solvable!!!
– Shoo Limberger