Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,630 --> 00:00:08,000
All right, so now that you're done trying to solve this exercise, let's start solving the sex or shall
2
00:00:08,010 --> 00:00:10,320
exercise together.
3
00:00:10,350 --> 00:00:14,270
So my name is Lisa Lisa's Alphatech, and here we go.
4
00:00:15,360 --> 00:00:21,450
And the first thing that I want you to think about is, first of all, they are the fact that you could
5
00:00:21,450 --> 00:00:29,850
also solve this kind of question by using the interactive approach and by just using some wire loop.
6
00:00:29,850 --> 00:00:36,570
And as long as inside the while loop, you're going to receive some input from the user.
7
00:00:36,640 --> 00:00:44,430
So as long as this input does not equal to this dollar sign, but what we are requested to do here is
8
00:00:44,430 --> 00:00:51,430
basically to work with the recursive approach with by using recursion functions.
9
00:00:51,870 --> 00:01:00,080
So first of all, what we need to understand here is just what will be the type of this function.
10
00:01:00,450 --> 00:01:08,760
So we know that the name of the function can be decided and it can be like, I don't know, total characters,
11
00:01:08,990 --> 00:01:10,530
OK, characters.
12
00:01:11,750 --> 00:01:18,050
And basically, this function is going to receive what is going to receive this function is going to
13
00:01:18,050 --> 00:01:19,750
receive a given character.
14
00:01:19,760 --> 00:01:24,500
So that's why we are going to specify CHA and let's use clear character.
15
00:01:25,320 --> 00:01:25,840
Awesome.
16
00:01:26,250 --> 00:01:32,860
So now the last question for the signature that remains is what will be the function type?
17
00:01:33,480 --> 00:01:39,260
I mean, should the function be of a character, a type, maybe a float type or maybe an integer?
18
00:01:39,780 --> 00:01:41,160
So what do you think, guys?
19
00:01:41,220 --> 00:01:43,200
What should be the function type?
20
00:01:44,060 --> 00:01:45,380
So to be honest.
21
00:01:46,260 --> 00:01:52,860
It's pretty obvious because we said that we want to calculate the total amount of appearances of a given
22
00:01:52,860 --> 00:02:00,870
character that we pass here in a given sequence that will be inserted inside the loop or the body of
23
00:02:00,870 --> 00:02:01,610
this function.
24
00:02:02,280 --> 00:02:02,700
Good.
25
00:02:03,060 --> 00:02:07,260
And now let us talk about the body of the function.
26
00:02:08,050 --> 00:02:16,240
So basically saying we need to take care of three things now, we need to take care of the staffing
27
00:02:16,240 --> 00:02:26,080
condition or base condition, staffing condition or base condition, however you call it base condition,
28
00:02:27,370 --> 00:02:37,750
then we need to talk about the main logic, which will be like how we make our recursive recursive calls
29
00:02:37,750 --> 00:02:39,940
on what principle and what idea.
30
00:02:40,780 --> 00:02:47,070
So first of all, what we need to understand is some sort of a diagram.
31
00:02:47,080 --> 00:02:49,960
Let's try to maybe maybe try together.
32
00:02:51,130 --> 00:02:56,770
OK, so let's take a look at this example, right, so we have a character to see and we would like
33
00:02:56,770 --> 00:02:58,460
to call this function OK?
34
00:02:58,480 --> 00:03:00,210
We would like to call this function.
35
00:03:00,220 --> 00:03:01,930
Let's let me just throw it.
36
00:03:02,710 --> 00:03:06,730
Here, OK, so we would like to call this function, let's say, from the main.
37
00:03:07,640 --> 00:03:14,720
And we would like to call this function and to give here a character, let's say, see, OK, so sees
38
00:03:14,720 --> 00:03:15,390
the character.
39
00:03:15,410 --> 00:03:22,070
That's the first call to the title character is functions, I will say, like see title characters.
40
00:03:23,140 --> 00:03:30,430
And inside of these function, we will have to basically to check out two things we will have to check
41
00:03:30,430 --> 00:03:37,600
out if we are not on the final or basically no stopping condition.
42
00:03:38,110 --> 00:03:43,390
If that's the case, if we received a stopping condition, we would like to simply to return to zero.
43
00:03:43,840 --> 00:03:52,210
Otherwise, we would like to check if the input received from the user on this iteration equals to this
44
00:03:52,210 --> 00:03:53,380
character or not.
45
00:03:53,950 --> 00:04:03,730
If yes, then in this case we would like this function, this function call to return one plus the result
46
00:04:03,730 --> 00:04:06,420
of another call from the user.
47
00:04:06,430 --> 00:04:09,700
So that's why we will need to call in another function.
48
00:04:09,750 --> 00:04:12,340
OK, another instance of these function.
49
00:04:13,600 --> 00:04:19,510
And here we will also receive some input from the user and once again, we will have to check if the
50
00:04:19,510 --> 00:04:23,370
condition of the base case is not satisfied.
51
00:04:23,530 --> 00:04:28,210
And if that's the case, we will have to return something, OK?
52
00:04:28,240 --> 00:04:38,050
It may be if the input of the user equals to to the character or not, if it if it does equal to the
53
00:04:38,050 --> 00:04:44,830
character, then we will return out of this instance one plus the result of the call to another instance
54
00:04:44,830 --> 00:04:45,500
and so on.
55
00:04:45,850 --> 00:04:52,480
Otherwise we will need simply to return the result of the next call for the next instance, for the
56
00:04:52,480 --> 00:05:00,130
next element in the sequence that the user is going to specify, OK, we are going to do that until
57
00:05:00,280 --> 00:05:03,260
some base condition has been met.
58
00:05:03,280 --> 00:05:13,240
OK, all of these things right here, OK, all of these things are basically recursive recursive calls.
59
00:05:13,550 --> 00:05:20,920
OK, so we are calling recursively to the one and the same function and run multiple instances of these
60
00:05:20,920 --> 00:05:27,040
function, one after the other, applying some logic, some idea behind it to find the solution.
61
00:05:28,460 --> 00:05:29,070
Awesome.
62
00:05:29,090 --> 00:05:34,140
So now let us go back and start writing the code.
63
00:05:34,250 --> 00:05:42,290
OK, and basically the first thing that we have to do is we know that on every function call we are
64
00:05:42,290 --> 00:05:46,000
going to read some data from the user.
65
00:05:46,430 --> 00:05:51,770
And in this case, we are going to read some data of a character, a type.
66
00:05:51,770 --> 00:05:54,350
So char input user.
67
00:05:54,530 --> 00:05:55,130
All right.
68
00:05:56,600 --> 00:06:02,990
And now we will print some nice message to the user and we will ask him, I don't know.
69
00:06:03,020 --> 00:06:10,130
Please, please, please answer a character.
70
00:06:11,060 --> 00:06:11,860
Is that clear?
71
00:06:12,710 --> 00:06:15,920
Then we will read this character and story.
72
00:06:16,190 --> 00:06:18,230
Where are we going to store it?
73
00:06:18,470 --> 00:06:25,250
Inside of these inside of these characters are variable in user.
74
00:06:25,700 --> 00:06:26,300
All right.
75
00:06:26,400 --> 00:06:27,120
Is that clear?
76
00:06:27,140 --> 00:06:27,680
So far.
77
00:06:28,580 --> 00:06:38,110
And something very important to note here is that is opposed to one of our previous examples here,
78
00:06:38,120 --> 00:06:46,660
we read a character from the user and if we are going to insert a and then press enter and then insert
79
00:06:46,700 --> 00:06:53,180
B and then presenter and then insert C and presenter and so on, that may cause us some problems.
80
00:06:53,220 --> 00:06:58,930
OK, so you can try it by yourself, but you will see that something simply stops working.
81
00:06:59,270 --> 00:07:08,960
And the reason is because we are going every time we are going to press a and then enter then A is going
82
00:07:08,960 --> 00:07:17,450
to get into the input user and then it will be like it will take afterwards the answer a character and
83
00:07:17,450 --> 00:07:25,390
story in the next function call and then only on the third function call you will store the B value,
84
00:07:25,400 --> 00:07:29,800
OK, and that can cause you problems because that's not something that you want.
85
00:07:29,810 --> 00:07:36,200
You don't want to get every time some unnecessary characters and to make comparisons between them,
86
00:07:36,200 --> 00:07:38,290
just like the enter button.
87
00:07:39,470 --> 00:07:44,510
And that's exactly why you can use here just as a temporary solution.
88
00:07:44,510 --> 00:07:56,600
Get Char, OK, and these get char will be used to kind of to kind of to kind of leave the next new
89
00:07:56,600 --> 00:08:06,050
line, OK, to kind of read it and remove it from the stream of the input and then to be prepared after
90
00:08:06,050 --> 00:08:13,580
reading a story and getting to input user and then to read and to kind of leave the new line and then
91
00:08:13,580 --> 00:08:20,060
to be prepared for the next iteration, for the next recursive call to store the B also inside the input
92
00:08:20,060 --> 00:08:20,440
user.
93
00:08:20,450 --> 00:08:20,690
So.
94
00:08:21,850 --> 00:08:27,520
Hopefully that's clear and it's not so complicated, but with that being said, let's proceed to the
95
00:08:27,520 --> 00:08:28,190
logic, OK?
96
00:08:28,210 --> 00:08:31,150
So that's basically the part where we read from the user.
97
00:08:31,150 --> 00:08:33,610
And now comes the interesting part.
98
00:08:33,610 --> 00:08:39,670
And we are going to talk about, first of all, the stopping condition and the base case.
99
00:08:40,800 --> 00:08:45,950
So regarding the base case, what do you think should be the base case in this case?
100
00:08:46,950 --> 00:08:54,180
And that's actually very, very simple because we know that the user is going to insert characters one
101
00:08:54,180 --> 00:09:02,570
after the other in this sequence until he answers he or she inserts a dollar sign, a dollar character.
102
00:09:02,940 --> 00:09:05,450
So that will be our stopping condition.
103
00:09:05,460 --> 00:09:14,180
So if input user equals to a dollar, then in this case we know that, listen, we got to stop.
104
00:09:14,220 --> 00:09:21,930
We don't want to do any more recursive calls and that's why we are going to return simply zero.
105
00:09:22,590 --> 00:09:23,010
OK.
106
00:09:24,100 --> 00:09:24,890
Is that clear?
107
00:09:25,810 --> 00:09:34,510
And regarding the recursive calls, that's something that we have to do if the user has not specified
108
00:09:34,900 --> 00:09:36,660
the terminating characters.
109
00:09:36,660 --> 00:09:42,510
So now we will have to decide how to to make the recursive call.
110
00:09:42,970 --> 00:09:54,280
So if if the input user equals to the character that we are checking with, for example, like in this
111
00:09:54,280 --> 00:10:03,910
case, to see if that's the case, then what we are going to do is simply to return basically of the
112
00:10:03,910 --> 00:10:11,130
addition of this element in the sequence, because it's it's the same as the character.
113
00:10:11,140 --> 00:10:17,100
So it's one instance, one one additional instance of this character in the sequence.
114
00:10:17,110 --> 00:10:25,270
So that's why we are going to return one plus a recursive call for another element in the sequence.
115
00:10:25,450 --> 00:10:29,500
So this will be total characters with this character.
116
00:10:29,680 --> 00:10:30,990
OK, is that clear?
117
00:10:31,630 --> 00:10:38,800
And if this condition happens not to be true, meaning it happens to be false, that the input user
118
00:10:38,800 --> 00:10:44,770
does not equal to these given character, then in this case, what you should return is basically the
119
00:10:44,770 --> 00:10:52,730
total characters that will be received from the next call of very encouraging function.
120
00:10:52,750 --> 00:10:57,120
So once again, a character here and there you go.
121
00:10:58,510 --> 00:11:02,150
So yeah, this is it for this solution.
122
00:11:02,470 --> 00:11:09,250
Now, before we leave, OK, what I want us to do is let's just run this example and see what happens
123
00:11:09,250 --> 00:11:10,190
behind the scenes.
124
00:11:10,210 --> 00:11:17,980
OK, so in the main function, you call these first instance and you pass it a C, OK, this function
125
00:11:17,980 --> 00:11:18,960
goes like this, OK?
126
00:11:18,970 --> 00:11:21,590
It gets some input from you, the user.
127
00:11:21,610 --> 00:11:26,650
In this case, that's going to be a OK, so let me change the color.
128
00:11:27,100 --> 00:11:34,540
So that's going to be that's going to be the letter A once you received the letter A..
129
00:11:34,720 --> 00:11:42,490
This function, this instance is going to return one, plus the total characters for these characters.
130
00:11:42,490 --> 00:11:48,200
So a one plus this thing that we still don't know yet.
131
00:11:48,310 --> 00:11:56,650
So that's why we call the next the next recursive call in here will receive B, OK, B, because that's
132
00:11:56,650 --> 00:11:57,430
the example.
133
00:11:57,880 --> 00:11:59,710
And since we know that B.
134
00:12:00,710 --> 00:12:05,870
Oops, sorry, it's not one plus sorry, sorry, it's just the result.
135
00:12:05,900 --> 00:12:13,070
OK, we are just going to return this, OK, from the first recursive call because the input user does
136
00:12:13,070 --> 00:12:14,340
not equal to character.
137
00:12:14,360 --> 00:12:21,800
So this line twenty six is not going to be executed, but rather we will simply execute the twenty seventh
138
00:12:21,800 --> 00:12:22,220
line.
139
00:12:23,080 --> 00:12:31,990
All right, so now we are going also to run these to the next call, also does not return anything.
140
00:12:33,580 --> 00:12:34,890
It does not add anything.
141
00:12:35,200 --> 00:12:41,170
Then we call another function instancing to this instance.
142
00:12:41,170 --> 00:12:47,230
We pass a letter C and this function will return.
143
00:12:47,260 --> 00:12:50,860
Right, because this F condition is going to be executed.
144
00:12:50,860 --> 00:12:51,820
And in this instance.
145
00:12:51,820 --> 00:12:58,960
Right, vestor just like a whole a whole different standalone instances for each of these squares that
146
00:12:58,960 --> 00:12:59,960
you can see right here.
147
00:13:00,310 --> 00:13:05,800
So here we are going to return one plus the result of another call in here.
148
00:13:05,800 --> 00:13:08,950
In this call, we are going to receive again a right.
149
00:13:08,950 --> 00:13:14,770
I'm going like with this example and we are going to return the result of another call.
150
00:13:14,830 --> 00:13:17,470
OK, so here is another call here.
151
00:13:17,470 --> 00:13:25,530
We are going to receive C and now we are going to return here one plus the result of another call.
152
00:13:25,780 --> 00:13:28,440
Maybe that's going to be our last call.
153
00:13:29,200 --> 00:13:33,250
And here we receive what what are we going to receive here?
154
00:13:33,280 --> 00:13:35,870
We are going to receive the dollar sign.
155
00:13:35,920 --> 00:13:36,880
So that's it.
156
00:13:37,600 --> 00:13:43,660
And we know that if the input user equals to these dollar sign, then return zero.
157
00:13:43,690 --> 00:13:47,080
So from here to here, we are going to return zero.
158
00:13:47,140 --> 00:13:52,710
So basically, this is going to be zero one zero is going to be returned here.
159
00:13:52,720 --> 00:13:58,960
So that's why we will have here one one is going to return here and that's what we are going to have,
160
00:13:58,960 --> 00:13:59,710
one plus one.
161
00:14:00,010 --> 00:14:06,190
And from here we are going to have two because one plus one equals to two even at these times.
162
00:14:07,090 --> 00:14:13,180
And then from here to here, we are going also to return to and the final result that is going to be
163
00:14:13,180 --> 00:14:15,400
returned to the main function.
164
00:14:15,400 --> 00:14:20,760
Who called the first instance of this recursive function is the value of two.
165
00:14:21,190 --> 00:14:30,460
So this will mean that we have two instances that we had two instances, two occurrences of the letters
166
00:14:30,460 --> 00:14:38,860
C in the sequence received in the sequence of characters received from the user.
167
00:14:39,460 --> 00:14:47,320
OK, guys, so is that clear, this example, all of this stopping condition and the recursive call,
168
00:14:47,590 --> 00:14:55,630
why we did hear one plus the result or one plus another, a deeper recursive call and why here?
169
00:14:55,630 --> 00:15:00,490
We did just the recursive call without adding one.
170
00:15:00,850 --> 00:15:07,470
OK, because that's that's that's the time when this condition happens to be true.
171
00:15:07,780 --> 00:15:14,540
That's when exactly when we would like to add just one OK, one occurrence.
172
00:15:14,560 --> 00:15:18,610
That's why we want to take it into account, into consideration.
173
00:15:18,830 --> 00:15:24,880
And in the twenty seventh line, we we know that that's not the case.
174
00:15:24,890 --> 00:15:34,780
So this, this instance call for this function does not contribute anything to the total amount of characters
175
00:15:34,780 --> 00:15:36,910
appear in the sequence.
176
00:15:38,450 --> 00:15:41,310
Yes, who I think we are good.
177
00:15:41,330 --> 00:15:42,170
What do you think?
178
00:15:43,490 --> 00:15:46,070
I think we've done pretty nice example.
179
00:15:46,290 --> 00:15:55,910
OK, we have sharpened our knowledge, we have done a good a good exercise who I think I'm going to
180
00:15:55,910 --> 00:15:56,750
bring something.
181
00:15:57,080 --> 00:16:04,990
This exercise was really I know, not an easy one, but I think we covered everything correctly here.
182
00:16:05,840 --> 00:16:13,370
So with that being said, check out once again, run some of call from the main.
183
00:16:13,370 --> 00:16:18,820
Try to execute this with a few examples of your own.
184
00:16:19,130 --> 00:16:26,810
Make sure you understand every step in every in every nuance in this solution.
185
00:16:27,200 --> 00:16:30,450
And if you still have any questions, feel free to ask them.
186
00:16:30,470 --> 00:16:31,640
And until next time.
187
00:16:31,640 --> 00:16:32,660
My name is Rod.
188
00:16:32,840 --> 00:16:36,500
This is Alphatech or better say Alphatech Academy.
189
00:16:37,070 --> 00:16:38,810
And until next time.
190
00:16:38,810 --> 00:16:39,710
I'll see you then.
19527
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.