Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,900 --> 00:00:03,750
So basically, what we need to do in these function.
2
00:00:04,170 --> 00:00:10,290
OK, let's try to break it down into smaller pieces and try to figure out what should be the recursive
3
00:00:10,290 --> 00:00:10,750
calls.
4
00:00:10,770 --> 00:00:14,010
What should be the stopping condition and so on and so forth.
5
00:00:15,000 --> 00:00:22,830
So in this exercise, I think that first of all, once you realize that in order to solve the bigger
6
00:00:22,830 --> 00:00:26,550
problem, we need to split it up to smaller problems.
7
00:00:27,300 --> 00:00:33,810
And if that's the case, then probably we will reach someplace where we can say that we reached some
8
00:00:33,810 --> 00:00:41,010
stopping condition and there is no reason as to why we would like to make further recursive calls.
9
00:00:42,030 --> 00:00:49,290
And with this type of exercises, probably whenever we need to work with every digit, then on every
10
00:00:49,290 --> 00:00:53,880
recursive call, then we will get something like this right divided by 10.
11
00:00:54,060 --> 00:00:57,510
And then once again divided, let's do it like this.
12
00:00:57,720 --> 00:01:04,620
And then once again divided by 10 and so on and so forth until we reach some stopping condition when
13
00:01:04,620 --> 00:01:07,320
there is no reason to actually divide it by 10.
14
00:01:08,250 --> 00:01:11,100
And this stopping condition may simply be.
15
00:01:12,170 --> 00:01:20,720
Are specified as, for example, reaching out a digit number when number equals or less than.
16
00:01:22,480 --> 00:01:22,960
Nine.
17
00:01:23,020 --> 00:01:23,410
Right.
18
00:01:23,990 --> 00:01:31,330
Meaning whenever we will reach numb to be of one digit, then we can say that that will be the stopping
19
00:01:31,330 --> 00:01:37,540
condition because there is no reason as to why we would like to divide it even further.
20
00:01:37,570 --> 00:01:44,140
OK, so one digit so our stopping condition will probably be whenever we reach one digit.
21
00:01:44,290 --> 00:01:47,410
So let's take an example of here.
22
00:01:47,440 --> 00:01:54,160
The first example we have now equals to one two four and also digit.
23
00:01:55,100 --> 00:01:57,140
Equals to what equals two to.
24
00:01:58,300 --> 00:02:05,980
So if we want to count how many times the digit to abusing in these number, then we definitely need
25
00:02:05,980 --> 00:02:14,680
to know how many times or basically even if we the number of appearances was even or odd in this number
26
00:02:14,680 --> 00:02:18,460
of one two right with digit equals 82.
27
00:02:19,510 --> 00:02:26,890
So if we know that the if it's odd or even number of occurrences in this number, then we can conclude
28
00:02:27,040 --> 00:02:34,620
if it's even or in this number based on the result received from these recursive call and comparing
29
00:02:34,660 --> 00:02:35,740
the right most digit.
30
00:02:36,900 --> 00:02:42,690
Also, these can be done here comparing now equals to one and digit equals two to.
31
00:02:44,380 --> 00:02:51,400
So if we reached these base condition, right, these base statement, then we can say if digit equals
32
00:02:51,400 --> 00:02:55,300
to numb, then return, what then return?
33
00:02:55,300 --> 00:02:59,320
Probably the indicator of that we have an odd occurrences.
34
00:02:59,740 --> 00:03:06,400
Otherwise we turn zero, and every time we will return something, we will say that the number of occurrences
35
00:03:06,400 --> 00:03:09,310
inform the recursive calls even or odd.
36
00:03:09,580 --> 00:03:13,570
And what about the rightmost digit, the right?
37
00:03:13,570 --> 00:03:17,410
Most digit, of course, the right, most digit, which is still, for example.
38
00:03:17,650 --> 00:03:21,550
And if it changes the total occurrences in this number?
39
00:03:21,990 --> 00:03:24,070
OK, so do you get the idea?
40
00:03:25,030 --> 00:03:25,410
OK.
41
00:03:25,930 --> 00:03:28,390
So that's exactly what we are going to do and.
42
00:03:29,390 --> 00:03:35,330
I just, yeah, I suggest let's take a look at what will happen in sight of this example.
43
00:03:35,570 --> 00:03:42,950
So we compare now one with Egypt, and we know that the total of occurrences of Egypt in these numbers
44
00:03:42,960 --> 00:03:44,720
in this instance will be what?
45
00:03:46,230 --> 00:03:47,540
We'll be even.
46
00:03:47,730 --> 00:03:49,020
Right, because zero.
47
00:03:49,200 --> 00:03:52,200
That's why we will return, what should we return?
48
00:03:53,140 --> 00:04:01,660
We return one, then we ask the following question if there are the total occurrences, if it's even
49
00:04:01,660 --> 00:04:09,250
or odd so far is one and the digit equals to this digit, then this means that the total occurrences
50
00:04:09,250 --> 00:04:10,510
is now not even.
51
00:04:10,660 --> 00:04:14,650
But it's going to be odd because we will have now one appearance.
52
00:04:15,070 --> 00:04:21,400
So these function, since it's odd, is going to return zero and then we are going to say we have zero
53
00:04:21,400 --> 00:04:22,120
occurrences.
54
00:04:22,720 --> 00:04:27,100
OK, and meaning it's an odd number of occurrences.
55
00:04:27,100 --> 00:04:29,020
That's why the function returns zero.
56
00:04:29,350 --> 00:04:31,390
And here we compare four with two.
57
00:04:31,390 --> 00:04:38,920
And the result is that they are not equal, meaning they do not change the status of the odd or even
58
00:04:39,220 --> 00:04:41,020
total of occurrences.
59
00:04:41,650 --> 00:04:44,080
OK, so the final result will be zero.
60
00:04:44,810 --> 00:04:45,790
That's how it goes.
61
00:04:46,200 --> 00:04:51,970
OK, if you need to refresh it a little bit, swipe the video to the left.
62
00:04:51,970 --> 00:04:52,660
Watch it again.
63
00:04:53,380 --> 00:04:59,420
OK, so now that we know the idea behind it, let's try to solve this exercise, OK?
64
00:04:59,440 --> 00:05:05,710
The user exercises are actually not so easy, but I think we are going to do good.
65
00:05:06,730 --> 00:05:12,850
So the type of the of the function is going to be int because we return zero or one.
66
00:05:13,810 --> 00:05:19,660
We can optimize it, of course, but that's not the whole idea behind practicing the recursion.
67
00:05:19,670 --> 00:05:21,010
Let's go with the basic one.
68
00:05:21,700 --> 00:05:25,170
Let's create the function and what should be the function name.
69
00:05:25,180 --> 00:05:29,680
Let's call it digits, even appearances.
70
00:05:29,950 --> 00:05:35,080
OK, even all the appearances, whatever these function will receive numb, numb.
71
00:05:35,650 --> 00:05:39,490
And also these functions should receive in what int Egypt?
72
00:05:43,030 --> 00:05:50,320
And one of these function is going to check let us start with the base condition, we will ask the following
73
00:05:50,320 --> 00:05:55,020
question if numb is less than 10, right?
74
00:05:55,030 --> 00:06:05,380
Meaning if numb is one digit, if numb is one digit, then in this case we will ask another question
75
00:06:05,530 --> 00:06:13,600
if we already know if we already know that numb eyes of one digit, we can ask if numb does not equal
76
00:06:13,600 --> 00:06:14,410
two digit.
77
00:06:15,620 --> 00:06:22,340
Meaning if it does not appear in them, then in this case, based on the requirements of this exercise,
78
00:06:22,580 --> 00:06:25,400
we should return what we should return one.
79
00:06:26,180 --> 00:06:26,930
Why one?
80
00:06:26,930 --> 00:06:34,490
Because the total number of occurrences is zero and zero is even considered to be even right.
81
00:06:35,500 --> 00:06:37,150
Zero appearances.
82
00:06:38,350 --> 00:06:46,510
And that means that we are working with are even appearances, right, even appearances.
83
00:06:47,740 --> 00:06:48,130
OK.
84
00:06:48,730 --> 00:06:52,360
And the ill spark for these if condition, right?
85
00:06:52,750 --> 00:06:56,800
Meaning when NEM equals two digit.
86
00:06:56,950 --> 00:07:04,360
OK, so this statement will be executed whenever name equals two digit, then we should return zero.
87
00:07:04,510 --> 00:07:14,020
OK, so that means that zero will be returned when the digit appears appears once in NUM.
88
00:07:15,080 --> 00:07:16,340
And once meaning.
89
00:07:17,250 --> 00:07:18,840
All appearances, right?
90
00:07:20,210 --> 00:07:25,820
So there is even and odd the difference is one can be divided by two without their remainder, while
91
00:07:25,820 --> 00:07:31,250
the other will be with the remainder, OK, even more awesome.
92
00:07:31,880 --> 00:07:37,700
So that's about the base case, that's about the base case, and it will be executed whenever NAM is
93
00:07:37,700 --> 00:07:38,360
less than 10.
94
00:07:40,200 --> 00:07:42,960
What will happen otherwise?
95
00:07:43,710 --> 00:07:46,860
So we are interested in.
96
00:07:48,210 --> 00:07:55,890
Returning, whether the total number of appearances is even or odd, and for that, we need to split
97
00:07:55,890 --> 00:08:02,670
the problem into smaller parts and we need later on before these function, before these incidents will
98
00:08:02,670 --> 00:08:03,780
return the result.
99
00:08:04,410 --> 00:08:13,890
We need to know what is the status for all the other idiots except for the rightmost OK during the recursive
100
00:08:13,890 --> 00:08:15,810
calls whenever we divide it by 10.
101
00:08:16,620 --> 00:08:21,930
So for that, we will create additional variable that will be used for our assistance and we will call
102
00:08:21,930 --> 00:08:28,560
it into total appearance appearances so far.
103
00:08:29,750 --> 00:08:38,330
And total appearances so far will be calculated how by making their recursive call digits, even appearances
104
00:08:38,750 --> 00:08:43,460
for now divided by 10 right without the right most digit.
105
00:08:43,790 --> 00:08:46,790
And also we have what with the digit itself?
106
00:08:49,130 --> 00:08:49,640
All right.
107
00:08:50,210 --> 00:08:54,890
So total appearances so far, it can be either zero or one.
108
00:08:55,610 --> 00:08:56,030
OK.
109
00:08:56,090 --> 00:08:59,240
Total appearances so far can be zero or one.
110
00:08:59,720 --> 00:09:02,450
This means OK, pay very close attention.
111
00:09:02,660 --> 00:09:08,840
This means the status of total of occurrences for numb, divided by 10 from this point and further as
112
00:09:08,840 --> 00:09:15,080
you dive into making the recursive calls in the recursive functions.
113
00:09:16,170 --> 00:09:17,850
You feel me so far came with me.
114
00:09:18,660 --> 00:09:19,170
Awesome.
115
00:09:19,620 --> 00:09:23,100
And now we are going to ask very, very simple questions.
116
00:09:23,820 --> 00:09:28,740
We are going to ask Eve, so it's all appearances so far equals to one.
117
00:09:29,160 --> 00:09:34,800
If that's the case, then what it means is that so far we have even appearances.
118
00:09:34,800 --> 00:09:35,220
OK?
119
00:09:35,490 --> 00:09:38,610
So far we had even.
120
00:09:40,140 --> 00:09:47,160
Appearances of Digit in numb, right in numb, divided by 10.
121
00:09:47,760 --> 00:09:49,680
Let's see, OK, for at least instance.
122
00:09:51,700 --> 00:10:01,540
And also, if that's the case, then what we need to know is if no modular 10 meaning the rightmost
123
00:10:01,540 --> 00:10:07,990
digit that we did not take into account because the total appearances so far is related to number divided
124
00:10:07,990 --> 00:10:08,530
by 10.
125
00:10:09,310 --> 00:10:15,640
So we ask if not modular 10 equals two digit meaning we compare right.
126
00:10:15,640 --> 00:10:24,310
We compare the right most the right, most digit in NAM in this appearance with the digit itself.
127
00:10:24,520 --> 00:10:33,370
If that's the case, then all it means that what we need to swap or basically change the total appearances
128
00:10:33,610 --> 00:10:40,030
because we know that so far without the right most digit, we have even number of appearances.
129
00:10:40,270 --> 00:10:46,480
And if we found out that there is another appearance right for the right most digit, meaning the digit
130
00:10:46,480 --> 00:10:51,310
equals to the right, most digits inside number, then it means that we need simply to change it.
131
00:10:51,820 --> 00:10:52,750
How do we change it?
132
00:10:53,080 --> 00:10:57,600
We return what we return zero, right?
133
00:10:57,610 --> 00:11:00,430
If it was one, we return zero else.
134
00:11:02,050 --> 00:11:02,680
Meaning.
135
00:11:04,040 --> 00:11:05,270
Come again, sorry.
136
00:11:05,990 --> 00:11:08,390
Else that means when.
137
00:11:08,420 --> 00:11:09,980
What that means?
138
00:11:10,040 --> 00:11:12,380
Nothing changed, right?
139
00:11:12,410 --> 00:11:14,750
Nothing will change because.
140
00:11:16,110 --> 00:11:19,530
Digit does not equal to now modular 10.
141
00:11:20,370 --> 00:11:21,660
OK, so that makes sense.
142
00:11:22,110 --> 00:11:24,330
So we had total appearances so far.
143
00:11:25,360 --> 00:11:31,960
Of even number of occurrences, and we ask a simple question, if not modular 10 equals two digit.
144
00:11:32,900 --> 00:11:39,920
Then returns zero, changed the status, and if not, then we simply return whatever it was previously.
145
00:11:40,340 --> 00:11:42,140
So we return one.
146
00:11:44,190 --> 00:11:52,440
OK, but if we had else, the ills section is related to want to win total appearances.
147
00:11:52,950 --> 00:12:03,630
Appearances so far was equal to zero, then in this case, what should happen is if no more gelatin
148
00:12:04,140 --> 00:12:05,520
equals two digit.
149
00:12:07,760 --> 00:12:14,870
Then in this case, what we should try to do is to return what the return one because we want to change
150
00:12:15,200 --> 00:12:22,310
the status, if so far we had an odd occurrence, an odd number of occurrences of digits and say no,
151
00:12:22,790 --> 00:12:31,910
now we have added the digit equals to the rightmost digit number, then switch else returns zero.
152
00:12:33,290 --> 00:12:33,790
OK.
153
00:12:34,370 --> 00:12:35,090
Is it clear?
154
00:12:35,930 --> 00:12:37,370
And that's it.
155
00:12:37,580 --> 00:12:39,050
That's basically it.
156
00:12:39,500 --> 00:12:42,170
That will be that would be the solution.
157
00:12:44,100 --> 00:12:45,960
That would be the solution, guys.
158
00:12:46,620 --> 00:12:47,160
That's.
159
00:12:48,260 --> 00:12:49,040
All about it.
160
00:12:50,300 --> 00:12:50,720
OK.
161
00:12:51,610 --> 00:12:56,380
And I want to show you a simple example for both of these two options, OK?
162
00:12:57,010 --> 00:13:02,890
Let's say we started with NAM equal now equals to one hundred and twenty four.
163
00:13:03,950 --> 00:13:06,350
So we have now equals to.
164
00:13:07,410 --> 00:13:15,060
One hundred and twenty four now equals to one 24 that Egypt is going to be the same, so that's fine.
165
00:13:15,600 --> 00:13:20,670
So here we count the total appearances and we don't know yet what it is.
166
00:13:21,060 --> 00:13:23,760
So we make the recursive call for now equals.
167
00:13:25,310 --> 00:13:29,210
212 then we try to calculate this line right here.
168
00:13:29,420 --> 00:13:34,010
This one we tried to calculate, we say total equals to what something we don't know.
169
00:13:34,250 --> 00:13:38,060
We make another recursive call for a now equals to one.
170
00:13:39,020 --> 00:13:43,600
And then we ask a simple question, if army is less than 10, that's the case.
171
00:13:43,610 --> 00:13:49,340
If no does not equal two digit and it doesn't equal two digits since digit equals to two now equals
172
00:13:49,340 --> 00:13:49,610
to one.
173
00:13:49,880 --> 00:13:57,050
So these function is going to return what is going to return one where he's going to return one inside
174
00:13:57,050 --> 00:13:57,540
of here.
175
00:13:57,560 --> 00:13:58,670
So here will be one.
176
00:13:59,600 --> 00:14:04,250
Then what we need to complete inside of this instance of the function is that we know total appearances
177
00:14:04,250 --> 00:14:08,270
so far equals to one, and we ask if it equals to one.
178
00:14:08,570 --> 00:14:14,720
That then means that so far we have even appearances of digits in number divided by 10.
179
00:14:14,780 --> 00:14:15,710
And that's the case.
180
00:14:16,430 --> 00:14:21,950
And we ask, if not modulo 10 inside of these, it's not modular 10 equals to two.
181
00:14:22,340 --> 00:14:25,160
If it equals the digit and they is digit two.
182
00:14:25,400 --> 00:14:31,970
That's the case, then we should change the status from one to zero and return it so we return zero.
183
00:14:32,420 --> 00:14:40,250
That means that we know that there is an odd number of occurrences so far number of occurrences.
184
00:14:42,420 --> 00:14:42,930
OK.
185
00:14:43,930 --> 00:14:50,050
And then finally, they started we finished here, we ask if it equals to zero, right, because it
186
00:14:50,050 --> 00:14:52,240
does not equal to one, so it equals to zero.
187
00:14:52,420 --> 00:14:53,560
That's our else.
188
00:14:54,100 --> 00:15:00,040
Then we ask, if not divided by 10 equals two digit and it's not the case then returned.
189
00:15:00,040 --> 00:15:00,730
What was it?
190
00:15:01,030 --> 00:15:03,200
One else return is zero.
191
00:15:03,220 --> 00:15:04,870
Don't change the status.
192
00:15:05,140 --> 00:15:09,610
So the final result will be zero exactly as requested.
193
00:15:10,750 --> 00:15:14,530
And the same thing with the same steps, I will not even run, it's OK.
194
00:15:14,860 --> 00:15:20,230
The same thing, the same steps for any other value and any other number.
195
00:15:21,160 --> 00:15:27,870
So I hope it's clear to you guys, OK, and very, very important to know this and to understand that
196
00:15:28,070 --> 00:15:33,700
all the process and how it works, OK, you can stop the video, return back and watch again the the
197
00:15:33,700 --> 00:15:36,160
drawings and basically all the steps.
198
00:15:37,360 --> 00:15:41,590
And yeah, not a very easy exercise, but it's kind of nice.
199
00:15:41,620 --> 00:15:45,490
OK, we solved it using recursion, using a step by step approach.
200
00:15:46,360 --> 00:15:47,980
And now I have a question to ask you.
201
00:15:48,790 --> 00:15:56,380
So we completed this exercise in about like about pretty much two lines of code, including the signature
202
00:15:56,380 --> 00:15:57,070
of the function.
203
00:15:58,060 --> 00:16:01,260
I want to be a little bit minimize this.
204
00:16:01,270 --> 00:16:04,240
OK, so this will be the bonus for this exercise.
205
00:16:04,600 --> 00:16:06,800
Try to minimize this part, OK?
206
00:16:06,820 --> 00:16:14,190
Take a look at it and try to figure out if things are pretty much looking the same.
207
00:16:14,200 --> 00:16:20,350
OK, so we check this condition here in here and we return the result, which is the opposite of total
208
00:16:20,360 --> 00:16:22,480
appearances so far here and here.
209
00:16:22,960 --> 00:16:26,230
So we change the status and once we do return the same status.
210
00:16:26,830 --> 00:16:33,040
So try to think if we can minimize these lines of code in three about, I don't know, instead of how
211
00:16:33,040 --> 00:16:39,790
much instead of ten lines of code, maybe we can do it in less in five or four lines of code.
212
00:16:40,600 --> 00:16:43,150
OK, so this will work just fine.
213
00:16:43,420 --> 00:16:46,180
Trying to think about OK, that's a bonus.
214
00:16:46,480 --> 00:16:52,510
First of all, these exercises over you completed it successfully, and now you simply need to think
215
00:16:52,510 --> 00:16:57,250
about what in how this exercise can be a little bit optimized.
216
00:16:57,250 --> 00:17:00,850
The solution minimized, actually not optimized, but minimized.
217
00:17:01,150 --> 00:17:04,300
So it will look more cleaner.
218
00:17:04,780 --> 00:17:06,040
So try this one.
219
00:17:06,040 --> 00:17:06,460
And.
220
00:17:08,050 --> 00:17:14,440
Try this one again and again and again until you succeed, and if not, and also if yes, compare it
221
00:17:14,440 --> 00:17:15,600
with the next video.
222
00:17:15,610 --> 00:17:20,680
We know I'm going to also just for you to give you the solution for this option.
223
00:17:21,100 --> 00:17:22,780
OK, so goodbye.
21402
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.