Hello everyone.
So welcome to this video.
In this video we are going to continue our discussion on recursion and going to solve a question which
is in front of you.
Now we have already solved lots of question and I will continue doing so because I believe when you
will see me solving those questions, it will help you to build your thought process.
Whenever you are solving any question, please make sure that you are focusing on exact framework as
well as the thought process which goes behind solving that instead of just chasing around the solution.
And you will see that difficult question will also be easier to solve.
So now let's get started.
The question in front of you is write a program for a given number n.
So that tells that okay, we will be given a particular number n where we have to print it one to n
and then n to one.
So first let us take a look at I would say the first question which is printing the number from one
to n.
Let us go back to our code.
So I've created a new file.
Print one to N and let us create a new function here.
Print one to n where we will be getting a number n.
And let us say the number is five Great.
So let us once go back into your, I would say, notepad and I try to see some things.
The question basically asks us to print a number, but of course we have to do it recursively, right?
This is something which is a very easy question, but there is some concept which I want you to explain
via this.
Now, if you have to do this recursively, the way you have to think about this problem is that you
will be doing some part of your work, and rest recursion will actually be taken care of, right?
So let us now try to see what part is that which we will be taking care of.
So if you have to print numbers from one to n.
I feel what is that one part which we can actually do is we can print a single number, and we can let
or ask recursion to print the rest of the numbers.
Right.
So one number, we will take care of the rest all we will ask recursion to actually do for us.
So on that note what we can do.
So we have to print one to n right.
We will be first calling a number.
Calling our code with n is equals to five.
And this is our print function.
Print one to n.
Now if I have to actually print them and have to start from one, what I have to do is to make sure
that one is getting printed first.
Because if I print five, which is the number I am getting in my function first, right?
So let us say this is our output screen.
If I print five first, then of course I do not have any particular way to stop it, right?
I cannot remove it or even delete it and then print one after it.
Right?
So what we can do is first let us think of a base case.
I think base case is very easy if you can if n is one okay.
Or let us say if n is zero then we do not have to do anything right.
We can return just so if n is less than or equal to zero, maybe I can write it like this.
If my n is less than or equal to zero because I have to print from one to n, right?
So I have to start from one and go till n.
So if someone is giving me any number, let us say minus three as well.
It is not a valid input, right.
So please make sure that you think of the base case very much properly.
So this is your base case okay.
Now what you have to do next is we have to make sure that we have to rather see that, okay, should
we actually be doing our work first or should we ask the question.
So for this particular question, I can call recursion and ask it to print for n minus one to take care
of everything from n minus one.
Now I want to actually now help you think in terms of, I would say, recursion, then what exactly
it is and how it will help you.
Right?
So if you pay attention here.
So if you now pay attention here everyone, when I call this particular line, this line number five,
this actually is the same problem.
But it is actually I would say reduced.
Right.
So this is the same problem.
Now the output of this problem and this is where I want all of you to think and actually understand
what is going to happen.
I have to actually assume that this particular line of code, it will print from 1 to 4 for me.
Now, I would have to question that how exactly that will happen, because we have already seen via
PMI that that is not something which we do.
Right.
So the second you call this particular function with an input, you have to assume that your work.
Sorry, the recursion is going to do the work for you, which is going to be right.
Now, once this has been done, you just have to make sure that you are doing your work properly and
everything will be fine.
Right.
So if this has already been done, the one which you see on screen, what you have to do now is you
just have to print the number which is n.
And if I think if we run this because we have done our work properly recursion, we assume that it will
be doing things properly.
And this is the base case as well, right?
So all the things are being taken care of.
And if I run this I am able to get my answer.
This is printing the number from one to n.
Now again I know this question is very easy, but the thing which I want you to focus on is the understanding.
So whenever you are calling the function itself on a same smaller problem, this is what you have to
assume.
And this is how you have to solve the questions.
Right?
So instead of just thinking on the solution, the very first time you I would see see any particular
problem in recursion or anywhere else, please make sure that you understand the approach.
You are able to actually build a thought process and then try to write the code for it.
Okay.
So again I think this is pretty easy to comprehend and understand.
Maybe I can just run it quickly on Python Tutor one.
So let us do that.
Let us remove our code.
So this is pretty straightforward.
Function is going to get defined.
And we are calling it for five.
Now of course first we will see n is equal to five.
It's not the base case.
So we are not going to return.
And we are calling this for the smaller problem.
Now the thing which I have asked you always remember that you do not have to draw these functions and
everything while writing the code.
Once you write the code and I would say even before writing, you can then of course draw that.
But please, while writing it will be a little problematic because you will be thinking that, okay,
what will happen when n is equal to three, then two, then one?
No, you don't have to do that.
You just assume that this is going to do the work for you.
And as we move forward and even solve difficult question, you will actually see how powerful this particular
technique is.
So now going forward of course.
Now let us see.
We are going to call for n is equal to four.
The same thing is going to happen.
Then three, two, one and zero.
So zero for n is equal to zero, it's going to return, right.
Because this is the base case.
So this is going to return.
Nothing is going to get printed.
Now in print one to n the line says print n n.
Here is one.
So we are going to print one to screen.
Okay.
It's going to return nothing.
So you have to understand this as well that it will be still returning nothing.
Because a function in Python either returns none or any value which you want it to return going forward.
Same is going to happen for two, same is going to happen for three.
Same is going to happen for four.
Same is going to happen for five.
And we will come out of this function and our result is on in front of us.
Okay.
So I hope with this this particular function is clear.
Now you can also draw the recursion tree for this particular thing.
So it will be a little easy.
But I still want you to draw it so that you get the understanding here.
In the next video we are going to do this print N21 question and I'm going to then explain you what
the exact difference arises in these questions.
Right.
So I hope this is clear.
And let's meet in the next video.
Thank you everyone.