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 what is going on, guys, and I've just looked at the previous exercise and and the previous solution
that was provided.
And I have been thinking to myself as to can we improve this solution?
Yes or no?
I mean, like it seems to be working just fine by splitting up every time for a smaller sleep problems
and on every recursive call, taking some rightmost digit and comparing it with the results so far.
And although this work just works just fine, I have been thinking if we can optimize it a little bit
and reduce the number of lines of code that we use here in order to make these recursive call recursive
function much smaller in code.
So what I want you to think about is, first of all, these works just fine.
Now we can try to think about how we can optimize it and minimize it.
So a result so far, if the result so far is one right?
So it will stay one if basically if we take a look at the right, most digit.
So if the right, most digit will be even right, this indicates that the right most digit still remains
even right, then the results so far will remain the same.
And also, the results so far will have remained the same in this case, whenever the results so far
is zero.
So if the result so far was even or odd, OK doesn't really matter.
And of the right most, the digit in this recursive call was what was even then the results so far,
and this function will return the same value that it had previously.
So if it was one, it still will will remain one.
And if it was zero, it still will remain zero.
OK, so what I'm trying to say is that the results so far, maybe even.
Plus, the even right most digit will result in an even, and if it was odd plus in even which is the
right most digit, it will still remain odd.
OK, so it will not change the status.
On the other hand, if we know that these else are relates to the fact that they're right, most digit
is odd.
Then if the result so far was one, it will change its status, OK?
It will become zero, because if we know that the result so far is odd, and if we know that the result
so far is even and we know that the right most digit is odd, then odd plus even will result in an odd
value.
And that's the same also for here, if it was an order value, right?
The result so far was a sum that is odd.
Plus, this else relates to the right most digit.
When it's odd, then odd plus order will equal to even meaning we will need to change the status.
The results so far, the return value from this function again.
So what we'll try to do is basically to minimize this task, OK, and make it a little smaller with
much less lines of code.
OK, that's what I'm going to show you right now.
So instead of using it like this, what we will ask is the following question we will ask if the right
most digit like this, if it's the case right, then what we will need to do is simply too.
Okay, I'm trying to build this thing together with you.
If that's the case, right?
If that's the case, then what we need to return is what do you think is just the results so far?
Return result?
So far, that's it.
Because since the right, most digit is in even one.
OK.
There is no reason for us to change it.
OK, but if it's not, then we will need simply to return what?
We will simply need to return.
Let me get it right here.
We will simply need to return, not results so far.
So what I've written here and I'm not finished yet.
I said that if the rightmost digit will be even then returned the results so far because this does not
affect the overall sum.
If it was, if it was an even some, it will still remain even.
And if it was odd, it will still remain odd.
On the other hand, if the right was the it was odd, then switch from the results.
So far, OK, not means here.
Basically, if we use just ones and zeros.
So if the result so far was zero, this not will turn it into one.
And if the results so far was one, the Senate will change it to zero.
OK, awesome.
So now what I'm also going to do is to say that let's not using these results so far are variable.
And instead of these results so far right, we know that this is the value.
So instead of using it, we will simply change instead of results so far will specify the recursive
call.
OK.
It's just the same.
OK, so you see how the recursive call function, the recursive function has got much smaller, although
I do not recommend to make this version right from the beginning.
But after you practice the little bit of your exercises, I suggest creating the first initial version
that we had previously OK with all these lines of code and then ask yourself a simple question Can you
optimize this solution?
Can you minimize the number of lines of code and the number of variables created in the process of the
recursive function?
And the answer is yes.
OK.
And now I want to also continue optimizing it.
OK.
I want to say that instead of using these four lines of code.
What I will use is the following line of code.
I will say return return in modular to.
Equal to zero right, or B or C, let's use it like this, OK, if an equal to zero, then we will return
one.
Right?
That's what we will return.
Otherwise, we will return zero.
So this line of code I simply am showing, I'm simply showing you a little another option to do that.
OK, so return and modular two equals to zero.
So basically, both of these two versions, these four lines and this one one on one line, they represent
the same thing.
So we ask if that's the case.
Question mark, then return one.
OK.
The result for this condition is true.
Then return one, otherwise return zero.
OK, so that's basically what we have here.
OK, and we can also further optimize it.
Instead of saying it like this, we can say if and modular two.
Does not equal to zero, then returns zero in one, I'm simply switching places.
OK, follow up this like line by line, OK?
I didn't do anything wrong.
I just switched the places between the true result for this condition and the false result for this
condition.
And what I can ask is this simple question.
I can also remove this not equal to zero, because that's how this condition is interpreted.
It return in modular two.
If that's the case, and if it doesn't equal to zero, then return zero, otherwise return one.
Okay, so that's just short version of writing the code.
And finally, I will do also the same here.
I will say this line of code, I will say return, and I will say this part OK, if this part does not
equal to zero.
OK, so if that does not equal to zero, then return this part.
OK, if there's an integral to zero on this one and otherwise return this option.
That's it.
So these four lines of code are equivalent to this line of code.
So I will remove it in.
Finally, the final version for these function looks like this.
OK, so that's very nothing to do to read it first.
But actually, that's the answer.
That's the recursive function call optimized, minimized in lines of code.
And yeah, OK.
So you can also make sure that I've done everything correctly here by simply testing out with some examples
and hopefully because the hour is late and make sure that it works exactly as you expect.
And I really do recommend to start with the basic option that we've solved in previous videos.
And only when you feel more confident to start trying to take a look at how you can reduce the number
of lines of code and make it look like this, OK?
So yeah, this is it for these video guys.
Thank you so much for watching.
Keep on practicing.
Let me know what you think of it.
And until the next time, I will see you in the next videos by.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.