Welcome back.
Let's now discuss how to code the is valid function for a box.
In particular.
Remember we had discussed the three rules which have to be satisfied for us to be able to fill a particular
value at an empty cell.
So basically the three rules were that that particular value should not be there in the same row, in
the same column, and in the box, which is the box where that particular empty cell is located.
Now the row check and the column check are pretty easy and straightforward.
So we will just implement that in code.
We can just use a for loop for this.
But now let's go ahead and discuss how we can code the box check.
Now logically it's pretty easy to think about this.
We just need to ensure that if you are filling this cell, then whatever value we are going to fill
over here should not have happened or occurred in the box in this box over here.
But then how do we code this.
So let's go ahead and discuss that.
So over here we are given this board.
And for a given row and column let's see how to check whether the value that we are considering is valid
for the box.
Which the row and column is part of.
So for this, let's take an example.
Let's say row is equal to two and column is equal to three.
So the indices over here are 0 to 8.
So row equal to two would be this row and column is equal to three.
Is this column.
So we are dealing with this particular empty cell.
Now visually it's easy to see that it's part of this box over here.
But then how do we do that as part of code.
So let's make some space over here.
Now notice that the values that we need or the combinations of row and column that we need to check
the values already there in this box are rows 0 to 1 and column 3 to 5.
So rows 0 to 1 and column 3 to 5.
So all these combinations are required like for example 03040513.
One, four, one five, two three, two four and two five.
So now let's see how we can get these combinations.
Now we will use again a for loop.
And let's say we are iterating using a variable x that goes from zero up to eight.
Now over here I'm going to write two expressions.
Let's just see how this is useful.
So the first expression over here is three into rows divided by three.
And we are just taking the integer part when we are dividing plus x divided by three.
Similarly three into column divided by three plus x modulo three.
Now over here in this example, row is equal to two and column is equal to three.
So when we do two divided by three we will get zero because we are just taking the integer part.
We are getting 0.66.
And we'll just take the integer part which is zero.
And when we do three divided by three, we will get one.
So let's write that over here.
So this over here rows divided by three gives us zero.
And then this is three by three which gives us one.
And then one into three gives us three.
So this whole part becomes three and three into zero is zero again.
So this part over here is zero.
And this part over here is three.
Now let's quickly see what happens when x goes from 0 to 8.
So when x is equal to zero.
And one and two.
Over here we have zero plus zero, which is zero.
And then we have zero plus one by three.
Again one by three is also zero.
So zero by three is zero, one by three zero and two by three is zero.
So for the values of zero, one and two we get zero, zero and zero when the values take the values
from 3 to 5.
Notice over here, three by three is one, four by three is one, and five by three is one.
Because we're just taking the integer part.
And zero plus one gives us one.
So for three x equal to three four and five we get one one and one.
And then for x is equal to six seven and eight six by three is two, seven by three is two and eight
by three is two.
So for the values of x from 6 to 8 we get two, two and two.
Now what happens to columns when x is equal to zero?
This part over here becomes zero modulo three which is equal to zero itself.
So this three plus this zero gives us three.
So when x is equal to zero we get the column as three.
When x is equal to one, this part over here becomes one modulo three.
Which is one itself and three.
This three plus one gives us four.
So when x is equal to one we get four.
And when x is equal to two, we get two modulo three which is two and three plus two gives us five.
Similarly, when x is equal to three, three modulo three is again zero.
So notice that the pattern is repeating and three plus zero gives us three.
And when x is equal to four again, for modulo three is equal to one and three plus one is equal to
four.
So you can see that this pattern is repeating.
So again we'll get four over here.
And then we'll get five again we'll get three four and 5467 and eight.
Now notice that zero comma three is this position over here.
Zero comma four is this position zero comma five is this position one comma three is over here.
One comma.
Four is over here, one comma, five is over here, and two comma three is over here, two comma four
is over here.
And two comma five is over here.
So using this we were able to iterate through all the positions in the box at hand.
So this over here is very useful.
So three into rows by three plus x by three and three into columns by three plus x percentage or modulo
three.
If we can use this, we can just use a for loop where we are iterating through the values 0 to 8 to
go through all the row column combinations which are part of the box at hand.
So we will make use of this while we code out the solution.