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
Hey, welcome back, ladies and gentlemen, and in this video, what we are going to do is simply to
expand our previous exercise where we have written some functions.
One of them was like print one to end and the other function was print and to one.
And basically these functions were all about like printing all the numbers from one up to a given and
assuming that any positive.
Positive, of course.
So in this case, if we got a number of four and input four, then we simply would have printed print
one to end one, two, three, four.
OK, because here again is four.
And also we created another function which was print and to one.
And all these function was doing is like printing all the numbers from four, three, two, one.
OK, so basically we had created these two recursive functions and we also talked about their flow,
how they make the recursive calls, what is printed before, what is printed after and so on and so
forth.
Right.
So let's just review and make sure that everything looks exactly as we were expecting.
So print into one and getting one in, for example, for printing for then going through the same function,
printing three, printing to printing one until we reach some Stubing condition.
OK, so also this thing was done here.
But just the difference was, is that when we print the current value of the recursive call and when
we make the recursive call itself.
OK, so here the print comes after words and then in here print comes before the recursive call.
And that's the difference in terms of when you print one, two, three, four or four, three, two,
one.
We've seen some diagrams.
We already feel more confident about it.
And if not, please go back to the previous video and make sure that you understand that maybe replay
the video and make sure you get the whole idea, because in this exercise, we are going to take it
even further.
OK, so what we are going to do now is we know how to work with print and one print, one to end it.
Now, what I want us to do is simply to modify, to extend the previous exercise and to make it a little
bit more unique.
OK, am I saying more unique, what I actually mean is simply to like to take this exercise, OK, and
print all the numbers.
Let's say that's our example and I want to print all the numbers like this.
So two, three, four.
So printing all the numbers from a given N this is the input M equals to four.
And I want to bring all the numbers from four up to one and then from one up to four.
So one of the things and one of the options to do so was simple simply let's say we will print all the
numbers from an to one.
OK, let's just modify the main function.
We'll simply assume that maybe we can use two functions that we've already created in one of our previous
videos.
So we just move this microphone, OK?
And basically what we want to do is to print all the numbers from end to one and all the numbers from
one, from one to M.
So what do you think, guys?
Do you think this solution will work?
Do you think we've made it?
Take a few seconds.
Don't even run it on your cell, but you're on your own.
Simply try to think what will be printed.
Now.
Do you think?
That all the numbers will be printed like this or maybe some other version will be printed, something
something else.
So let's let's give it a try.
So let's build and run it and enter a number.
So let's enter for and what we can see is four, three, two, one, one, two, three, four.
Although it's pretty, pretty, pretty similar to the result we expected to see.
That's not actually the same.
What's important here is basically to see this step.
OK, so four, three, two, one, one, two, three, four.
In here I have only just one.
OK, I don't have this one repeated twice.
And that's something we need to take care of, but we can't do it in these trivial solutions.
So bear this in mind.
So what do you think?
What do you think, guys, we should do if we want simply to to eliminate this task?
So my suggestion is, first of all, to start thinking of another function, OK, which will simply
print into one and two M basically from these one to end without these duplication.
So let's call it I don't know, it's called this function.
I don't know.
It's let's make it for simplicity.
Call it final and final and to one.
And and vice versa.
OK, I don't know, something like that, and this function is going to receive him and now we have
to construct these function from the start.
What do you think should be different in such a case?
So basically saying we can simply open up these Perinton to one and we can say the following thing.
So let's say if any is greater or equal to one, then in this case, what will print is simply the before.
Of brain printed percentage D and.
And and then make the recursive call.
OK, so that's the final viceversa all the time.
And here do the same.
Right.
So basically something like that.
And what do you think will happen now.
What do you think will happen.
So let's simply call this function and also see for ourselves what will happen.
So for these num so let's build an R on it.
What do you think will be the result goes think about it.
Will it be the same as it was previously or not.
I think it will be the same.
OK, so let's run four and you can see for three to one, one, two, three, four.
So that's one happening behind the scenes.
Every time we call this function, we first of all print the value we call it with.
In this case, it will be four and then we call it to the recursive call to three and then print three,
print to print one.
And then once we go back to R to where we start in the previous call, then we execute the print F for
one, four, two, four, three and four for OK.
So that's pretty much the same as it, as we were using like print and one and print one to n one after
the other.
OK, so we definitely understand that this solution is pretty much similar to what we are looking for,
but not exactly the same.
So what should be changed.
Right.
Changed right now.
OK, what do you think guys?
What do you think?
So my suggestion is to look at the base case that we are having a hard time with in this base case is
basically when we reach this place of one when and equals to one.
So let's just try to focus on what happens when we reach the fact that and equals to one.
So if any goes to one, we will still let's say let's say we will even be on two.
OK, let's discuss two.
So when we reach two, two is greater or equal than one, then we print two and then we call the function
for one.
And then we after we are done, we also print two.
So we understand that somewhere in the time when any equals to one and we call this function, something
shouldn't be done like this, like printed twice.
But that's actually what happens behind the scenes right now.
We call these function final and to one and vice versa.
And we use here one.
And if we ask if an equal or greater than one and in this case it's equals it equals to one, then we
print one, then we ring, then we run of the following recursive call, which will be simply nothing
and nothing will be printed for find and to one and vice versa for zero.
And then we print one once again so we can do something like this to just like take you to eliminate
it, because this will not print R the next time also for all the other numbers.
Right.
Not just for one.
So what we would like to do is basically to split up this condition.
Right, because this condition will be true basically for two main cases.
The first case is if M is greater than one or if an equal to one.
So let's simply split it up because we know that when N will be equal to one, we would not want to
print it twice.
So let's try to think about it.
So if any equals to one, then in this case, what we would like to do is just to print its value and
that's it.
Without even any recursive call.
Right, because we know that we don't have to like to make recursive calls for an equal to zero.
And I hope that's clear to you guys and here Will simply mortified, right, we will simply mortified,
if any, is greater or equal to one.
Or basically you can also do like HCF or basically leave it as LS.
OK, this will be pretty much the same.
So if an equals to one, we will print it else.
We will print every time like these parents who for and for three and three, two and two and so on
and so forth.
So let's just build and run it.
So there you go, build and run.
What happens here or what's the problem?
What is the problem here?
Printf percentage, the number.
There was no no.
There was just not right.
So let's build and run it.
And they did it in four equals.
Two, four, three, two, one, two, three, four.
Awesome.
So it seems to work.
It's also tested out for the value of five.
So five, four, three, two, one, one, two, three, four, five.
Awesome.
No duplicates here exactly as we were requested.
Amazing.
So now what I want us to do is simply to let's just open it up and basically what I would like us to
do now is just to see the flow of printing step by step and block by block to make sure that you understand
it even further.
OK, so let me just get my panel right.
Awesome.
So let us start with the final end to one and vice versa.
Four a.m. equals to four.
So here we will send now equals to four.
And there you go.
Basically, this will be our console where all the output is printed.
So that's the console.
And what we also will have is basically the first call for this function.
Right.
So final into one and vice versa for an equals two four, if any goes to one, not the case else, which
you may be the case probably.
We are going to print and to the screen we will print four and then we will call this function again.
So we are calling this function.
And equals to three, and these function does the same, it also prints three to the screen and there
you go again and equals to two and you point two to the screen and then you call one and equals to one.
Right.
And you get to this point and equals to one.
And if any equals to one, you print one to the screen here and that's it.
These function here does not call any recursive calls, meaning that in the previous instance when and
were was equal to two, we left from this line.
Right.
We printed two to the screen and then we called the final end to one and vice versa for the with the
value of one.
And we are done.
These function call is executed, that's executed.
One is printed and we came here.
Right.
That's four and equals to two where we left off.
So we stop here now.
We will print the value of two again.
And once we are done, this function is over.
Then we reach these final into one and vice versa for any equals to three.
And that's the stopping point where we stopped to call this function with the value of two.
Then we print the value of three and then the same for the value of four.
So that's basically how how it works.
Okay, so once we are back, we do not return anything, but we basically are simply continuing to execute
the commands right where we left off.
So let me know if this solution is clear to you guys.
I hope it is.
It's very important.
Recursions concept is not easy, especially when you try to mix things into one question and to make
it more difficult and to think of different base conditions.
And when you should and when you shouldn't make the recursive calls and so on and so forth, so I hope
this is clear to you guys, let me know if you like this video and you have any questions, leave some
review.
And until next time, I wish you a great day.
Great time.
Keep on practicing.
Keep on moving forward.
And you were about to succeed.
I'll see you next time.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.