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
So basically, what we need to do in these function.
OK, let's try to break it down into smaller pieces and try to figure out what should be the recursive
calls.
What should be the stopping condition and so on and so forth.
So in this exercise, I think that first of all, once you realize that in order to solve the bigger
problem, we need to split it up to smaller problems.
And if that's the case, then probably we will reach someplace where we can say that we reached some
stopping condition and there is no reason as to why we would like to make further recursive calls.
And with this type of exercises, probably whenever we need to work with every digit, then on every
recursive call, then we will get something like this right divided by 10.
And then once again divided, let's do it like this.
And then once again divided by 10 and so on and so forth until we reach some stopping condition when
there is no reason to actually divide it by 10.
And this stopping condition may simply be.
Are specified as, for example, reaching out a digit number when number equals or less than.
Nine.
Right.
Meaning whenever we will reach numb to be of one digit, then we can say that that will be the stopping
condition because there is no reason as to why we would like to divide it even further.
OK, so one digit so our stopping condition will probably be whenever we reach one digit.
So let's take an example of here.
The first example we have now equals to one two four and also digit.
Equals to what equals two to.
So if we want to count how many times the digit to abusing in these number, then we definitely need
to know how many times or basically even if we the number of appearances was even or odd in this number
of one two right with digit equals 82.
So if we know that the if it's odd or even number of occurrences in this number, then we can conclude
if it's even or in this number based on the result received from these recursive call and comparing
the right most digit.
Also, these can be done here comparing now equals to one and digit equals two to.
So if we reached these base condition, right, these base statement, then we can say if digit equals
to numb, then return, what then return?
Probably the indicator of that we have an odd occurrences.
Otherwise we turn zero, and every time we will return something, we will say that the number of occurrences
inform the recursive calls even or odd.
And what about the rightmost digit, the right?
Most digit, of course, the right, most digit, which is still, for example.
And if it changes the total occurrences in this number?
OK, so do you get the idea?
OK.
So that's exactly what we are going to do and.
I just, yeah, I suggest let's take a look at what will happen in sight of this example.
So we compare now one with Egypt, and we know that the total of occurrences of Egypt in these numbers
in this instance will be what?
We'll be even.
Right, because zero.
That's why we will return, what should we return?
We return one, then we ask the following question if there are the total occurrences, if it's even
or odd so far is one and the digit equals to this digit, then this means that the total occurrences
is now not even.
But it's going to be odd because we will have now one appearance.
So these function, since it's odd, is going to return zero and then we are going to say we have zero
occurrences.
OK, and meaning it's an odd number of occurrences.
That's why the function returns zero.
And here we compare four with two.
And the result is that they are not equal, meaning they do not change the status of the odd or even
total of occurrences.
OK, so the final result will be zero.
That's how it goes.
OK, if you need to refresh it a little bit, swipe the video to the left.
Watch it again.
OK, so now that we know the idea behind it, let's try to solve this exercise, OK?
The user exercises are actually not so easy, but I think we are going to do good.
So the type of the of the function is going to be int because we return zero or one.
We can optimize it, of course, but that's not the whole idea behind practicing the recursion.
Let's go with the basic one.
Let's create the function and what should be the function name.
Let's call it digits, even appearances.
OK, even all the appearances, whatever these function will receive numb, numb.
And also these functions should receive in what int Egypt?
And one of these function is going to check let us start with the base condition, we will ask the following
question if numb is less than 10, right?
Meaning if numb is one digit, if numb is one digit, then in this case we will ask another question
if we already know if we already know that numb eyes of one digit, we can ask if numb does not equal
two digit.
Meaning if it does not appear in them, then in this case, based on the requirements of this exercise,
we should return what we should return one.
Why one?
Because the total number of occurrences is zero and zero is even considered to be even right.
Zero appearances.
And that means that we are working with are even appearances, right, even appearances.
OK.
And the ill spark for these if condition, right?
Meaning when NEM equals two digit.
OK, so this statement will be executed whenever name equals two digit, then we should return zero.
OK, so that means that zero will be returned when the digit appears appears once in NUM.
And once meaning.
All appearances, right?
So there is even and odd the difference is one can be divided by two without their remainder, while
the other will be with the remainder, OK, even more awesome.
So that's about the base case, that's about the base case, and it will be executed whenever NAM is
less than 10.
What will happen otherwise?
So we are interested in.
Returning, whether the total number of appearances is even or odd, and for that, we need to split
the problem into smaller parts and we need later on before these function, before these incidents will
return the result.
We need to know what is the status for all the other idiots except for the rightmost OK during the recursive
calls whenever we divide it by 10.
So for that, we will create additional variable that will be used for our assistance and we will call
it into total appearance appearances so far.
And total appearances so far will be calculated how by making their recursive call digits, even appearances
for now divided by 10 right without the right most digit.
And also we have what with the digit itself?
All right.
So total appearances so far, it can be either zero or one.
OK.
Total appearances so far can be zero or one.
This means OK, pay very close attention.
This means the status of total of occurrences for numb, divided by 10 from this point and further as
you dive into making the recursive calls in the recursive functions.
You feel me so far came with me.
Awesome.
And now we are going to ask very, very simple questions.
We are going to ask Eve, so it's all appearances so far equals to one.
If that's the case, then what it means is that so far we have even appearances.
OK?
So far we had even.
Appearances of Digit in numb, right in numb, divided by 10.
Let's see, OK, for at least instance.
And also, if that's the case, then what we need to know is if no modular 10 meaning the rightmost
digit that we did not take into account because the total appearances so far is related to number divided
by 10.
So we ask if not modular 10 equals two digit meaning we compare right.
We compare the right most the right, most digit in NAM in this appearance with the digit itself.
If that's the case, then all it means that what we need to swap or basically change the total appearances
because we know that so far without the right most digit, we have even number of appearances.
And if we found out that there is another appearance right for the right most digit, meaning the digit
equals to the right, most digits inside number, then it means that we need simply to change it.
How do we change it?
We return what we return zero, right?
If it was one, we return zero else.
Meaning.
Come again, sorry.
Else that means when.
What that means?
Nothing changed, right?
Nothing will change because.
Digit does not equal to now modular 10.
OK, so that makes sense.
So we had total appearances so far.
Of even number of occurrences, and we ask a simple question, if not modular 10 equals two digit.
Then returns zero, changed the status, and if not, then we simply return whatever it was previously.
So we return one.
OK, but if we had else, the ills section is related to want to win total appearances.
Appearances so far was equal to zero, then in this case, what should happen is if no more gelatin
equals two digit.
Then in this case, what we should try to do is to return what the return one because we want to change
the status, if so far we had an odd occurrence, an odd number of occurrences of digits and say no,
now we have added the digit equals to the rightmost digit number, then switch else returns zero.
OK.
Is it clear?
And that's it.
That's basically it.
That will be that would be the solution.
That would be the solution, guys.
That's.
All about it.
OK.
And I want to show you a simple example for both of these two options, OK?
Let's say we started with NAM equal now equals to one hundred and twenty four.
So we have now equals to.
One hundred and twenty four now equals to one 24 that Egypt is going to be the same, so that's fine.
So here we count the total appearances and we don't know yet what it is.
So we make the recursive call for now equals.
212 then we try to calculate this line right here.
This one we tried to calculate, we say total equals to what something we don't know.
We make another recursive call for a now equals to one.
And then we ask a simple question, if army is less than 10, that's the case.
If no does not equal two digit and it doesn't equal two digits since digit equals to two now equals
to one.
So these function is going to return what is going to return one where he's going to return one inside
of here.
So here will be one.
Then what we need to complete inside of this instance of the function is that we know total appearances
so far equals to one, and we ask if it equals to one.
That then means that so far we have even appearances of digits in number divided by 10.
And that's the case.
And we ask, if not modulo 10 inside of these, it's not modular 10 equals to two.
If it equals the digit and they is digit two.
That's the case, then we should change the status from one to zero and return it so we return zero.
That means that we know that there is an odd number of occurrences so far number of occurrences.
OK.
And then finally, they started we finished here, we ask if it equals to zero, right, because it
does not equal to one, so it equals to zero.
That's our else.
Then we ask, if not divided by 10 equals two digit and it's not the case then returned.
What was it?
One else return is zero.
Don't change the status.
So the final result will be zero exactly as requested.
And the same thing with the same steps, I will not even run, it's OK.
The same thing, the same steps for any other value and any other number.
So I hope it's clear to you guys, OK, and very, very important to know this and to understand that
all the process and how it works, OK, you can stop the video, return back and watch again the the
drawings and basically all the steps.
And yeah, not a very easy exercise, but it's kind of nice.
OK, we solved it using recursion, using a step by step approach.
And now I have a question to ask you.
So we completed this exercise in about like about pretty much two lines of code, including the signature
of the function.
I want to be a little bit minimize this.
OK, so this will be the bonus for this exercise.
Try to minimize this part, OK?
Take a look at it and try to figure out if things are pretty much looking the same.
OK, so we check this condition here in here and we return the result, which is the opposite of total
appearances so far here and here.
So we change the status and once we do return the same status.
So try to think if we can minimize these lines of code in three about, I don't know, instead of how
much instead of ten lines of code, maybe we can do it in less in five or four lines of code.
OK, so this will work just fine.
Trying to think about OK, that's a bonus.
First of all, these exercises over you completed it successfully, and now you simply need to think
about what in how this exercise can be a little bit optimized.
The solution minimized, actually not optimized, but minimized.
So it will look more cleaner.
So try this one.
And.
Try this one again and again and again until you succeed, and if not, and also if yes, compare it
with the next video.
We know I'm going to also just for you to give you the solution for this option.
OK, so goodbye.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.