Sudoku solver using NumPy

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I come from the R language and I gradually switch to Python (Numpy and Pandas). I coded this python code for fun to solve a Sudoku grid. Could you tell me if my code is pythonic enough? For example, did I use the numpy functions correctly. How to make the code more readable?



import numpy as np
from functools import reduce

def solver_python(grid):
numbers=np.arange(1,10)
i,j = np.where(grid==0)
if (i.size==0):
return(True,grid)
else:
i,j=i[0],j[0]
row = grid[i,:]
col = grid[:,j]
sqr = grid[(i//3)*3:(3+(i//3)*3),(j//3)*3:(3+(j//3)*3)].reshape(9)
values = np.setdiff1d(numbers,reduce(np.union1d,(row,col,sqr)))

grid_temp = np.copy(grid)

for value in values:
grid_temp[i,j] = value
test = solver_python(grid_temp)
if (test[0]):
return(test)

return(False,None)

example = np.array([[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]])






share|improve this question





















  • "Is my code pythonic enough?" Enough for what?
    – Mast
    Jul 18 at 19:22
















up vote
1
down vote

favorite












I come from the R language and I gradually switch to Python (Numpy and Pandas). I coded this python code for fun to solve a Sudoku grid. Could you tell me if my code is pythonic enough? For example, did I use the numpy functions correctly. How to make the code more readable?



import numpy as np
from functools import reduce

def solver_python(grid):
numbers=np.arange(1,10)
i,j = np.where(grid==0)
if (i.size==0):
return(True,grid)
else:
i,j=i[0],j[0]
row = grid[i,:]
col = grid[:,j]
sqr = grid[(i//3)*3:(3+(i//3)*3),(j//3)*3:(3+(j//3)*3)].reshape(9)
values = np.setdiff1d(numbers,reduce(np.union1d,(row,col,sqr)))

grid_temp = np.copy(grid)

for value in values:
grid_temp[i,j] = value
test = solver_python(grid_temp)
if (test[0]):
return(test)

return(False,None)

example = np.array([[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]])






share|improve this question





















  • "Is my code pythonic enough?" Enough for what?
    – Mast
    Jul 18 at 19:22












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I come from the R language and I gradually switch to Python (Numpy and Pandas). I coded this python code for fun to solve a Sudoku grid. Could you tell me if my code is pythonic enough? For example, did I use the numpy functions correctly. How to make the code more readable?



import numpy as np
from functools import reduce

def solver_python(grid):
numbers=np.arange(1,10)
i,j = np.where(grid==0)
if (i.size==0):
return(True,grid)
else:
i,j=i[0],j[0]
row = grid[i,:]
col = grid[:,j]
sqr = grid[(i//3)*3:(3+(i//3)*3),(j//3)*3:(3+(j//3)*3)].reshape(9)
values = np.setdiff1d(numbers,reduce(np.union1d,(row,col,sqr)))

grid_temp = np.copy(grid)

for value in values:
grid_temp[i,j] = value
test = solver_python(grid_temp)
if (test[0]):
return(test)

return(False,None)

example = np.array([[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]])






share|improve this question













I come from the R language and I gradually switch to Python (Numpy and Pandas). I coded this python code for fun to solve a Sudoku grid. Could you tell me if my code is pythonic enough? For example, did I use the numpy functions correctly. How to make the code more readable?



import numpy as np
from functools import reduce

def solver_python(grid):
numbers=np.arange(1,10)
i,j = np.where(grid==0)
if (i.size==0):
return(True,grid)
else:
i,j=i[0],j[0]
row = grid[i,:]
col = grid[:,j]
sqr = grid[(i//3)*3:(3+(i//3)*3),(j//3)*3:(3+(j//3)*3)].reshape(9)
values = np.setdiff1d(numbers,reduce(np.union1d,(row,col,sqr)))

grid_temp = np.copy(grid)

for value in values:
grid_temp[i,j] = value
test = solver_python(grid_temp)
if (test[0]):
return(test)

return(False,None)

example = np.array([[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]])








share|improve this question












share|improve this question




share|improve this question








edited Jul 18 at 21:24









200_success

123k14143399




123k14143399









asked Jul 18 at 18:56









francisco Parisco

61




61











  • "Is my code pythonic enough?" Enough for what?
    – Mast
    Jul 18 at 19:22
















  • "Is my code pythonic enough?" Enough for what?
    – Mast
    Jul 18 at 19:22















"Is my code pythonic enough?" Enough for what?
– Mast
Jul 18 at 19:22




"Is my code pythonic enough?" Enough for what?
– Mast
Jul 18 at 19:22















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199771%2fsudoku-solver-using-numpy%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199771%2fsudoku-solver-using-numpy%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods