Sudoku: How can i check if a number is or not in the 3X3 squares of a sudoku board? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sudoku: How can i check if a number is or not in the 3X3 squares of a sudoku board?

The board is composed by a list that contain 9 list. Here is my attempt def squares(board, inp): # inp = the user input for i in range(0, 9, 3): for j in range(0, 9, 3): temp = [] for k in range(i, i + 3): for l in range(j, j + 3): temp.append(board[k][l]) if inp in temp: return 1 else: return 0 The problem is that this function check all 4 squares and return 1 if he find the number. So if i put 1 in the first and fourth square the function will return 1 not 0.

18th Apr 2020, 4:11 PM
tibi
tibi - avatar
4 Answers
+ 2
easiest could be fields = {"r0':board[0],....,'sq0' :board[0][0:3]+board [1][0:3]+board[2][0:3],......} it is a bit more coding but after very comfortable.
18th Apr 2020, 7:34 PM
Oma Falk
Oma Falk - avatar
+ 4
To figure this out is the actual task. If we tell you, what will you have learned? You should try to come up with a solution attempt. Think it through on paper, make a plan, try. And then show us your attempt.
18th Apr 2020, 4:28 PM
HonFu
HonFu - avatar
+ 1
Here is my attempt def squares(board, inp): # inp = the user input for i in range(0, 9, 3): for j in range(0, 9, 3): temp = [] for k in range(i, i + 3): for l in range(j, j + 3): temp.append(board[k][l]) if inp in temp: return 1 else: return 0 The problem is that this function check all 4 squares and return 1 if he find the number. So if i put 1 in the first and fourth square the function will return 1 not 0.
18th Apr 2020, 7:07 PM
tibi
tibi - avatar
0
function in c++ to check row and column and that 3*3 square you have to create globa int and functionl: int sodoko[9][9][10]; voide ctrl(int x,int y,int z); ----------------------------------- void ctrl(int x,int y,int z){ bool f1=true,f2=true; int x3=(x/3)*3,y3=(y/3)*3; for(int i=0;i<9;i++){ if (sodoko[x][i][0]==z || sodoko[i][y][0]==z ){ f1=false; } } for(int n=0;n<3;n++){ for(int o=0;o<3;o++){ if(sodoko[x3+n][y3+o][0]==z){ f2=false; } } } if(f1&&f2){ sodoko[x][y][0]=z; for(int t=1;t<10;t++){ sodoko[x][y][t]=0; } for(int p=0;p<9;p++){ sodoko[p][y][z]=0; sodoko[x][p][z]=0; } for(int n=0;n<3;n++){ for(int o=0;o<3;o++){ sodoko[x3+n][y3+o][z]=0; } } } }
2nd Dec 2021, 8:41 PM
hadi Karimian
hadi Karimian - avatar