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 what are we going to do in this exercise, so in this exercise, what we are going to
do is pretty interesting, OK, not so trivial and not so easy because, you know, like, we are getting
better and better every exercise and we should also make the exercise a little bit harder.
So what we are going to do is let me see wrote it down here, so what we are going to do is to make.
We are going to write right down a recursive function that gets in an integer.
OK, and these function is going to return one if all the basically the sum of all the numbers in the
received value is.
Even OK, otherwise, we should return.
Return one.
OK, so a recursive function that gets an integer, let's say, I don't know, some end and an OK in
these function is going to return one if the sum of all the number of digits.
OK, I didn't specify if all the sum of all the digits in the received number is even.
OK, so basically you take all the digits in a given.
No, you sum them up and you return one if the result is even otherwise, if the result is odd, you
return zero.
OK, so a little bit mistake here.
OK, so that's what you do.
And basically let's take a look at the example, number one.
And if the value is, let's say, an equal to one five six, then in this case, if you sum them up,
that's going to be one plus five, one plus five plus six.
A total result will be 12.
And that's an even number and even some because it can be divided by two without the remainder.
Then that's why the final result, final return value will be in this case one.
And if the sum.
OK, let's take another example.
And if the sum of, I don't know, example to let's say three five.
OK, something like this.
Thirty six thousand eight thousand eight hundred fifty nine in this case, the same is going to be like
three plus six plus eight plus five plus nine, which will leave us a total of what it will give us
11 plus nine.
Twenty thirty three, if I'm not mistaken.
Right.
So this is nine nine plus nine eighteen plus thirteen.
The thirty one thirty one Miami stake.
OK, so thirty one and the final result is going to be zero because the sum is odd.
OK, so that's what do you have to do in this exercise?
That's what do you have to practice and to try running this on your own.
So recursive function.
Take your time and I'll see you in the Solutions video.
And of course, by saying the solutions video, I will simply simply think I will make it in this one,
because you can always pause the video and you can always come back and try writing this code on your
own.
So.
Let us start thinking of what should be the solution, guys.
OK, so what do you think?
How should we even start this exercise?
What do you think?
What will be the signature of this function?
What should be its signature?
So basically, if it's going to return either zero or one, we can always make a boolean result.
But let's stick with integers just for simplicity, because this section is not about it.
It's about the recursive manner and the recursive calls.
So end let's make it, um, I don't know, digits some even.
OK, and we are going to get value and awesome.
So now what we have to do is to start thinking of how we can basically even approach this exercise.
How can we solve it, because that's not so easy and not so trivial is a few examples that we've done
at the beginning of this section.
So, guys, what are your thoughts?
Try to think about it, try to come up with at least one or two options and basically try to run some
code to see how it works.
Will it work?
OK, so think about it.
Think about it.
Take some time.
And once you're done, let's continue to gather.
OK, so what I suggest to do, first of all, is to think of some logic on how it can be solved.
So let's just draw something here.
Let's start with the simple example of one five six.
OK, so one five six one five six, so what do we want to do is somehow on every on every recursive
call to check out some status?
OK, so you have to to think of of some rule that can be applied on every recursive call and basically
based on these rule, finally, once we will get kind of back from our recursive calls, we will be
able to make some conclusions for their result so far.
So if we take one, five, six.
And we just I don't know, and we just let's think about it together and we just will divide it like
two, one, five and six, and then we will divide these one five into one and five.
You know, we'll say the following thing, we'll say that basically we always know we always know that
the base result will be like probably just one digit because we know that one digit he can be the results
for it can be decided, you know, like trivially right away.
So let's start with this with this condition.
So if and is less than is less than 10, OK, meaning if it's a one digit number, then in this case,
that's very simple.
We can say that give and can be divided by two without a remainder, then it means this number is an
even number that in this case we can return.
What can we do?
Return one.
OK, so if M can be divided by two without the remainder.
Then we can return one and we can also say the following.
Let's take like this case, it will be much clearer to you guys.
So elusive and divided by two does not equal to zero in this case.
It's an odd number.
In this way we are going to return zero.
OK, so if M is less than zero, then we are going to ask if it can be divided by two without a remainder
returned one otherwise returns zero.
So that's basically the base case that we can work with.
Now comes the question of.
Of what we should we do if it's not the base case?
OK, so that's the base case, let's say one in five.
And we know that one in five, for example, for five, we can find the result pretty, pretty easily
right.
For or for one.
We know that one.
Let's make it like this color.
And we can say that one is, of course, and definitely one is.
What is an odd number is an odd number.
And we are we know that this function is going to return for an odd number.
It's going to return zero.
OK, so these function is going to return zero for these base case of one.
OK, and what I want you to think about is that, first of all, we are probably always going to return
either one or zero.
We are not going to return anything else because the final result should be either zero or one.
And we don't want to like to.
Mess things up.
OK, so maybe there is also an option to like to return the some.
I'm not sure about it, we maybe try to think about it or come up with a solution and let me know in
the comments and in the questions, if you manage to find another solution to mine by returning this
sum and finally to making some assumptions and conclusions.
But for this solution, I'm going to always kind of try to return either a zero or one based on the
result so far.
Okay, so now I'm going to say that that's the base case and let's try to try to think about it like.
The results so far.
OK, so let's use something like this, let's create additional variable results so far.
OK, and this variable will have like resolved result result so far is going to be equal to digits,
some even for.
I'm just trying to build this together with you.
OK, so the result is going to be digits, some even for and divided by two, which means without the
rightmost digit.
So we know that digits some even can return one.
If if the sum of all the digits is even otherwise, it will return to zero.
So we will call the results so far, meaning four one five in this case.
The results so far, OK, no problem.
So that's basically how it will look like.
Let me just draw it here.
OK, so let's say it's going to be something like this, just trying to draw the structure together
with you so and equals to one, five, six.
And then we are going also to call for an equals to one five and also four and equals to six.
OK, and that's it.
And on every iteration we are going also to hold a variable.
This result, let's call it r r equals to this result return from here.
And this is going to have also the result return from here.
And basically we know that this equals to six will we will satisfy the base condition.
It's less than 10.
And he's going in this case to return one thing since since this value is even.
OK, so our equals to one.
And these are equal to one.
What it indicates is that.
The results so far result.
So far.
Is what is even the result so far, is even.
OK, OK, so the result so far is even and what can we make out of this conclusion?
We know that the result so far is even so, we maybe try to kind of formulate a question to see if we
can like it further.
It was so oops, my mistake, guys, sorry for that, it's divided every time by 10, so it should be
like one five in here.
It should be one.
I'm sorry.
Sorry, sorry for that.
It should be one so and equals to one.
And here are should be equal to zero since no one is an odd number.
So that's also I'm going to change here.
So that's going to be an odd value.
OK, so results so far is odd.
OK, so let's try to now proceed.
And what we are going to do is that we know that we have an equal to one five and we know that the results
are far from the from the left is an odd result.
So we are going to ask also a question if.
What should we ask, what do you think those?
What do you think we should ask now?
We can definitely ask about these rightmost digits right now and we can ask if it equals two, basically
if it's an even or odd.
So if.
If.
How can you find this Redwater statement basically use and do let him write, it will give you the rightmost
digit in this number and help you find if this result is even or odd, you're basically going to.
Divided by two, OK, Modula two so if that's the case, if if.
If it's even then what you are going to do, we're going to ask again.
Let's ask another question.
So now if if the results so far results so far, if it was if it was one, it means if the result so
far is odd, OK, if the result so far is odd, OK, the results so far is odd.
And these rightmost digit is even then in this case, you know, that is some of odd plus and even will
always give you a result of OD.
So that's why you are going to return one.
And if that's not the case, OK else meaning the result so far equals to zero.
OK, then it means what it means that the result so far is even OK then in this case you know that we
are talking about a situation where these digits is even.
So you know that the result so far is even plus these given digit is even so the final result, the
sum of them is also going to be even worse.
And that's why we are going to return zero.
OK, not so trivial.
Not so straightforward.
OK, guys.
So we took care of the fact when this value is is is even which is not the case here.
Basically, what we should take care of is also when this and Modula 10, the rightmost digit is in
the basically I think I've done a little mistake.
So just with these ones and zeros.
So if it's an I told you, this exercise is not easy, you should be like very concentrated.
And now I'm recording this video when I'm not in my fullest concentration.
So if it's an even, we are going to return once a one relates to even numbers.
So if it's in, it can be divided by two without a remainder, then it's an even number and the results
so far equals to one meaning.
The result is also even three for that then even plus even will result in an even result.
Otherwise, if the result so far was equal to zero and it was all OK then in this case the sum of odd
and even will result in odd.
OK.
Now, if we have an odd number here, OK, if the number is odd, the rightmost digit is odd and we
also know that the result so far is even right, because it equals to one that was just in the comments
and the the the final explanation here, there was this problem, if it's odd and even then the final
result is zero.
And otherwise, when the result so far was all the who then the final result of good.
So let's just remove this one that it won't bother us.
OK.
OK, OK, so the results of the result, no problem.
So we start with this simple question.
Are digits OK?
That what we what we want to find digits some even for one, five, six.
So we want to understand what is this value?
So we call this function OK.
We call this function and equals two one five six.
So the results so far are using these are short R if M is less than zero.
No, that's not the case.
So let's find out the results so far.
Again, the results so far is basically the results so far from the left or the right.
What do you think?
Let's proceed.
So let's proceed.
Do not answer this question.
If you are not certain, let's see how we can visualize it.
So results so far equal two equals two digits, some even for an equals to fifteen.
OK, so now we come back again here.
This is another function instance and we ask the following questions.
We don't know the results of.
We don't know the results so far again.
So we called these function four and divided by ten in these just one.
OK, and here we already know the results so far is going to be returned OK and is less than ten and
is less than ten.
The value of end is one that in this case, if M is even that's not it, then we would have been returned
one otherwise.
We return zero.
So from here we return zero.
And where are the zero is going to be stored in the results of our value in this instance.
So R equals to zero.
The result so far is zero, meaning the results so far, ok is what it is.
It's an odd number.
OK, so now we come and proceed the running of this instance from this point.
So if the rightmost element is even, does it even use five and even value the rightmost element so
anybody will attend the rightmost?
No, that's not the case.
So we are going to execute this ls OK.
And this is going to say the following thing.
If the result so far was one and it's not meaning it's this ls will be executed.
The results so far is odd and that's the case are equal to zero then in this case, taking into consideration
that the result so far is zero, meaning it's in order.
Right.
The results so far, the sum of digits so far is odd, plus the current rightmost digit is also odd.
Then the result should be one because all the plus odd equals two to what do even so, that's why we
are going to return one to this instance.
And in this instance we are going to proceed executing these.
These are from these points.
So R equals to one.
That means we have the sum of digits so far is even.
And we are going to ask if this the rightmost digit is even, does it even?
Yes, it's six and it's even also even even digit.
So we know that even the results of our equals to one meaning, if the results so far is even that's
the case, then returned one.
OK, so even plus even basically will return even.
And that's going to be indicated by the value of one.
Oh, OK, guys, so I think we've managed to kind of solve these difficult exercise together, once
again, we took one, five, six, we split it up to smaller numbers, OK?
And on every on every iteration, on every basically recursive call, we've compared the sum of digits.
OK, basically, whether it was even even or odd so far, plus the rightmost element for these points.
So we do here are the some so far was one OK, which was an odd number basically indicated by zero and
every time we compare that with the rightmost digit.
So here we compared it with five here we compared it with six because the sum so far was 15.
And we calculated it on this later on this recursive call and so on and so forth.
So, guys, I'm leaving to you this this example.
Try to also build these blocks on your own, see what happens.
Make sure that you understand every step and every like every every nuance in this exercise, because
these is physically not so trivial.
You have to think about it.
You have to like to try to.
Yeah.
At least one or two exercises to do like this to make sure you understand everything to the fullest.
So with that being said, of course, maybe I will even create additional exercise of just some odd
OK, but there is no much things that should be changed, but I will think about it.
So thank you.
And I will see you next time.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.