Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,090 --> 00:00:05,260
OK, so now let's try to solve this exercise together, shall we?
2
00:00:06,500 --> 00:00:14,450
And the first thing that I want to talk with you guys about is our first and basic usage of using the
3
00:00:14,450 --> 00:00:21,680
for loops or while loops to iterate over each of the elements of this number and finding out the solution.
4
00:00:22,100 --> 00:00:26,690
So basically, just talk about the logic of the iterative approach.
5
00:00:26,700 --> 00:00:30,230
Okay, and then we will come in, try to think about the recursive one.
6
00:00:30,860 --> 00:00:40,490
So the whole idea here is every time to take two values, OK, every time to take on each number to
7
00:00:40,490 --> 00:00:45,440
take the right, most digit in one digit on its left and compare between them.
8
00:00:46,130 --> 00:00:52,190
And based on these comparisons, we will make assumptions whether these two numbers are very descending,
9
00:00:52,640 --> 00:00:54,920
very ascending or neither of them.
10
00:00:55,760 --> 00:01:02,690
And based on how we move on, we will say that, OK, we have a flag that specifies that we found out
11
00:01:02,930 --> 00:01:04,790
a pair that is very ascending.
12
00:01:05,240 --> 00:01:08,960
That means that the next pair should also be very ascending.
13
00:01:09,320 --> 00:01:12,080
If not, that means that the next pair.
14
00:01:13,710 --> 00:01:20,640
Indicates the status that the whole number is neither very ascending nor very descending.
15
00:01:21,910 --> 00:01:27,370
So with four loops, it's pretty much something that we can work out, right?
16
00:01:27,460 --> 00:01:30,430
You can also try to solve it on your own using sound for a loop.
17
00:01:30,790 --> 00:01:34,270
But we are talking about a recursion in our goal.
18
00:01:34,450 --> 00:01:34,840
OK?
19
00:01:34,870 --> 00:01:40,620
Our goal is to split this exercise in into some sub problems.
20
00:01:40,630 --> 00:01:49,180
OK, so our main problem is to find out whether this number, let's say one to four, is very ascending,
21
00:01:49,180 --> 00:01:51,160
very descending or neither of them.
22
00:01:52,270 --> 00:02:00,310
In one conclusion that we can make is basically that here we will take this status between these two
23
00:02:00,310 --> 00:02:00,890
numbers.
24
00:02:00,910 --> 00:02:08,500
OK, so we will say that we will say that 10s will be equal to two and a units.
25
00:02:09,670 --> 00:02:10,990
Will be equal to four.
26
00:02:11,710 --> 00:02:17,350
Okay, so we will say tannen's equals to two units, equals two four, and then we will ask a simple
27
00:02:17,350 --> 00:02:17,890
question.
28
00:02:18,280 --> 00:02:22,090
We will say, let's basically we found out the problem about it.
29
00:02:22,450 --> 00:02:27,580
Let's split it into smaller problem and talk about one and two.
30
00:02:30,330 --> 00:02:39,630
If we come to think about it, then tense here equals to one and then units here equals to what to do.
31
00:02:40,740 --> 00:02:43,320
So we know that basically from here.
32
00:02:44,610 --> 00:02:50,820
It probably also be our stopping condition, OK, whenever we have a number of at least two digits.
33
00:02:50,940 --> 00:02:52,470
OK, one digit or digit.
34
00:02:52,830 --> 00:02:59,190
So we ask a simple question if Thames is less than units, then we can assume that this number right
35
00:02:59,190 --> 00:03:02,400
is very ascending, very ascending.
36
00:03:02,880 --> 00:03:03,570
Do you agree?
37
00:03:05,460 --> 00:03:12,210
And then we'll try to return the result that this number is very ascending and it can be indicated by
38
00:03:12,210 --> 00:03:13,320
returning one.
39
00:03:14,600 --> 00:03:15,020
Right.
40
00:03:15,830 --> 00:03:24,050
So now what we have to do here is to ask a simple question if the and the units in these numbers, if
41
00:03:24,050 --> 00:03:26,270
they represent a very ascending.
42
00:03:27,230 --> 00:03:27,920
Or are there?
43
00:03:27,980 --> 00:03:28,430
OK?
44
00:03:28,700 --> 00:03:38,030
And also so far, we returned and received from the sub problems we received that there was a result
45
00:03:38,030 --> 00:03:39,140
of various sending.
46
00:03:39,350 --> 00:03:46,070
Then also these functions should return one, but only when the condition for both of them applies.
47
00:03:46,910 --> 00:03:50,510
OK, so let me draw once again.
48
00:03:50,690 --> 00:03:51,170
OK.
49
00:03:51,410 --> 00:04:00,110
We specify we use some time to to draw these exercises exactly before we start writing the code if you
50
00:04:00,110 --> 00:04:03,320
are interested in the code, so we can skip up to this part.
51
00:04:03,620 --> 00:04:06,410
But for now, we are going to solve this exercise.
52
00:04:06,410 --> 00:04:09,200
Also for, let's say, the second example.
53
00:04:09,500 --> 00:04:13,100
So we start with what with nine, six, four and three.
54
00:04:13,580 --> 00:04:19,340
We say that the units equals two three and we say that the town's equals two four.
55
00:04:20,180 --> 00:04:27,080
Then we make the function call for the next one, four, nine, six and four, divided by 10.
56
00:04:27,110 --> 00:04:28,250
All right, that's what we do.
57
00:04:28,700 --> 00:04:32,330
Units will be equal to four and 10s will be equal to six.
58
00:04:33,080 --> 00:04:39,920
Then we make another function call for a 96 because we cannot say for sure if this number is very ascending
59
00:04:39,920 --> 00:04:42,890
or very descending just based on the units and the tense.
60
00:04:44,320 --> 00:04:50,860
And inside of these function, basically when we have two digits, we can say that units equals to six
61
00:04:51,070 --> 00:04:53,080
and 10s equals to nine.
62
00:04:53,920 --> 00:04:58,240
That means that tenths from the left is greater than units on their right.
63
00:04:59,200 --> 00:05:05,830
That means that we are going to return from this function, that this function is what is very ascending,
64
00:05:05,830 --> 00:05:07,570
that this number is very ascending.
65
00:05:08,290 --> 00:05:10,060
OK, so we return minus one.
66
00:05:10,750 --> 00:05:16,180
And then right before this function is going to return something, we ask a simple question.
67
00:05:16,630 --> 00:05:18,400
We ask if.
68
00:05:19,580 --> 00:05:26,420
The return value for the SA problem was minus one, meaning it was very descending this pair of numbers.
69
00:05:26,960 --> 00:05:29,630
So we want to ask if this number.
70
00:05:30,690 --> 00:05:32,940
Is greater also than this one.
71
00:05:33,480 --> 00:05:39,570
Meaning if it's also very descending based on the units and the tents, and if both of these conditions
72
00:05:39,570 --> 00:05:42,090
apply, then we return minus one to here.
73
00:05:42,480 --> 00:05:45,480
And we do the same process here we ask return value.
74
00:05:45,500 --> 00:05:48,660
So far, four nine six four is very descending.
75
00:05:48,900 --> 00:05:54,870
And also four and three is very descending, then return minus one to indicate that the whole number
76
00:05:54,870 --> 00:05:56,010
was very descending.
77
00:05:56,610 --> 00:05:59,070
And if, for example, units here was like, I don't know.
78
00:05:59,070 --> 00:05:59,670
Seven.
79
00:06:00,980 --> 00:06:08,390
Then the condition of Thames greater than the units will not apply, and the result so far is very descending.
80
00:06:08,630 --> 00:06:14,210
So basically, these two conditions would not work and the final result should be zero.
81
00:06:14,310 --> 00:06:18,290
OK, because it was not very descending nor very ascending.
82
00:06:19,380 --> 00:06:26,260
OK, so that's about the drawing that we had to make and the explanation of recursion.
83
00:06:26,290 --> 00:06:30,180
OK, some exercises require from us more attention.
84
00:06:30,750 --> 00:06:34,290
Now what we want to do is to start writing the actual code.
85
00:06:35,130 --> 00:06:37,530
So what should be the type of the function?
86
00:06:38,460 --> 00:06:42,900
Basically, we say that the function is returning zero, one or minus one.
87
00:06:43,410 --> 00:06:45,750
So it's probably going to be of an integer type.
88
00:06:47,470 --> 00:06:51,430
Right after that, what we do is we say what will be the functioning.
89
00:06:51,910 --> 00:06:55,510
So make sure you choose the proper function name and don't use something like that.
90
00:06:56,410 --> 00:06:56,700
OK.
91
00:06:57,220 --> 00:07:00,640
Here's something very nice digits sorted, for example.
92
00:07:00,940 --> 00:07:01,260
OK.
93
00:07:01,300 --> 00:07:02,980
This function will receive No.
94
00:07:04,170 --> 00:07:09,390
And what this function is going to do, this function is going to do something very interesting.
95
00:07:09,990 --> 00:07:16,170
It's going first of all for every function call like you can see right here, for every function call,
96
00:07:16,170 --> 00:07:20,430
we are going to extract the unique digits and the translated.
97
00:07:20,690 --> 00:07:24,850
OK, we assume that the number is going to be, of course, greater than 10.
98
00:07:24,870 --> 00:07:26,460
OK, that was our assumption.
99
00:07:26,520 --> 00:07:29,760
If you don't remember, check out the exercise.
100
00:07:29,770 --> 00:07:32,190
Okay, so you can see that here we have.
101
00:07:32,400 --> 00:07:33,420
I can't open it.
102
00:07:33,660 --> 00:07:37,410
So the assumption was that initial number will be at least of two digits.
103
00:07:38,400 --> 00:07:40,430
So we extracted the units.
104
00:07:40,440 --> 00:07:43,890
So we say units equals two number modulo 10.
105
00:07:43,980 --> 00:07:47,610
Remember how you extract the units the right most digit?
106
00:07:48,350 --> 00:07:53,880
Then we say intense equals two what two nouns divided by 10.
107
00:07:53,910 --> 00:07:55,710
And all of that divided modular.
108
00:07:55,710 --> 00:07:55,950
10.
109
00:07:56,190 --> 00:08:00,300
OK, so we extract the dance off a certain number.
110
00:08:02,330 --> 00:08:07,900
As I do that, I would like us to create additional variable that will hold the status that will be
111
00:08:07,900 --> 00:08:15,520
returned from every function call and we will call it, I don't know and sorted so far.
112
00:08:15,790 --> 00:08:17,170
OK, sorted so far.
113
00:08:18,070 --> 00:08:22,180
And this variable should hold either zero, one or minus one.
114
00:08:23,290 --> 00:08:23,650
OK.
115
00:08:24,770 --> 00:08:26,930
So how it will look like.
116
00:08:27,230 --> 00:08:33,510
First of all, we need in our recursion to use some stopping condition, some base condition, OK,
117
00:08:33,530 --> 00:08:41,750
some base case and the base case can be when NAM has less than three digits, meaning two digits or
118
00:08:41,750 --> 00:08:42,080
less.
119
00:08:42,770 --> 00:08:48,880
And we can indicate that is the case if numb is less than 100, for example, right?
120
00:08:49,940 --> 00:08:55,640
And if that's the case, if numb is less than 100, then what we can do is ask the following question
121
00:08:56,060 --> 00:09:03,010
we can ask if the two digits, tens and units are sorted in ascending order.
122
00:09:03,020 --> 00:09:06,470
So we ask if Tannen's is greater than units.
123
00:09:07,220 --> 00:09:08,920
OK, we'll look at from left to right.
124
00:09:08,930 --> 00:09:11,390
Then we say that it is descending.
125
00:09:11,660 --> 00:09:12,770
It is ascending.
126
00:09:12,770 --> 00:09:13,460
Sorry about that.
127
00:09:14,870 --> 00:09:17,210
So in this case, what the function should do?
128
00:09:18,440 --> 00:09:22,760
We know that if the number is very ascending, then we should return.
129
00:09:22,880 --> 00:09:27,410
One else, we should return zero.
130
00:09:27,650 --> 00:09:36,440
OK, so this LS basically refers to when units is less than 10s since we're assuming that all digits
131
00:09:36,440 --> 00:09:37,700
are different.
132
00:09:37,790 --> 00:09:38,270
OK?
133
00:09:38,510 --> 00:09:41,210
In our NUM, that was also an assumption.
134
00:09:41,240 --> 00:09:46,970
If not, we should also take some consideration with additional eve to check if they are both equal
135
00:09:46,970 --> 00:09:47,750
to one another.
136
00:09:48,170 --> 00:09:56,720
OK, but for now, this will be sufficient so that the base case, if we reach the number of digits,
137
00:09:56,720 --> 00:10:03,740
then we can ask if the tenth is greater than the units return one, otherwise return minus one minus
138
00:10:03,740 --> 00:10:03,950
one.
139
00:10:03,960 --> 00:10:04,760
Sorry about that.
140
00:10:05,030 --> 00:10:10,700
Yes, minus one, because this indicates that the number is very descending.
141
00:10:10,970 --> 00:10:11,540
And that's it.
142
00:10:11,760 --> 00:10:13,490
That's that was the whole point.
143
00:10:13,790 --> 00:10:23,390
Once again, if you want also to take into account that it may be the quality numbers like twenty to
144
00:10:23,390 --> 00:10:24,770
thirty three and so on.
145
00:10:25,250 --> 00:10:32,450
And based on that, to say that the number is not very ascending, nor it is very descending.
146
00:10:32,450 --> 00:10:37,580
So adding just additional condition, additional Ilse elseif and return zero.
147
00:10:37,620 --> 00:10:38,900
OK, nothing complicated.
148
00:10:40,250 --> 00:10:49,880
Then after that, we say OK, if the result, if the NUM was not basically related to the base condition,
149
00:10:50,600 --> 00:10:52,610
then we will say that the results so far.
150
00:10:53,750 --> 00:11:01,790
Let's sort it so far, thought that so far will be equal to what through the result that will be returned
151
00:11:01,790 --> 00:11:06,430
by the recursive call of numb divided by 10.
152
00:11:07,100 --> 00:11:08,410
That's what we've done here.
153
00:11:08,960 --> 00:11:14,960
Every time we divided the number by 10 and based on the smaller, smaller, smaller results, we tried
154
00:11:14,960 --> 00:11:16,280
to build our way up.
155
00:11:17,360 --> 00:11:22,370
OK, so sorted so far equals the digits sorted, numb, divided by 10.
156
00:11:23,150 --> 00:11:28,120
And we ask if so far the digits were sorted in ascending order.
157
00:11:28,220 --> 00:11:31,160
That's what we would like to ask and the current write.
158
00:11:31,160 --> 00:11:34,040
Most digits also satisfy the condition.
159
00:11:34,550 --> 00:11:36,170
Then we will return one.
160
00:11:36,830 --> 00:11:43,370
So what if we do we ask if sorted so far equals to one that means what did it mean?
161
00:11:44,210 --> 00:11:50,510
That means that all of the sequence of the digits was so far.
162
00:11:51,950 --> 00:12:02,330
Various ending, and we say that if tens is less, then raped less or greater, if Thames is less.
163
00:12:03,420 --> 00:12:06,690
Then units right here, I think I made a mistake, right?
164
00:12:06,990 --> 00:12:11,670
If dance is less than units, then yeah, that's how should be.
165
00:12:12,480 --> 00:12:16,110
So if is less than units, then return one else, return minus one.
166
00:12:17,140 --> 00:12:18,610
OK, because we look from left to right.
167
00:12:19,210 --> 00:12:24,130
So if I thought that so far equals to one in towns is less than units, then in this case we know that
168
00:12:24,340 --> 00:12:31,270
so far we've found out that the result, the digits so far were sort in a very ascending order.
169
00:12:31,450 --> 00:12:38,290
And also the rightmost digit is less than one when it's left, meaning it still keeps the very ascending
170
00:12:38,290 --> 00:12:38,710
order there.
171
00:12:38,810 --> 00:12:40,090
Still return one.
172
00:12:40,840 --> 00:12:41,410
Awesome.
173
00:12:42,520 --> 00:12:51,010
And if sorted so far, equals to minus one, meaning it was very descending and the 10s is greater than
174
00:12:51,010 --> 00:12:53,650
the units in this case than a return minus one.
175
00:12:54,720 --> 00:12:55,080
Right.
176
00:12:55,860 --> 00:13:00,360
Otherwise, if so far, the digits were not sorted.
177
00:13:00,780 --> 00:13:08,130
Neither are sending or nor descending, meaning the sort that so far was zero or basically one of these
178
00:13:08,130 --> 00:13:13,200
conditions did not apply with the conjunction of either of the status so far.
179
00:13:13,500 --> 00:13:16,110
Then in this case, we should return.
180
00:13:17,690 --> 00:13:18,410
Zero.
181
00:13:19,160 --> 00:13:19,670
OK.
182
00:13:20,360 --> 00:13:23,840
And that's basically it for this function.
183
00:13:23,960 --> 00:13:25,580
It's all it has to do.
184
00:13:27,080 --> 00:13:28,190
Now, let's take a look.
185
00:13:28,370 --> 00:13:34,670
Once again, and this example and another drawing and see how it a function calls were made and basically
186
00:13:34,670 --> 00:13:36,080
what these two were inside of it.
187
00:13:36,800 --> 00:13:44,720
OK, so we start with this simple option we start with now equals two nine six four three.
188
00:13:45,020 --> 00:13:46,070
That's where we start.
189
00:13:46,970 --> 00:13:53,270
We say that the unions equals two three, and we say that the ten sequels to four and then we ask a
190
00:13:53,270 --> 00:13:56,160
simple question if Nnam is less than 100.
191
00:13:56,180 --> 00:13:59,270
No, so sort that so far equals the digits sorted.
192
00:13:59,690 --> 00:14:04,910
So we stop here inside of this instance of the function on the value of sorted.
193
00:14:05,900 --> 00:14:08,480
So far that we don't know about it, right?
194
00:14:08,990 --> 00:14:13,220
We know that it should be filled by the results returned from the recursive call.
195
00:14:13,520 --> 00:14:16,670
So we do not proceed inside of these incidents.
196
00:14:17,330 --> 00:14:21,050
We call these function four nine six four using it stands.
197
00:14:21,050 --> 00:14:24,830
Then we call another function that also waits for sorted.
198
00:14:25,790 --> 00:14:26,690
So far.
199
00:14:27,840 --> 00:14:29,430
It also waits for the result.
200
00:14:29,850 --> 00:14:30,180
Right?
201
00:14:30,870 --> 00:14:32,850
And the result should be returned from here.
202
00:14:33,150 --> 00:14:39,450
So for 96, this condition is true if tens is less than units, no health.
203
00:14:39,450 --> 00:14:42,210
When units, it's less than dense return minus one.
204
00:14:42,660 --> 00:14:46,260
So the result here is going to be returned is minus one.
205
00:14:46,830 --> 00:14:50,430
OK, so minus one will be returned to here minus one.
206
00:14:51,330 --> 00:14:54,270
And where did we stop in this instance of this function?
207
00:14:54,570 --> 00:14:55,830
We stopped in this line.
208
00:14:56,370 --> 00:15:00,090
So inside of these functions throughout that so far, we'll be equal to minus one.
209
00:15:00,600 --> 00:15:05,920
And all that remains for us to complete is the following things you've sorted so far equals to one.
210
00:15:05,940 --> 00:15:07,110
No, that's not the case.
211
00:15:07,350 --> 00:15:11,110
You thought that's a one equals so far equals two minus one.
212
00:15:11,130 --> 00:15:11,910
That's the case.
213
00:15:12,120 --> 00:15:18,480
And the tents and the units inside of these function call 10s is greater than the units.
214
00:15:18,480 --> 00:15:19,350
That's the case.
215
00:15:19,620 --> 00:15:22,710
Then these function, these instance should return minus one.
216
00:15:23,220 --> 00:15:30,120
So minus one should be returned right here, right minus what happened minus one.
217
00:15:31,260 --> 00:15:34,170
And where did we stop inside of this instance of the function?
218
00:15:34,620 --> 00:15:36,480
We stopped right here.
219
00:15:36,630 --> 00:15:37,110
OK.
220
00:15:37,410 --> 00:15:41,550
So this line is completed, and we ask the following question No, that does not happen.
221
00:15:42,120 --> 00:15:44,280
If sorted so far equals two minus one.
222
00:15:44,340 --> 00:15:50,010
Then also, if we have tenths greater, then the units, that's the case.
223
00:15:50,010 --> 00:15:51,270
We return minus one.
224
00:15:51,720 --> 00:15:57,870
So this function finally returns minus one, and that's basically how it works.
225
00:15:58,350 --> 00:16:03,030
So a function call of, let's say, inside of the main you have like, I don't know int result.
226
00:16:04,200 --> 00:16:09,360
Inside of the main or some other function that called it, and in result, it's destroyed right here,
227
00:16:09,360 --> 00:16:09,810
OK?
228
00:16:09,850 --> 00:16:10,710
It will be better.
229
00:16:11,900 --> 00:16:15,770
OK, so you have something like end result.
230
00:16:17,250 --> 00:16:18,810
Equals two digits.
231
00:16:20,070 --> 00:16:22,220
Digits sorted.
232
00:16:23,330 --> 00:16:23,940
Four.
233
00:16:24,860 --> 00:16:25,550
What was it?
234
00:16:25,730 --> 00:16:27,530
Nine, six, four three.
235
00:16:28,220 --> 00:16:34,810
So the final result that this function is going to return is minus one and it's going to replace these
236
00:16:34,820 --> 00:16:35,180
burnt.
237
00:16:36,500 --> 00:16:36,980
OK.
238
00:16:38,430 --> 00:16:41,250
So I hope that's clear and you understand what's happening.
239
00:16:42,500 --> 00:16:45,920
And basically, you see the whole picture of the recursive calls.
240
00:16:46,310 --> 00:16:53,390
Also, what I do recommend, you guys, is take a couple of moments, try to draw all the recursive
241
00:16:53,390 --> 00:16:59,570
calls that will happen inside whenever you will try to call this number one two three four zero and
242
00:16:59,570 --> 00:17:06,290
see basically how the returned results will be zero and how basically this chain will return finally
243
00:17:06,290 --> 00:17:09,740
zero to whoever called this function.
244
00:17:09,980 --> 00:17:11,120
OK, very important.
245
00:17:11,480 --> 00:17:16,560
Take these drawing lesson and draw the actual function calls, right?
246
00:17:16,580 --> 00:17:17,540
The main function?
247
00:17:17,810 --> 00:17:18,470
Make some.
248
00:17:19,460 --> 00:17:27,190
Function calls check if the results are exactly as you expected and compare it with your results, suggest
249
00:17:27,200 --> 00:17:30,080
any suggestions and yeah, make sure that it works.
250
00:17:31,370 --> 00:17:33,830
Who once again, thank you guys for watching.
251
00:17:33,860 --> 00:17:35,030
Keep on practicing.
252
00:17:35,150 --> 00:17:36,350
Keep on moving forward.
253
00:17:36,380 --> 00:17:39,740
This is very important concept, very important exercises.
254
00:17:40,040 --> 00:17:44,430
I think you're getting more confident about it now.
255
00:17:44,450 --> 00:17:47,380
If not, you have plenty of other exercises.
256
00:17:47,390 --> 00:17:52,940
Hopefully, you will have time and arm and strength to solve them.
257
00:17:53,450 --> 00:17:55,400
So until next time, my name is Vlad.
258
00:17:55,430 --> 00:17:56,470
This is alpha tech.
259
00:17:56,480 --> 00:18:02,440
We are getting better in programming and I will see you in the next videos until the next time.
260
00:18:02,510 --> 00:18:03,290
What do we say?
261
00:18:03,800 --> 00:18:04,340
Bye bye.
24220
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.