Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, so now that you're done trying to solve this exercise, let's start solving the sex or shall
exercise together.
So my name is Lisa Lisa's Alphatech, and here we go.
And the first thing that I want you to think about is, first of all, they are the fact that you could
also solve this kind of question by using the interactive approach and by just using some wire loop.
And as long as inside the while loop, you're going to receive some input from the user.
So as long as this input does not equal to this dollar sign, but what we are requested to do here is
basically to work with the recursive approach with by using recursion functions.
So first of all, what we need to understand here is just what will be the type of this function.
So we know that the name of the function can be decided and it can be like, I don't know, total characters,
OK, characters.
And basically, this function is going to receive what is going to receive this function is going to
receive a given character.
So that's why we are going to specify CHA and let's use clear character.
Awesome.
So now the last question for the signature that remains is what will be the function type?
I mean, should the function be of a character, a type, maybe a float type or maybe an integer?
So what do you think, guys?
What should be the function type?
So to be honest.
It's pretty obvious because we said that we want to calculate the total amount of appearances of a given
character that we pass here in a given sequence that will be inserted inside the loop or the body of
this function.
Good.
And now let us talk about the body of the function.
So basically saying we need to take care of three things now, we need to take care of the staffing
condition or base condition, staffing condition or base condition, however you call it base condition,
then we need to talk about the main logic, which will be like how we make our recursive recursive calls
on what principle and what idea.
So first of all, what we need to understand is some sort of a diagram.
Let's try to maybe maybe try together.
OK, so let's take a look at this example, right, so we have a character to see and we would like
to call this function OK?
We would like to call this function.
Let's let me just throw it.
Here, OK, so we would like to call this function, let's say, from the main.
And we would like to call this function and to give here a character, let's say, see, OK, so sees
the character.
That's the first call to the title character is functions, I will say, like see title characters.
And inside of these function, we will have to basically to check out two things we will have to check
out if we are not on the final or basically no stopping condition.
If that's the case, if we received a stopping condition, we would like to simply to return to zero.
Otherwise, we would like to check if the input received from the user on this iteration equals to this
character or not.
If yes, then in this case we would like this function, this function call to return one plus the result
of another call from the user.
So that's why we will need to call in another function.
OK, another instance of these function.
And here we will also receive some input from the user and once again, we will have to check if the
condition of the base case is not satisfied.
And if that's the case, we will have to return something, OK?
It may be if the input of the user equals to to the character or not, if it if it does equal to the
character, then we will return out of this instance one plus the result of the call to another instance
and so on.
Otherwise we will need simply to return the result of the next call for the next instance, for the
next element in the sequence that the user is going to specify, OK, we are going to do that until
some base condition has been met.
OK, all of these things right here, OK, all of these things are basically recursive recursive calls.
OK, so we are calling recursively to the one and the same function and run multiple instances of these
function, one after the other, applying some logic, some idea behind it to find the solution.
Awesome.
So now let us go back and start writing the code.
OK, and basically the first thing that we have to do is we know that on every function call we are
going to read some data from the user.
And in this case, we are going to read some data of a character, a type.
So char input user.
All right.
And now we will print some nice message to the user and we will ask him, I don't know.
Please, please, please answer a character.
Is that clear?
Then we will read this character and story.
Where are we going to store it?
Inside of these inside of these characters are variable in user.
All right.
Is that clear?
So far.
And something very important to note here is that is opposed to one of our previous examples here,
we read a character from the user and if we are going to insert a and then press enter and then insert
B and then presenter and then insert C and presenter and so on, that may cause us some problems.
OK, so you can try it by yourself, but you will see that something simply stops working.
And the reason is because we are going every time we are going to press a and then enter then A is going
to get into the input user and then it will be like it will take afterwards the answer a character and
story in the next function call and then only on the third function call you will store the B value,
OK, and that can cause you problems because that's not something that you want.
You don't want to get every time some unnecessary characters and to make comparisons between them,
just like the enter button.
And that's exactly why you can use here just as a temporary solution.
Get Char, OK, and these get char will be used to kind of to kind of to kind of leave the next new
line, OK, to kind of read it and remove it from the stream of the input and then to be prepared after
reading a story and getting to input user and then to read and to kind of leave the new line and then
to be prepared for the next iteration, for the next recursive call to store the B also inside the input
user.
So.
Hopefully that's clear and it's not so complicated, but with that being said, let's proceed to the
logic, OK?
So that's basically the part where we read from the user.
And now comes the interesting part.
And we are going to talk about, first of all, the stopping condition and the base case.
So regarding the base case, what do you think should be the base case in this case?
And that's actually very, very simple because we know that the user is going to insert characters one
after the other in this sequence until he answers he or she inserts a dollar sign, a dollar character.
So that will be our stopping condition.
So if input user equals to a dollar, then in this case we know that, listen, we got to stop.
We don't want to do any more recursive calls and that's why we are going to return simply zero.
OK.
Is that clear?
And regarding the recursive calls, that's something that we have to do if the user has not specified
the terminating characters.
So now we will have to decide how to to make the recursive call.
So if if the input user equals to the character that we are checking with, for example, like in this
case, to see if that's the case, then what we are going to do is simply to return basically of the
addition of this element in the sequence, because it's it's the same as the character.
So it's one instance, one one additional instance of this character in the sequence.
So that's why we are going to return one plus a recursive call for another element in the sequence.
So this will be total characters with this character.
OK, is that clear?
And if this condition happens not to be true, meaning it happens to be false, that the input user
does not equal to these given character, then in this case, what you should return is basically the
total characters that will be received from the next call of very encouraging function.
So once again, a character here and there you go.
So yeah, this is it for this solution.
Now, before we leave, OK, what I want us to do is let's just run this example and see what happens
behind the scenes.
OK, so in the main function, you call these first instance and you pass it a C, OK, this function
goes like this, OK?
It gets some input from you, the user.
In this case, that's going to be a OK, so let me change the color.
So that's going to be that's going to be the letter A once you received the letter A..
This function, this instance is going to return one, plus the total characters for these characters.
So a one plus this thing that we still don't know yet.
So that's why we call the next the next recursive call in here will receive B, OK, B, because that's
the example.
And since we know that B.
Oops, sorry, it's not one plus sorry, sorry, it's just the result.
OK, we are just going to return this, OK, from the first recursive call because the input user does
not equal to character.
So this line twenty six is not going to be executed, but rather we will simply execute the twenty seventh
line.
All right, so now we are going also to run these to the next call, also does not return anything.
It does not add anything.
Then we call another function instancing to this instance.
We pass a letter C and this function will return.
Right, because this F condition is going to be executed.
And in this instance.
Right, vestor just like a whole a whole different standalone instances for each of these squares that
you can see right here.
So here we are going to return one plus the result of another call in here.
In this call, we are going to receive again a right.
I'm going like with this example and we are going to return the result of another call.
OK, so here is another call here.
We are going to receive C and now we are going to return here one plus the result of another call.
Maybe that's going to be our last call.
And here we receive what what are we going to receive here?
We are going to receive the dollar sign.
So that's it.
And we know that if the input user equals to these dollar sign, then return zero.
So from here to here, we are going to return zero.
So basically, this is going to be zero one zero is going to be returned here.
So that's why we will have here one one is going to return here and that's what we are going to have,
one plus one.
And from here we are going to have two because one plus one equals to two even at these times.
And then from here to here, we are going also to return to and the final result that is going to be
returned to the main function.
Who called the first instance of this recursive function is the value of two.
So this will mean that we have two instances that we had two instances, two occurrences of the letters
C in the sequence received in the sequence of characters received from the user.
OK, guys, so is that clear, this example, all of this stopping condition and the recursive call,
why we did hear one plus the result or one plus another, a deeper recursive call and why here?
We did just the recursive call without adding one.
OK, because that's that's that's the time when this condition happens to be true.
That's when exactly when we would like to add just one OK, one occurrence.
That's why we want to take it into account, into consideration.
And in the twenty seventh line, we we know that that's not the case.
So this, this instance call for this function does not contribute anything to the total amount of characters
appear in the sequence.
Yes, who I think we are good.
What do you think?
I think we've done pretty nice example.
OK, we have sharpened our knowledge, we have done a good a good exercise who I think I'm going to
bring something.
This exercise was really I know, not an easy one, but I think we covered everything correctly here.
So with that being said, check out once again, run some of call from the main.
Try to execute this with a few examples of your own.
Make sure you understand every step in every in every nuance in this solution.
And if you still have any questions, feel free to ask them.
And until next time.
My name is Rod.
This is Alphatech or better say Alphatech Academy.
And until next time.
I'll see you then.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.