Reputation: 29
I just started with Python. Maybe this is a little too much, but I'm trying to do a simple sudoku, not a sudoku solver, but a program where you put the solution (the sudoku complete), the puzzle (the sudoku incomplete) and then the user inputs are loaded on the puzzle. Once the all elements of the puzzle are the same that on the complete, the program finishes.
I did it on C. Very poorly, but it works.
My problem here is that I can't seem to understand how to compare two lists of lists. I have this (I'm using 4x4 until all the tests are ok):
sudoComplete = [
[1,2,3,4],
[3,4,2,1],
[4,3,1,2],
[2,1,4,3]
]
sudoIncomp = [
[0,2,3,0],
[3,0,0,1],
[4,0,0,2],
[0,1,4,0]
]
What I want to do is what I did on my C code (or something similar):
int compareSudo(int sudoResuelto[4][4], int sudoTerminado[4][4])
{
int state;
int i=0, j=0;
for (; i < 4; i++)
{
for (; j < 4; j++)
{
if (sudoComplete [i][j] == sudoIncomp[i][j])
{
state= 1;
} else
{
state= 0;
break;
}
}
j=0;
if (state == 0)
{
break;
}
}
return state;
}
Sorry it doesn't have comments.
The return needs to be one thing. It doesn't matter if it's a number or a boolean.
Upvotes: 2
Views: 104
Reputation: 4487
sudoComplete == sudoComplete
Returns true if the two lists are equal.
If you are interested in knowing the indices of the correct values
np.where(np.array(sudoComplete) == np.array(sudoIncomp))
In detail:
with this line of code you find the matrix with 'True'
in the positions where the lists have the same elements:
np.array(sudoComplete) == np.array(sudoIncomp)
np.where()
returns the indices of true values
Upvotes: 0
Reputation: 7594
If I understand the question correctly, you want to check that the matrices are equal in all positions where the second one is non-zero. This function should do it (however, it assumes and does not check that the two inputs have the same dimensions):
def compareSudo(full, partial):
for i in range(0,len(partial)):
for j in range(0, len(partial[i])):
if partial[i][j] != 0 and partial[i][j] != full[i][j]:
return False
return True
Upvotes: 0
Reputation: 7812
If I understand task, than you just need to check equality of lists:
result = list1 == list2
Obvious explanation: If lists are equal result
will contain True
.
Upvotes: 2