Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,430 --> 00:00:08,770
Hey, welcome back, ladies and gentlemen, and in this video, what we are going to do is simply to
2
00:00:08,770 --> 00:00:14,250
expand our previous exercise where we have written some functions.
3
00:00:14,260 --> 00:00:20,190
One of them was like print one to end and the other function was print and to one.
4
00:00:20,710 --> 00:00:28,780
And basically these functions were all about like printing all the numbers from one up to a given and
5
00:00:28,840 --> 00:00:30,750
assuming that any positive.
6
00:00:30,760 --> 00:00:31,880
Positive, of course.
7
00:00:32,710 --> 00:00:41,620
So in this case, if we got a number of four and input four, then we simply would have printed print
8
00:00:41,620 --> 00:00:43,730
one to end one, two, three, four.
9
00:00:43,780 --> 00:00:45,850
OK, because here again is four.
10
00:00:46,360 --> 00:00:51,250
And also we created another function which was print and to one.
11
00:00:51,460 --> 00:00:58,660
And all these function was doing is like printing all the numbers from four, three, two, one.
12
00:00:58,690 --> 00:01:09,880
OK, so basically we had created these two recursive functions and we also talked about their flow,
13
00:01:09,880 --> 00:01:17,230
how they make the recursive calls, what is printed before, what is printed after and so on and so
14
00:01:17,230 --> 00:01:17,640
forth.
15
00:01:17,650 --> 00:01:17,950
Right.
16
00:01:18,220 --> 00:01:27,150
So let's just review and make sure that everything looks exactly as we were expecting.
17
00:01:27,280 --> 00:01:33,910
So print into one and getting one in, for example, for printing for then going through the same function,
18
00:01:33,910 --> 00:01:39,190
printing three, printing to printing one until we reach some Stubing condition.
19
00:01:39,250 --> 00:01:43,780
OK, so also this thing was done here.
20
00:01:43,810 --> 00:01:54,040
But just the difference was, is that when we print the current value of the recursive call and when
21
00:01:54,040 --> 00:01:56,590
we make the recursive call itself.
22
00:01:56,620 --> 00:02:04,180
OK, so here the print comes after words and then in here print comes before the recursive call.
23
00:02:04,300 --> 00:02:10,360
And that's the difference in terms of when you print one, two, three, four or four, three, two,
24
00:02:10,360 --> 00:02:10,720
one.
25
00:02:11,020 --> 00:02:12,310
We've seen some diagrams.
26
00:02:12,310 --> 00:02:16,300
We already feel more confident about it.
27
00:02:17,570 --> 00:02:23,990
And if not, please go back to the previous video and make sure that you understand that maybe replay
28
00:02:23,990 --> 00:02:30,440
the video and make sure you get the whole idea, because in this exercise, we are going to take it
29
00:02:30,590 --> 00:02:31,700
even further.
30
00:02:31,730 --> 00:02:39,220
OK, so what we are going to do now is we know how to work with print and one print, one to end it.
31
00:02:39,320 --> 00:02:45,710
Now, what I want us to do is simply to modify, to extend the previous exercise and to make it a little
32
00:02:45,710 --> 00:02:47,120
bit more unique.
33
00:02:48,460 --> 00:02:58,450
OK, am I saying more unique, what I actually mean is simply to like to take this exercise, OK, and
34
00:02:58,750 --> 00:03:00,750
print all the numbers.
35
00:03:00,790 --> 00:03:05,710
Let's say that's our example and I want to print all the numbers like this.
36
00:03:06,130 --> 00:03:08,050
So two, three, four.
37
00:03:08,080 --> 00:03:13,570
So printing all the numbers from a given N this is the input M equals to four.
38
00:03:14,140 --> 00:03:19,850
And I want to bring all the numbers from four up to one and then from one up to four.
39
00:03:21,040 --> 00:03:29,920
So one of the things and one of the options to do so was simple simply let's say we will print all the
40
00:03:29,920 --> 00:03:32,830
numbers from an to one.
41
00:03:33,040 --> 00:03:35,380
OK, let's just modify the main function.
42
00:03:36,760 --> 00:03:44,440
We'll simply assume that maybe we can use two functions that we've already created in one of our previous
43
00:03:44,440 --> 00:03:45,040
videos.
44
00:03:45,340 --> 00:03:48,590
So we just move this microphone, OK?
45
00:03:48,730 --> 00:03:54,790
And basically what we want to do is to print all the numbers from end to one and all the numbers from
46
00:03:54,790 --> 00:03:56,350
one, from one to M.
47
00:03:56,500 --> 00:03:58,660
So what do you think, guys?
48
00:03:58,690 --> 00:04:01,410
Do you think this solution will work?
49
00:04:01,420 --> 00:04:02,740
Do you think we've made it?
50
00:04:03,850 --> 00:04:04,960
Take a few seconds.
51
00:04:05,230 --> 00:04:09,130
Don't even run it on your cell, but you're on your own.
52
00:04:09,790 --> 00:04:13,710
Simply try to think what will be printed.
53
00:04:13,720 --> 00:04:14,050
Now.
54
00:04:15,160 --> 00:04:16,000
Do you think?
55
00:04:17,170 --> 00:04:22,980
That all the numbers will be printed like this or maybe some other version will be printed, something
56
00:04:23,000 --> 00:04:23,830
something else.
57
00:04:24,100 --> 00:04:25,710
So let's let's give it a try.
58
00:04:25,720 --> 00:04:30,670
So let's build and run it and enter a number.
59
00:04:30,700 --> 00:04:37,390
So let's enter for and what we can see is four, three, two, one, one, two, three, four.
60
00:04:38,860 --> 00:04:44,240
Although it's pretty, pretty, pretty similar to the result we expected to see.
61
00:04:44,650 --> 00:04:46,430
That's not actually the same.
62
00:04:46,990 --> 00:04:50,420
What's important here is basically to see this step.
63
00:04:50,440 --> 00:04:53,110
OK, so four, three, two, one, one, two, three, four.
64
00:04:53,410 --> 00:04:55,690
In here I have only just one.
65
00:04:55,870 --> 00:04:58,690
OK, I don't have this one repeated twice.
66
00:04:59,290 --> 00:05:05,560
And that's something we need to take care of, but we can't do it in these trivial solutions.
67
00:05:05,560 --> 00:05:08,020
So bear this in mind.
68
00:05:08,740 --> 00:05:10,200
So what do you think?
69
00:05:10,210 --> 00:05:16,820
What do you think, guys, we should do if we want simply to to eliminate this task?
70
00:05:17,500 --> 00:05:24,970
So my suggestion is, first of all, to start thinking of another function, OK, which will simply
71
00:05:24,970 --> 00:05:31,580
print into one and two M basically from these one to end without these duplication.
72
00:05:32,080 --> 00:05:34,330
So let's call it I don't know, it's called this function.
73
00:05:34,330 --> 00:05:34,700
I don't know.
74
00:05:34,720 --> 00:05:36,730
It's let's make it for simplicity.
75
00:05:36,730 --> 00:05:42,430
Call it final and final and to one.
76
00:05:43,800 --> 00:05:46,680
And and vice versa.
77
00:05:46,710 --> 00:05:52,260
OK, I don't know, something like that, and this function is going to receive him and now we have
78
00:05:52,260 --> 00:05:55,530
to construct these function from the start.
79
00:05:56,340 --> 00:06:00,190
What do you think should be different in such a case?
80
00:06:00,660 --> 00:06:07,340
So basically saying we can simply open up these Perinton to one and we can say the following thing.
81
00:06:07,360 --> 00:06:17,450
So let's say if any is greater or equal to one, then in this case, what will print is simply the before.
82
00:06:18,060 --> 00:06:20,050
Of brain printed percentage D and.
83
00:06:20,070 --> 00:06:23,130
And and then make the recursive call.
84
00:06:23,200 --> 00:06:28,470
OK, so that's the final viceversa all the time.
85
00:06:28,500 --> 00:06:30,340
And here do the same.
86
00:06:30,360 --> 00:06:30,600
Right.
87
00:06:30,630 --> 00:06:33,800
So basically something like that.
88
00:06:34,500 --> 00:06:36,400
And what do you think will happen now.
89
00:06:37,320 --> 00:06:38,830
What do you think will happen.
90
00:06:38,850 --> 00:06:44,820
So let's simply call this function and also see for ourselves what will happen.
91
00:06:44,970 --> 00:06:49,440
So for these num so let's build an R on it.
92
00:06:49,830 --> 00:06:52,890
What do you think will be the result goes think about it.
93
00:06:53,010 --> 00:06:56,240
Will it be the same as it was previously or not.
94
00:06:57,120 --> 00:06:59,430
I think it will be the same.
95
00:06:59,640 --> 00:07:04,490
OK, so let's run four and you can see for three to one, one, two, three, four.
96
00:07:05,130 --> 00:07:07,890
So that's one happening behind the scenes.
97
00:07:07,890 --> 00:07:12,300
Every time we call this function, we first of all print the value we call it with.
98
00:07:12,450 --> 00:07:20,490
In this case, it will be four and then we call it to the recursive call to three and then print three,
99
00:07:20,490 --> 00:07:21,570
print to print one.
100
00:07:21,840 --> 00:07:29,790
And then once we go back to R to where we start in the previous call, then we execute the print F for
101
00:07:29,790 --> 00:07:32,580
one, four, two, four, three and four for OK.
102
00:07:33,000 --> 00:07:41,490
So that's pretty much the same as it, as we were using like print and one and print one to n one after
103
00:07:41,490 --> 00:07:42,060
the other.
104
00:07:42,420 --> 00:07:51,090
OK, so we definitely understand that this solution is pretty much similar to what we are looking for,
105
00:07:51,330 --> 00:07:53,410
but not exactly the same.
106
00:07:53,970 --> 00:07:55,620
So what should be changed.
107
00:07:55,620 --> 00:07:56,000
Right.
108
00:07:56,010 --> 00:07:57,270
Changed right now.
109
00:07:57,440 --> 00:07:59,390
OK, what do you think guys?
110
00:07:59,490 --> 00:08:01,060
What do you think?
111
00:08:01,630 --> 00:08:10,650
So my suggestion is to look at the base case that we are having a hard time with in this base case is
112
00:08:10,650 --> 00:08:15,630
basically when we reach this place of one when and equals to one.
113
00:08:16,470 --> 00:08:23,680
So let's just try to focus on what happens when we reach the fact that and equals to one.
114
00:08:24,300 --> 00:08:30,250
So if any goes to one, we will still let's say let's say we will even be on two.
115
00:08:30,270 --> 00:08:31,460
OK, let's discuss two.
116
00:08:31,620 --> 00:08:37,950
So when we reach two, two is greater or equal than one, then we print two and then we call the function
117
00:08:37,950 --> 00:08:38,520
for one.
118
00:08:38,520 --> 00:08:41,370
And then we after we are done, we also print two.
119
00:08:41,940 --> 00:08:50,760
So we understand that somewhere in the time when any equals to one and we call this function, something
120
00:08:50,760 --> 00:08:53,730
shouldn't be done like this, like printed twice.
121
00:08:54,270 --> 00:08:57,720
But that's actually what happens behind the scenes right now.
122
00:08:58,140 --> 00:09:01,470
We call these function final and to one and vice versa.
123
00:09:01,710 --> 00:09:02,880
And we use here one.
124
00:09:03,210 --> 00:09:10,530
And if we ask if an equal or greater than one and in this case it's equals it equals to one, then we
125
00:09:10,530 --> 00:09:20,070
print one, then we ring, then we run of the following recursive call, which will be simply nothing
126
00:09:20,070 --> 00:09:24,870
and nothing will be printed for find and to one and vice versa for zero.
127
00:09:25,620 --> 00:09:33,040
And then we print one once again so we can do something like this to just like take you to eliminate
128
00:09:33,120 --> 00:09:40,140
it, because this will not print R the next time also for all the other numbers.
129
00:09:40,140 --> 00:09:40,490
Right.
130
00:09:40,500 --> 00:09:41,920
Not just for one.
131
00:09:42,570 --> 00:09:47,520
So what we would like to do is basically to split up this condition.
132
00:09:47,520 --> 00:09:52,290
Right, because this condition will be true basically for two main cases.
133
00:09:52,560 --> 00:10:00,540
The first case is if M is greater than one or if an equal to one.
134
00:10:00,690 --> 00:10:08,280
So let's simply split it up because we know that when N will be equal to one, we would not want to
135
00:10:08,280 --> 00:10:10,250
print it twice.
136
00:10:10,410 --> 00:10:12,270
So let's try to think about it.
137
00:10:12,390 --> 00:10:22,920
So if any equals to one, then in this case, what we would like to do is just to print its value and
138
00:10:23,040 --> 00:10:23,880
that's it.
139
00:10:24,000 --> 00:10:26,280
Without even any recursive call.
140
00:10:26,280 --> 00:10:32,760
Right, because we know that we don't have to like to make recursive calls for an equal to zero.
141
00:10:33,740 --> 00:10:41,030
And I hope that's clear to you guys and here Will simply mortified, right, we will simply mortified,
142
00:10:41,300 --> 00:10:44,420
if any, is greater or equal to one.
143
00:10:44,420 --> 00:10:48,770
Or basically you can also do like HCF or basically leave it as LS.
144
00:10:48,980 --> 00:10:51,150
OK, this will be pretty much the same.
145
00:10:51,740 --> 00:10:55,950
So if an equals to one, we will print it else.
146
00:10:56,030 --> 00:11:02,090
We will print every time like these parents who for and for three and three, two and two and so on
147
00:11:02,090 --> 00:11:03,260
and so forth.
148
00:11:04,390 --> 00:11:06,730
So let's just build and run it.
149
00:11:07,180 --> 00:11:09,550
So there you go, build and run.
150
00:11:09,580 --> 00:11:12,110
What happens here or what's the problem?
151
00:11:12,760 --> 00:11:14,620
What is the problem here?
152
00:11:14,620 --> 00:11:17,150
Printf percentage, the number.
153
00:11:17,500 --> 00:11:18,460
There was no no.
154
00:11:18,760 --> 00:11:20,760
There was just not right.
155
00:11:20,800 --> 00:11:22,690
So let's build and run it.
156
00:11:23,410 --> 00:11:27,070
And they did it in four equals.
157
00:11:27,070 --> 00:11:29,520
Two, four, three, two, one, two, three, four.
158
00:11:29,560 --> 00:11:30,140
Awesome.
159
00:11:30,520 --> 00:11:31,620
So it seems to work.
160
00:11:31,690 --> 00:11:35,440
It's also tested out for the value of five.
161
00:11:35,460 --> 00:11:39,400
So five, four, three, two, one, one, two, three, four, five.
162
00:11:39,400 --> 00:11:39,880
Awesome.
163
00:11:39,880 --> 00:11:45,220
No duplicates here exactly as we were requested.
164
00:11:46,310 --> 00:11:47,010
Amazing.
165
00:11:47,420 --> 00:11:54,530
So now what I want us to do is simply to let's just open it up and basically what I would like us to
166
00:11:54,530 --> 00:12:02,720
do now is just to see the flow of printing step by step and block by block to make sure that you understand
167
00:12:02,720 --> 00:12:04,280
it even further.
168
00:12:04,970 --> 00:12:08,050
OK, so let me just get my panel right.
169
00:12:08,420 --> 00:12:08,810
Awesome.
170
00:12:08,820 --> 00:12:14,060
So let us start with the final end to one and vice versa.
171
00:12:14,060 --> 00:12:16,110
Four a.m. equals to four.
172
00:12:16,130 --> 00:12:19,010
So here we will send now equals to four.
173
00:12:19,580 --> 00:12:20,410
And there you go.
174
00:12:20,510 --> 00:12:26,520
Basically, this will be our console where all the output is printed.
175
00:12:26,540 --> 00:12:28,360
So that's the console.
176
00:12:29,060 --> 00:12:34,900
And what we also will have is basically the first call for this function.
177
00:12:35,060 --> 00:12:35,470
Right.
178
00:12:35,660 --> 00:12:44,500
So final into one and vice versa for an equals two four, if any goes to one, not the case else, which
179
00:12:44,500 --> 00:12:46,460
you may be the case probably.
180
00:12:46,880 --> 00:12:53,590
We are going to print and to the screen we will print four and then we will call this function again.
181
00:12:53,780 --> 00:12:55,850
So we are calling this function.
182
00:12:56,870 --> 00:13:02,840
And equals to three, and these function does the same, it also prints three to the screen and there
183
00:13:02,840 --> 00:13:09,590
you go again and equals to two and you point two to the screen and then you call one and equals to one.
184
00:13:10,160 --> 00:13:10,550
Right.
185
00:13:10,550 --> 00:13:12,830
And you get to this point and equals to one.
186
00:13:12,830 --> 00:13:18,610
And if any equals to one, you print one to the screen here and that's it.
187
00:13:18,740 --> 00:13:26,180
These function here does not call any recursive calls, meaning that in the previous instance when and
188
00:13:26,180 --> 00:13:29,990
were was equal to two, we left from this line.
189
00:13:29,990 --> 00:13:30,260
Right.
190
00:13:30,260 --> 00:13:37,400
We printed two to the screen and then we called the final end to one and vice versa for the with the
191
00:13:37,400 --> 00:13:38,480
value of one.
192
00:13:39,390 --> 00:13:41,390
And we are done.
193
00:13:41,420 --> 00:13:45,200
These function call is executed, that's executed.
194
00:13:45,200 --> 00:13:47,690
One is printed and we came here.
195
00:13:47,690 --> 00:13:48,040
Right.
196
00:13:48,080 --> 00:13:50,870
That's four and equals to two where we left off.
197
00:13:50,870 --> 00:13:52,670
So we stop here now.
198
00:13:52,670 --> 00:13:55,580
We will print the value of two again.
199
00:13:56,030 --> 00:13:59,060
And once we are done, this function is over.
200
00:13:59,330 --> 00:14:03,770
Then we reach these final into one and vice versa for any equals to three.
201
00:14:03,980 --> 00:14:09,310
And that's the stopping point where we stopped to call this function with the value of two.
202
00:14:09,890 --> 00:14:16,370
Then we print the value of three and then the same for the value of four.
203
00:14:16,390 --> 00:14:18,700
So that's basically how how it works.
204
00:14:18,740 --> 00:14:26,720
Okay, so once we are back, we do not return anything, but we basically are simply continuing to execute
205
00:14:26,720 --> 00:14:29,610
the commands right where we left off.
206
00:14:30,470 --> 00:14:34,130
So let me know if this solution is clear to you guys.
207
00:14:35,000 --> 00:14:36,050
I hope it is.
208
00:14:36,050 --> 00:14:37,220
It's very important.
209
00:14:37,220 --> 00:14:44,900
Recursions concept is not easy, especially when you try to mix things into one question and to make
210
00:14:44,900 --> 00:14:49,070
it more difficult and to think of different base conditions.
211
00:14:50,100 --> 00:14:56,670
And when you should and when you shouldn't make the recursive calls and so on and so forth, so I hope
212
00:14:56,670 --> 00:15:02,730
this is clear to you guys, let me know if you like this video and you have any questions, leave some
213
00:15:02,730 --> 00:15:03,290
review.
214
00:15:03,780 --> 00:15:06,900
And until next time, I wish you a great day.
215
00:15:06,960 --> 00:15:07,910
Great time.
216
00:15:08,370 --> 00:15:09,760
Keep on practicing.
217
00:15:09,810 --> 00:15:11,150
Keep on moving forward.
218
00:15:11,160 --> 00:15:13,230
And you were about to succeed.
219
00:15:13,500 --> 00:15:14,580
I'll see you next time.
19988
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.