Lec 78
Lec 78
Alright guys in the previous video we had seen the outline of the game and the flow of the
game how the flow would go this is how the game is played all this we had seen let’s give a
very quick summary, we had collected a movies from imdb and we had inputted the two
players name for personalisation purpose we had started with zero points each we had a
variable to keep track of whose turn it is and we had a variable to keep track of whether they
are willing to continue the game or not. And in the persons turn you have to display the that
is his turn, pick a random movie from this list create the question that is encode all characters
except by star print the question then as long as he says the answer you repeat this process
you ask him to guess the letter whether it is present in the movie name, if it is present you
need to unlock it else say that letter is not found, for unlocking what is that we are doing is, in
that movie name if that particular letter is present that particular star is replaced by the actual
letter and other letters just remains star this is how it happens, it happens repeatedly
sometimes because with one character sometimes the person may able to guess the movie
name in that case if he wants to guess the movie name, you get his answer check if it is the
correct movie name, if it is correct display the message it is correct and increase his score by
one point and say that this is your score and once he has said that it is correct this particular
thing comes to an end, the thing of guessing the letter unlocking the letter these thing comes
to an end. In case if he is wrong you have to ask him to try again and in case if he wants to
unlock another character and he doesn’t want to have a guess, you need to repeat the same
process again in this particular subsequent round of unlocking what you have to do is, the
letter that is already unlocked must remain unlock only that shouldn’t be encoded by stars
again so the thing that are noted know to the person is what encoded as stars so this is how
the game goes and after he has given the answer ask him he wants to continue the game or
quit based on that you act accordingly if he wants to quit say that this is the scores and this is
the status till now and give him a good greeting message and say that you want to quit the
game so just say willing is false where the games comes to an end, in case the person wants
to continue what should be done is you should increment the value of turn as you could see
here you could increment the value of turn so that the next player gets the chance to play, the
thing continue this way you can continue the game on and on as long as not even one of them
wants to quit the game, you can play the game on and on so here there are some things that
are undefined you could see this one create question is present and unlock this three
functionalities are not yet defined so let us define them one by one create question this is
functionality that is not defined so let me define it so let me say here let me say i have to
define the particular process of creating a question so let me say define this is the way you
create a new function, this is creating a question is a function you want to be created so we
will create this question based on a given movie there movie will be given here so based on a
movie. You need to create a question alright so how do we do this? First let me capture the
length of the movies let me say n let the movie contain n characters this included spaces as
well please note that this length include spaces as well so for example if you would take this
thing if you would take golmaal movie there is three characters under space and four
character so it will be counted as eight characters so it is golmaal has three characters space
and four characters so it will be counted as eight characters so this is how the length of the
movie will be counted so i need to calculate the length for this we have a pre defined function
length, length of the movie that has been fast raved that is for which movie you want to create
a question get its length and for encoding we need to process the individual characters if it is
a space you should remain you should keep it as a space only otherwise you should make it a
star so we have to process has individual characters so to get individual characters what you
do is maybe you can say may be i will say letters, let me say the letters is the list i am
creating out of this movie name list of movie, movie is a string that is this as a single word
will be passed so that would be decomposed into individual letters by this functionality list
so that letters would be a list. We will have now also create a new temporary list may be i will
call it as temp that will be used later we will come to know what is the purpose why we are
creating this new list because we need to output the encoded format so the encoded format
based on individual character i will have it has a list and then i will join it to a single string
that is how i am going to do it so let me do that, so we have to hydrate over that is we will
move over every character every letter of the movie name so that is present every letter is
present in this list letters so i have to move over this for i let me have a variable most over i in
the range of how many letters are there? That is captured by this value n which shows the
length which calculated the length range of n ok right? if what we should check, if that is
space i should just keep it as a space otherwise i should make it a star so let me say that is
found in letters if letters at this position i is equal to space then to this temporary array to the
output array i have to append that is i have to add at the end, end of the current list i have to
add a space else if it is not a space i have to add a, add the same character, i have to add the
character star so every letter is encoded by star except this face that is taken care of here if it
is a space you append a space else you append a star that’s what you have done now your
temporary array contains the encoded form of your temporary list contains the encoded form
of your movie which is nothing but your question you call that as your question so you need
to return it but your question is a single word not a list so you need to make it a single word
for that we have a built in functionality called join, so let’s make use of it and we will join it
so let me call it as may be let me say it is a question is nothing but irrespective of any
character just keep joining, just keep joining every letter you convert the letter into a string
format because string format it is easier you to join so you are converting into string format
for every letter x in the temp, so how this works, let me once again say this temp is the array
which has, temp is the list which has the stars and spaces with respect to the chosen movie
name so say for example if our movie name is gol maal then with respect to g the star is
appended then with respect to o the star is appended with respected to l the star is appended
here is a space so space is appended then star star star star this is how you have encoded and
you have it has a individual character for each of the individual character you convert it to a
string and keep joining into one composite string that is as one question so this i don’t want it
as a list of eight characters but i want it as one string of eight characters that is how one word
of eight characters something like that i want, so that is what i am doing by this join function
so i have created this question so let me return the question that’s it create question no words,
if you would see here then you would say create question what you do is you calculate the
length of the movie you convert it into individual characters the list of individual characters
because based on the characters you have to append that is if it is a space you just keep it as a
space that’s what is taken care of here if it is a space then keep it as the space itself else you
have to put a star and then now from the list you merge back and get back the original string,
get back as a string that is a question so you get back the question you return that question
that is instead of having a list of some characters you merge those single individual characters
and make it as a single string and return that single string that is what we are doing here with
the help of the join functionality all these are pre defined functionalities alright so create
question part is over so sorry this is picked movie sorry for this picked movie, its picked
movie given that i have copied pasted even here i am sorry this is picked movie not pick
alright ok so now the warning occur at is present define name is present and undefined name
unlocked so we will define them slowly so let us define that, define so let me a single
functionality is not define is present is present so what are the characters what are the things i
have passed one letter and a movie so i have to see if this particular letter is present in the
movie name so this is what i have to check in this functionality so let me say i will have a
counter, so for this what do i do is my logic is i will count the number of occurrences of the
letter and this movie there is a functionality pre defined functionality for strings to count the
number of occurrences of characters say for example my name vidya, if i want to count the
number of occurrences of letter a in vidya i have a functionality if i pass vidya and a to it i
would get back one as the answer because letter a occurs once in string vidya in case if i
would give the input as t, letter t is not present in vidya so it would output zero so it would
return back the answer as zero because this occurrence of t, that is letter t occurs zero times in
the word vidya where as the letter a occurs one time in the word vidya, if it occurs multiple
time may be if you could take some may be you take from the same movie itself say dangal
letter a occurs two time so it will return two so if the letter is present it will return non zero
value that is some positive value if it is not present that function will return a zero value, if
the value is zero then the letter is not present that is the logic we are going to use say we are
making use of the pre defined functionality of strings ok so i need to get the count so let me
get the count so let us say c for count equals to movie dot count that’s it this is the
functionality count of letter so this particular functionality will count how many times this
letter occurs in that movie name so it will count if this value of c is equal to zero that means
that letter is not present so is present this what you are asking if the if that is present so given
that it is zero it means it is not present so you have to return false return false sorry false else
what should be the case? It should return true i guess this is quite intuitive only thing that we
are using is this is the pre defined functionality count that counts the number of occurrences
of the particular letter in the movie so if the movie doesn’t contain that letter then it will
return zero which will say that false for which we are returning false that is this letter is not
present in that movie name or else we will say it is present so is present functionality has
been find so if we could see the warnings are disappear the warnings are disappeared so here
only warning that is still there is unlocked so we need to define this, let us do it or maybe you
can pause here for some time just see to the flow once, once again and now we have defined
this two new functionalities you would, you may even see to the flow and pause it in that
particular point where this functionality is being called you trace what is happening how
things were done just trace it for clear and understanding so may be i would recommend you
guys pause here understand the flow completely and then proceed, now comes the unlocking
part we will do it so before going to the unlocking part please do make sure that you have
understood the flow till here. Alright i hope you guys have paused and understood the flow
till here let us get with unlocking part define unlock? For unlocking what are all the things i
am having the modified question let me call it question itself here it doesn’t matter and then
you have a movie name that is the picked movie name you have passed that you have passed
then the particular letter alright oh here it is letter than i am sorry for this, this is letter
because we are not using the term character, character is the technical term that generally
being used in the computer science for letter, letter is a intuitive term for anyone who knows
English, so its better if we use letter itself so here also its letter sorry for this confusion ok so
for so good so we have requesting that is the status till now that is how many letters have
been unlocked or nothing as been unlocked all letters will be encoded as stars and spaces will
remain as spaces if something as been opened then that particular letter will be visible others
will be encoded as star and the spaces will remain as spaces so the movie will contain the
actual movie name and the letter which the person wants to unlock that is this will in this case
note that it will never happen that letter is not present because here itself we are making a
change if the letter is present only then we are getting into the uncloaking part if it is not
present we just say that it is not present so here that particular thing you need not worry about
what if the letter is not present what will i unlock if you need not worry about it the letter will
be definitely present. Ok so i need to process character wise so similar to how we did create
question part we will break the string into list of characters that is what we will be doing now,
we are going to refer to this movie so let me call it as reference, reference list, list of movie
ok then i want the question list as well so let me call it as question list or may be ql for short
anything but it’s not a problem we can have that is question list, the question list is the list of
the question, question is nothing but the status till now how many characters are under has
been known to the player and how much has been not untracked so this is the question
basically here and same like that for output say we will have a temporary list, if you could
understand the create question part well this would be easier for you so that’s why i had
recommended that you guys please do pause and take a look at it in case you feel again you
need to take a look at it it’s never a problem you can pause here take a look at it you
understand that really well then now proceed here alright so i believe you guys are now very
clear with all the parts so let’s proceed similarly i would find the number of characters in the
movie that is determine using the pre defined functionality like number of characters in the
movie. So let me iterate in the similar way for i in range of n so for each of that what should i
do, if the particular character in this reference list is the actual letter or if it is a space you
have to append the space only else you have to append the stars or the characters that is
already there as per the scenario so let us do it. Let us first have it if letter in the movie actual
movie name if the letter is equal to sorry space or if that letter is equal to the letter that you
had asked what should you do? You should append the actual letter to the output; you should
add the actual letter to the output. Why do we do that? If it is a space or if it is desired letter
in that particular position see for example may be anand is the movie that is been picked and
a is the letter they are seeking for at the first position the letter is a which is the desired letter
so in that case i should reference that list i should move to that list and take that particular
character and append it to my output, so my output are also showing, a is the letter by player
is seeking to unlock an a is present in that position so a has to be appended in the output. Else
if it is none of them else what should i do? I should check whether this particular question list
has star in it in this position or it is not star that mean say for example the same movie anand
in the first iteration probably the person has unlocked a and he wants to unlock d now let us
just assume he wants to unlock d so what is the thing that noticed, so our letter now we want
to unlock is d and the question is a star a star star and movie is anand this is what is pass to us
so the reference list will contain anand in single characters a list of single characters the
question will still contain a star a star star as a list of characters and this is our output list and
we have taken the length and it is this reference list at this position is not a space also it is not
equal to our letter d so it will come to the else part so you have to check so he wants to
unravel d it shouldn’t be the case that you should give him star star star star d you should give
him a star a star, that should be the output right so you have to check whether the question list
has star or some other character that is that character might have been unravel during the
previous runs so you have to check it so let me check it if the question list at that particular
position contains star, if that is containing star then you append star then you append a star
else what you do? Sorry it’s an spelling mistake append ok and here it is else you append the
actual character sorry sorry it’s again an spelling mistake append the actual character where is
the actual character present? It is present actually in both the places in this reference list as
well as the question list because during the previous run of unravelling also you would have
referred to the reference list and unravel so you can use any of the two question list of i or
reference list of i that is ith position of question list or the reference list you can use any of
them. Let me use reference list, reference list of i so i have appended it ok so in a similar
fashion so now given that i have iterated in this would be done for all the characters all the
letter of the movie given that we have completed now we have we would have got the output
list as a list of characters we want it as a single string so similar to our previous operation we
would do a join functionality so let me say this is my let me say this i will copy, this is what i
am basically precisely doing this way i am copying and pasting it so question is already
given so let us say this as question new just for clarity sake i am saying this is question new,
new question we got and now return the new question so whatever we have done is almost
very much similar to what is done in the create question part so if you could understand that
there you can definitely understand it what you need to take care is if it is space or if it is
required character then you append the required character else you see if it is star that is it has
been not unravelled the previous run then you append a star else you append a actual
character here you could use reference of i or question list of i as well anything can be used
because during the previous runs of unravelling as well you would have referred to the
reference list and you would have followed the same procedure so it doesn’t matter both will
actually be the same value you could use any value and then now we are joining the output
list into a single string and we are returning that string this is how unlocking works alright?
Now may be you can pause and have a look at the entire code how things go on, how
everything works, you just have a look at it, let’s run this in the next video.