 Hi everyone once again welcome to the LPS course this lecture we are continuing to look at the the Python programs we covered six of them today we are going to cover the remaining one so I am going to go quickly over these things basically so that you get an idea of how the Python code is written and what are the benefits of the once again Python is used for pretty much like I mean it is a scripting but at the same time you get the full power of the programming language so so here we are actually like going to the number seven which is the programs on lists essentially so here the first one is just a simple function given a list of numbers we return a list there all the adjacent elements have been reduced to a single element I just sent equivalent elements so if it is 1 2 2 3 the program returns 1 2 3 as a list so here we define this function remove adjacent and then basically it is a list of numbers and in the result it is also a list so we define as an empty list initially and then we basically say like I mean you for each of these elements inside this num so num num now if length result equal to 0 or num is not equal to result minus 1 then we append result minus up and result we use this particular method that is happened with the number so here you can see basically like I mean so the length of result is 0 that means that it is a starting point this right now it is 0 and then if the number is not equal to the result minus result minus 1 that means that the last element then we append it otherwise we do not append we just continue on then until all these numbers are exhausted and then we return the result as the list so simple program just look at the way that it is written how elegant it is all we do is essentially like I mean to smoke out the append that is what we are using it here so another one is basically given two lists sorted in increasing order create and return a merged list of all elements in sorted order you may modify the past in list ideally the solution should work in linear time so what that means is basically for every element it needs to take a fixed amount of time so that it is not an exponential or anything like that so we make we need to do only one pass for both the lists so here we define the same function like a linear mode this one on this two those are the two input ones that we are giving here and then we define a new one called the result a list and that is defined as an empty list front so then now we are going to do basically for while for we look at the two lists so long as both are non empty take whichever element is the smaller so that that is the whole overall algorithm there is in this comments I can see that so while these two lists are non not zero non zero now what we do is basically like this one zero is less than list to zero then we append as list one basically and then what we do is basically like I mean that that is actually removed from the list whichever one gets appended that gets removed from the list and if it is the other one then we append the second one and then remove that from the list now we track what is left by using result dot extend list one and same list two and then we basically return the list for the result itself again it is a fairly simple program to write just keep in mind about this here what we do is basically like whichever one is model we put it in the you know result list and then pop it out of the original list so now a test function used in main to print what each function returns versus what it is supposed to return this is our standard one that you know basically so if body is equal to expected then physics is okay otherwise physics is x and then we print the whole thing so now we test all these programs and basically so the remove adjacent we just give like 1 2 2 3 and then we expect the result to be 1 2 3 and then we also try it with some other things basically 2 2 3 3 3 should be just 2 3 and then if it is an empty list it should print an empty list and then for the linear merge we again do like a couple of conditions one is we do a a xx and bb cc and then the bb cc should be in all of that and then the if it is a xx and then bb cc then the bb and cc should be middle and then b should be at the end and then a a and then a b b b b b should be like three a's and then two so here is what we get when we run this programs and we can say that actually it has executed successfully so this is one way to actually test these kind of programs I think you should be aware of this and you should be using these kind of testing before you start beating the upper programs in an assignment now let us look at some of the programs or string again we will go through the similar kind of testing procedure so let us see what kind of things that we can do so the number one is at donuts this is given an integer count of a number of donuts return a string in the form of number of donuts the count the count is a number passing however if the number if the count is 10 or more then the use the word mini instead of the actual count so if donuts 5 returns number of donuts is 5 but if donors try 3 returns number of almost me so how do we do that so here we again define the function called donuts and then we pass the counts as input and if the count is less than 10 then we return the number of donuts is plus so number of donuts column and then we print this string count which is essentially like whatever the number that is less than 10 else we just return number of donuts and so basically like this is a very short program to test the I mean to do what we are wanted now the next one is basically both ends in some other function here essentially like I mean if length of this string is less than 2 then return nothing the first two is basically like it strings the two values and then the last two is actually strings minus two column basically that specifies the last two and then we return the first two in the law so this is basically the it returns the two characters at both ends of the string now a fit starts essentially this is given a string s return a string there all occurrences of the first character having changed to star except do not change the first character it is so if it is a babble that will become be a star star any so the solution is here this is we get the string s and then we define what is the front which is 0 and what is the back which is s minus 1 and then we say like I mean fixed back which is we use this method back dot replays essentially and that we with so this this gives you the rest of the characters essentially one column rest is basically all the ones that have after the first one the first one we already captured in the form and then so once you want to fix that this back basically we replace the front which is this character with the start so that replaces all the remaining occurrences of this front character with the start and then we just return front and the fixed back I think these are fairly easy I think you should be able to understand most of the stuff still go through some of these things so here the mixed up basically given strings a and b return a single string with a and b separated by space that is a b except the swap the first two characters of each of string so mix part will become box mid or dog dinner will become dig down so you define again the function looks up a b and then essentially like we define the a swapped is b the first two column plus a to onwards the remaining and then the swap is same thing basically is first two characters and then the remaining of the b and then we simply basically return the a swapped then a space and then be swapped so again same function that we define basically and then here this is for testing these above functions and then we just find the main as basically like what is the function part with what should be the the the return value and when we test these different functions essentially like all the four functions you get following output which is confirmed that they have written the code correct so now more programs on string here basically it is called the verbi because given a string its length is at least three add ing to the end unless it already ends in the ng in which case add ly instead if the string length is less than three and return the resulting string so here is the our little function which takes yes as the input string now we first of all we see whether the length is less than or equal to three if that is there sorry if it is greater than or equal to three so if it is there then we try to see whether the last three characters are ing this is s-3 and if it is not ing then we add the ing to to the end but if it is ing it goes into the else now we add ly as a string and append that to the original string and then we return this so in this case if it is less than three it basically leaves it as is now the next function is not bad which is given a string find the first appearance of the substring not and bad if bad follows the not then replace the whole not not bad substring with good return the resulting string so if it is basically like says like dinner is not that bad then you get dinner is good so it is a not that and that or anything that is between and that is basically replace it is good so the way that we write it is basically then we get the string then we find not so this is a method and we also find bad store it in these two variables and then if if they are not the last characters last one is essentially which is in not equal to one or B not in one and if B is greater than N then we look at the string and then essentially replace from the last the end character essentially so we take out the characters up to N and put the good there and then the remaining basically so and B plus 3 so so that means that essentially you continue after the back so we go up to not print good and then go after the back so this brings the whole new string so most of these kind of algorithms are still explaining to I will go through it but I think like I mean once you read it it will be fairly easy for you to understand how these algorithms are written and then as you can see like I mean these are less than 10 lines of code so you should be able to pick it up very easily okay so the next one is the front back basically which is the dividing a string into two halves the length is even then the front and the back half as the former the length is odd then we will say that an extra character goes in the front half that is ABCDE the front half is ABC and the back half is the given two strings A and B we return a string in the form of a front B front and then a back B back so we take these two strings basically and now we need to figure out the middle position of each string so a middle is linked a you are that you and B middle is linked B you are that you now we see whether it's an even or an odd if this is odd now add one to the length one is the length is odd and then so we add that basically a middle plus one if it is if the same thing goes for the below so you see basically like when we divide by two you don't put the decimal so that it is an integer division and it is a lower number so that's why we are just adding one to it if you use like dot 0 or something here then that will mess up the whole confusion so please don't do that and then finally we return basically the a string from 0 all the way to a middle and then B string with 0 all the way to be middle and then a string a middle always then and then be middle of it then again we do the testing program and then test it and here you see that basically so this is the example program on game this is probably the last one here first we say basically the high score for the game and now we do a try basically recommend the high score or text essentially and then so basically there is already exceptions built in into this one that's like that so we try to read the high score from this file that's the whole intent so whether we just read that high score and then we close the file and then we print the high score is so basically and then we generate any exceptions and then we try to get the score from the current game which is current score is 0 and then we ask the user for first score and then again we have an exception if it is type something else and then if current score is greater than the high score then we say like yeah you are in the score and then we also open the thing and then write out the new score into the file and then if it is not there then just say like print the like next time so here this is one thing that how the program does so that's pretty much all that I want to cover for today I hope like Python programming is quite interval with these kind of snippets you can actually build bigger programs very easily and I would like you to try those kind of things and these are all like some basic programs which from which you can build bigger and better programs thank you very much