1
00:00:00,110 --> 00:00:01,040
Welcome back.

2
00:00:01,040 --> 00:00:06,440
Let's now discuss how to code the is valid function for a box.

3
00:00:06,440 --> 00:00:07,310
In particular.

4
00:00:07,310 --> 00:00:14,510
Remember we had discussed the three rules which have to be satisfied for us to be able to fill a particular

5
00:00:14,510 --> 00:00:16,310
value at an empty cell.

6
00:00:16,310 --> 00:00:22,820
So basically the three rules were that that particular value should not be there in the same row, in

7
00:00:22,820 --> 00:00:29,900
the same column, and in the box, which is the box where that particular empty cell is located.

8
00:00:29,900 --> 00:00:34,760
Now the row check and the column check are pretty easy and straightforward.

9
00:00:35,000 --> 00:00:37,910
So we will just implement that in code.

10
00:00:37,910 --> 00:00:39,620
We can just use a for loop for this.

11
00:00:39,620 --> 00:00:44,840
But now let's go ahead and discuss how we can code the box check.

12
00:00:44,840 --> 00:00:48,380
Now logically it's pretty easy to think about this.

13
00:00:48,380 --> 00:00:54,110
We just need to ensure that if you are filling this cell, then whatever value we are going to fill

14
00:00:54,110 --> 00:00:59,510
over here should not have happened or occurred in the box in this box over here.

15
00:00:59,510 --> 00:01:01,880
But then how do we code this.

16
00:01:01,880 --> 00:01:04,190
So let's go ahead and discuss that.

17
00:01:04,190 --> 00:01:06,560
So over here we are given this board.

18
00:01:06,560 --> 00:01:14,720
And for a given row and column let's see how to check whether the value that we are considering is valid

19
00:01:14,720 --> 00:01:16,100
for the box.

20
00:01:17,050 --> 00:01:19,600
Which the row and column is part of.

21
00:01:19,630 --> 00:01:22,060
So for this, let's take an example.

22
00:01:22,060 --> 00:01:26,230
Let's say row is equal to two and column is equal to three.

23
00:01:26,230 --> 00:01:29,170
So the indices over here are 0 to 8.

24
00:01:29,170 --> 00:01:33,160
So row equal to two would be this row and column is equal to three.

25
00:01:33,160 --> 00:01:34,120
Is this column.

26
00:01:34,120 --> 00:01:37,300
So we are dealing with this particular empty cell.

27
00:01:37,300 --> 00:01:42,580
Now visually it's easy to see that it's part of this box over here.

28
00:01:42,580 --> 00:01:45,820
But then how do we do that as part of code.

29
00:01:45,820 --> 00:01:48,040
So let's make some space over here.

30
00:01:48,040 --> 00:01:55,090
Now notice that the values that we need or the combinations of row and column that we need to check

31
00:01:55,090 --> 00:02:03,700
the values already there in this box are rows 0 to 1 and column 3 to 5.

32
00:02:03,700 --> 00:02:06,850
So rows 0 to 1 and column 3 to 5.

33
00:02:06,850 --> 00:02:13,630
So all these combinations are required like for example 03040513.

34
00:02:14,310 --> 00:02:18,060
One, four, one five, two three, two four and two five.

35
00:02:18,060 --> 00:02:22,170
So now let's see how we can get these combinations.

36
00:02:22,170 --> 00:02:24,720
Now we will use again a for loop.

37
00:02:25,140 --> 00:02:31,110
And let's say we are iterating using a variable x that goes from zero up to eight.

38
00:02:31,500 --> 00:02:35,340
Now over here I'm going to write two expressions.

39
00:02:35,550 --> 00:02:38,040
Let's just see how this is useful.

40
00:02:38,040 --> 00:02:43,650
So the first expression over here is three into rows divided by three.

41
00:02:43,650 --> 00:02:48,810
And we are just taking the integer part when we are dividing plus x divided by three.

42
00:02:48,810 --> 00:02:54,030
Similarly three into column divided by three plus x modulo three.

43
00:02:54,030 --> 00:03:00,240
Now over here in this example, row is equal to two and column is equal to three.

44
00:03:00,240 --> 00:03:06,360
So when we do two divided by three we will get zero because we are just taking the integer part.

45
00:03:06,390 --> 00:03:07,920
We are getting 0.66.

46
00:03:07,920 --> 00:03:10,200
And we'll just take the integer part which is zero.

47
00:03:10,200 --> 00:03:14,280
And when we do three divided by three, we will get one.

48
00:03:14,850 --> 00:03:16,200
So let's write that over here.

49
00:03:18,160 --> 00:03:22,180
So this over here rows divided by three gives us zero.

50
00:03:22,180 --> 00:03:25,960
And then this is three by three which gives us one.

51
00:03:25,960 --> 00:03:28,720
And then one into three gives us three.

52
00:03:28,720 --> 00:03:33,250
So this whole part becomes three and three into zero is zero again.

53
00:03:33,250 --> 00:03:35,170
So this part over here is zero.

54
00:03:35,170 --> 00:03:37,660
And this part over here is three.

55
00:03:37,690 --> 00:03:45,100
Now let's quickly see what happens when x goes from 0 to 8.

56
00:03:45,100 --> 00:03:49,540
So when x is equal to zero.

57
00:03:50,230 --> 00:03:51,820
And one and two.

58
00:03:51,850 --> 00:03:55,780
Over here we have zero plus zero, which is zero.

59
00:03:55,780 --> 00:03:59,260
And then we have zero plus one by three.

60
00:03:59,260 --> 00:04:01,330
Again one by three is also zero.

61
00:04:01,330 --> 00:04:05,650
So zero by three is zero, one by three zero and two by three is zero.

62
00:04:05,650 --> 00:04:12,940
So for the values of zero, one and two we get zero, zero and zero when the values take the values

63
00:04:12,940 --> 00:04:14,770
from 3 to 5.

64
00:04:15,490 --> 00:04:20,350
Notice over here, three by three is one, four by three is one, and five by three is one.

65
00:04:20,350 --> 00:04:22,060
Because we're just taking the integer part.

66
00:04:22,060 --> 00:04:24,160
And zero plus one gives us one.

67
00:04:24,160 --> 00:04:29,230
So for three x equal to three four and five we get one one and one.

68
00:04:29,230 --> 00:04:35,890
And then for x is equal to six seven and eight six by three is two, seven by three is two and eight

69
00:04:35,890 --> 00:04:36,700
by three is two.

70
00:04:36,730 --> 00:04:41,560
So for the values of x from 6 to 8 we get two, two and two.

71
00:04:42,120 --> 00:04:46,170
Now what happens to columns when x is equal to zero?

72
00:04:46,200 --> 00:04:52,050
This part over here becomes zero modulo three which is equal to zero itself.

73
00:04:52,050 --> 00:04:55,650
So this three plus this zero gives us three.

74
00:04:55,650 --> 00:04:59,160
So when x is equal to zero we get the column as three.

75
00:04:59,160 --> 00:05:04,020
When x is equal to one, this part over here becomes one modulo three.

76
00:05:04,620 --> 00:05:07,110
Which is one itself and three.

77
00:05:07,110 --> 00:05:09,930
This three plus one gives us four.

78
00:05:09,930 --> 00:05:12,240
So when x is equal to one we get four.

79
00:05:12,240 --> 00:05:18,930
And when x is equal to two, we get two modulo three which is two and three plus two gives us five.

80
00:05:20,010 --> 00:05:26,640
Similarly, when x is equal to three, three modulo three is again zero.

81
00:05:26,640 --> 00:05:31,260
So notice that the pattern is repeating and three plus zero gives us three.

82
00:05:32,660 --> 00:05:39,650
And when x is equal to four again, for modulo three is equal to one and three plus one is equal to

83
00:05:39,650 --> 00:05:40,070
four.

84
00:05:40,070 --> 00:05:42,500
So you can see that this pattern is repeating.

85
00:05:42,500 --> 00:05:44,450
So again we'll get four over here.

86
00:05:44,450 --> 00:05:50,660
And then we'll get five again we'll get three four and 5467 and eight.

87
00:05:51,140 --> 00:05:55,790
Now notice that zero comma three is this position over here.

88
00:05:55,820 --> 00:06:02,870
Zero comma four is this position zero comma five is this position one comma three is over here.

89
00:06:03,520 --> 00:06:04,360
One comma.

90
00:06:04,360 --> 00:06:09,760
Four is over here, one comma, five is over here, and two comma three is over here, two comma four

91
00:06:09,760 --> 00:06:10,240
is over here.

92
00:06:10,240 --> 00:06:11,800
And two comma five is over here.

93
00:06:11,800 --> 00:06:19,150
So using this we were able to iterate through all the positions in the box at hand.

94
00:06:19,150 --> 00:06:22,540
So this over here is very useful.

95
00:06:22,540 --> 00:06:30,130
So three into rows by three plus x by three and three into columns by three plus x percentage or modulo

96
00:06:30,130 --> 00:06:30,520
three.

97
00:06:30,520 --> 00:06:38,470
If we can use this, we can just use a for loop where we are iterating through the values 0 to 8 to

98
00:06:38,470 --> 00:06:44,560
go through all the row column combinations which are part of the box at hand.

99
00:06:44,560 --> 00:06:49,060
So we will make use of this while we code out the solution.
