All.
And welcome back.
So in this video let's continue our discussion and do the third variation of this question where we
are asked to update indices in a global list.
So I will just explain you what the understanding is.
I sincerely hope that you have seen the last video and actually understood everything.
We have discussed the major part of this question, or I would say this particular variation in the
last video as well.
So humble request to please see that first.
Now let us do this question again.
I will tell you what I meant by global list.
So in the last variation we were actually sending a list down, right.
So let me just update it.
Update.
Indices.
In a global list.
Now what exactly is going to happen this time is I am not going to send a list okay.
What I mean by global list is the list is actually defined outside okay.
So let me just copy it.
Let us just make a new file.
Do To Python.
It says in a.
Global list.
Okay, so I'm just copying this all together.
Update all indices in.
Global list.
Okay.
So what I meant by global list is that let's say this list is now actually kept outside somewhere.
We have defined it outside and we are not passing it.
We are not passing it.
Okay.
This code is going to be a lot easier to understand.
The code is not actually going to be a problem.
Okay.
So we are going to come back to code.
But first let me explain you this again intuitively.
Because if you understand that then you will actually be able to appreciate and write the code for similar
problems.
Now, if you remember the last example of this teacher, which I gave you, okay, I can explain you
this using a similar example.
So pay attention everyone.
Let us say this is my teacher.
And this time also this teacher has a diary.
But when this teacher gives you the diary, okay, you instead.
This time place it on a desk.
You place this particular diary on a desk.
And you tell each and every student that.
Hey, the diary is kept on the desk.
If you have not done your homework, please go and update the diary.
Okay.
So I hope you are able to understand and appreciate this particular intuitive way of reaching out a
problem.
So what I am saying this time is, what I am saying this time is that now the diary is kept somewhere
on the desk.
I am not going to pass the diary to each and every student.
No, I am not going to do that.
Let us say I am the yellow one.
Okay?
What I am going to say is that.
Hey, have you done the homework?
If not, please go and update it in the diary.
So let us say again this.
These are the people who have not done it Either I can check for myself first, or I can just ask first
for the, uh, for the student who is after me.
That is the first index.
I hope till now we are aware of that particular thing.
First one is the base case.
If, let's say no students are there, then of course I'm going to return.
There is nothing which I can do.
Right?
If you go in the class and no students are there, then of course you are going to return.
You cannot do anything.
You cannot update.
Now let's say in the first case I will check for myself.
If I have done the homework, I will not do anything.
If not, I will just go and update because I know where the diary is.
I know where the diary is right now.
This understanding is exactly the same thing.
Just I want to share this particular approach with you because many times this is also asked see, when
we have this answer list outside, of course this is in a global scope and anything which is in the
global scope, and that is where this particular global list is coming in.
The question question I would say details because this is in the global scope.
Anyone can access it, right?
Any function which is defined after it can access it.
And the same thing is going to happen because I have defined my list outside.
Let's assume that it is some table which is kept, which is accessible by everyone.
You can actually access this list inside as well.
Okay, that is what Global Scope is.
So please make sure that it is actually defined somewhere where like this list is not defined somewhere
inside because then it will be in local scope and that's going to get destroyed.
Okay.
So if it is in a local scope it is something like like let us say you are not returning it.
Student will write somewhere, but he is not going to give that back to you.
Right?
So that is something which will happen if it is in the local scope.
So right now, because it is defined outside the whole function, again please make sure that you see
the local and global scope video if you have not seen that because here because this list in the global
scope.
That's the reason it can be used everywhere now because the list is outside, this can actually be used
inside without any problem.
Yes, we are still not passing it.
But again, I hope you can understand from this particular example that how even if you are not passing
it, students can still go and append and actually append or change in this particular diary because
it is accessible to them.
And actually the question is done like we do not have to do anything else.
The question in this particular way is done and you will get your answers.
See, I can run it quickly on Python Tutor as well, but I hope all of you understand that what exactly
has happened again, because this is just accessible.
Yes, I am very good.
Even if I am not.
Pass it and everything is going to work.
So pay attention everyone earlier.
Now see, this is global frame where my answer list is getting defined.
And this answer list can actually be useful inside this function as well.
So pay attention.
Update all indices like the function name is same, but that should be fine, right?
Now when I am calling this particular function, it checks for the base case.
And we know that base case is not as of now fine.
But see when I am just doing this L1 index and I am checking for this particular condition.
The answer dot append.
I know where the answer list is kept because this is in global frame.
Yes, it is not with me.
The diary is not with me.
But I still know where it is kept so I can access it right now.
Of course I will not access it, but when I call it for the next time, this particular function index
at one is having the value as two.
So once I go forward, once I go forward this time, then I will be actually trying to append onto this
answer list.
I will be able to do so.
And the reason is very simple.
It is in the global frame.
And when I do it, you can see that the overall this particular thing changes, right?
Overall, I am able to make a change into this thing.
And now again the same thing is going to get done and we are going to get an answer.
So the rest and the rest of the working is same.
It is just that this time I am not passing it.
So these are the kind of variation which you will actually encounter.
So I want that you understand each and every variation.
And I'm pretty sure that with the example it should be a lot clearer.
So you see we are getting an answer because this is in the global frame.
I can print the answer list and I am able to get my answer.
So the code again now is very easy.
Once you understand if someone will just give you this question directly, you will actually not be
able to comprehend that what he wants.
But once you have seen all the variations with me, then of course you will be able to understand that
how you have to deal with this particular question going forward in the next module as well, when we
will be dealing with recursion and string, we will be solving some very hard questions which were asked
in companies like Meta and Google, which are based on this particular principle only.
So please make sure that you understand this.
If you understand this properly, believe me, you will be able to solve all the questions which are
asked in recursion because normally these are the kind of variations which we get.
Okay, cool everyone.
So I hope this is clear as well.
Now, in the last and the last video of this particular module, we are going to solve this particular
question return answer as a new list.
Then I'm going to give you some assignment as well, which will be based on this particular variations
only.
So please make sure that you are trying those assignment based on everything which I have taught, and
make sure that you are drawing the recursion tree yourself in the starting video or starting questions
and your understanding will be crystal clear.
Okay, cool everyone.
So let's meet in the next video.
Thanks a lot.