 Hello all, welcome back. So, if you have noticed, so far we have done a lot of theory. I have done several examples and answered questions, but as with any language, only practice makes you perfect. So, you are going to need to practice a lot. So, there is one more topic on sets that I will skip for now. Instead, I will have about half an hour session where you will solve a problem. I will give you a simple problem to solve. You are supposed to solve it at your convenience right now. In the next 15 minutes, you solve the problem, come up with a working solution, then I will come back, discuss this answer and we will have roughly one and a half hours like this through the day, where you are going to program and I will show you how to program similarly. The idea is to give you practice on writing python codes rather than just going through the theory. The basic commands and syntax as you have noticed is very simple. However, without the basic commands and syntax, it is kind of difficult for me to give you a meaningful assignment. So, I will give you three or four problems now. I will give you one at a time and I will give you some 10 minutes depending on the difficulty of the problem. Come back and discuss. Now, what I will do is I will switch to the terminal here. So, the first problem I will ask is following. Write a function. You want me to make it bigger? I think this is better. Better, no? Write some function. I want to bother you with calling it some specific name. You can call it F, G, whatever you want. That takes, that is given a string say S. The variable name is whatever again you choose and the function returns the number of words in the file, in the string. Very simple problem. All you need to know is string manipulations. So, I will repeat the problem. Write a function that is given a string. So, it takes one argument that is a string. The function should calculate the number of words in that function, in that string and return that integer. That is your problem. Spend about till 11.50, about 7 minutes or 8 minutes and then we will discuss the solution. So, I will take different problems like this for you to look at. So, the solution to this problem, many people have actually sent it on chat. It is not very difficult at all. So, let us look at the solution. I will write the solution very simply. First thing is you have to consider that I hope many of you have tried. The importance, I am spending a lot of time on this because if you do not try these problems, you are not going to learn anything from this course. Next week you will have tests which you will not be able to take. So, let us try to solve this problem. So, first what we will do is we need to create a function. So, def, what shall we call it? Shall we call it word count? I will give it some input. I will call it input. First thing you have to remember stylistically is you should document your function. So, put triple quotes and say notice that my function documentation signature is very clear. It is very precise. Counts number of words in the given input string. So, I have also told you what I expect as this function input. Now, we can solve the problem. The input is a string. We simply need to find the number of words. There are many things I actually left unset. One of the problems is what do you do with punctuation marks or punctuation marks important? What do you define by a word? So, what we will do is we will not consider the most general most complicated problem. We will only say that any set of characters including punctuation that are separated by words separated by white space are considered as individual words. So, what we will do is we will first write out we will split this string. We know that the input is a string. We will split this string based on white space. We know that the split default behavior does this. So, we do not have to do anything special. All we need to do is input one. Words is equal to input dot split. Now, this will basically split every word automatically based on spaces. Now, to find the number of words you simply have to do count. So, if I look at the length of the words I should get, I should ideally get the correct answer. Hopefully, I will get the right answer. So, let us try this. Please realize that very often programming is not a it requires repeated attempts. You may not obviously get first time you write the solution everything is not going to come out correctly. So, we return length of words and let us see what happens. So, let us try it with. So, now comes the next question of how do I make sure that my code is correct? Which actually brings us to an entirely separate concept called test and I will do that in a little while. So, later this afternoon I will introduce you tests. How do you test? How do you make test formal? It is not very difficult and how this enhances your programming productivity. But for now we will try to just what do we normally do as programmers? We will say we wrote this function I now have to test it out. So, I will immediately test it with some known input for which I know the answer. So, let us try that. So, word count A B C simplest possible sentence I would expect this to give me 3. Is that correct? It gives me 3. So, now one simple way by which I can formalize this process is to use something called something called assert which I will come to in a little while. But right now I know that this has 3 words the answer it is giving me is correct. So, now can I make it little more complicated? What am I worried about? What if I give multiple spaces? Now, what if I give first thing what if I give multiple spaces? Does it still give me 3? Let us see yes it gives us 3. Why? Because the split actually considers multiple spaces as one space. It already handles this case for us. So, we do not have to worry too much very good. So, this is very good. What if I have a new line? So, let us put a new line inside here that also works because the wide space includes new lines what more can we do? What if I add punctuation also gives me 3 correct. What if there is some trailing space? Also is fine. However, sometimes people write their text badly. What if I give a comma? If I give a space between the comma and the word? Normally when you write text you are not supposed to leave this. So, if I write this a proper sentence the quick apples oranges many people will actually type space comma banana. Unfortunately this will return 4. Why? Because the comma has space between them. Sometimes if you put a full stop you should not put a full stop here. So, it will treat this as 5 words. Now, unfortunately some of these are not that easy to handle. But how can we handle this? Is there a way by which we can easily do? Can we handle this in our code? Is there an easy way by which we can handle this in our code? So, the basic simplest problem works no problem. But now if the user makes some stupid mistake, if the text has some obviously there will be people who make who do not write this very carefully and comma is not a word. So, how do we now fix these problems? The first thing to do is it always helps to switch to something like an editor. Because for small tests you can use Python, the iPython interpreter itself. But once you start having to do something little more complicated it becomes a little tedious to go back and edit the function. For this we need two more new concepts. What I will do is I will leave this problem at this stage. I will revisit this problem in about 5 to 10 minutes. We will solve some more simple problems to give you more experience and then we will come back to solving this problem. And we will look at how to solve say this problem of how do you deal with punctuation characters? How do you deal with text having space punctuation characters and how do we still get the correct answer? And finally once we do that we will also look at how do I formalize this so that it becomes an easy process for us to follow. So, as to write nice clean tests, write clean code and be able to reuse this and we have to do it properly. What we will do now is the basic problem has been solved. The basic solution is the following. And remember when you write your solutions please remember to add the dark strings. It is always a good idea to add the documentation string. The other issue to remember is make sure you use appropriate variable names and function names. Do not call this f. I have been doing that just to illustrate to you concepts. When you are actually trying to solve problems you have to spend a lot of time thinking about how to solve the problem. And when you think about how to solve the problem remember you yourself may reread your code some other point of time. If you just say def f x no dark string no comments and you use variable names that are arbitrary you yourself will not be able to understand the code that you have written 5 days later or a week later or a month or a year later. And the idea that you should learn take away from this course is that when you write your code you should write it cleanly. You should make sure that the code is reusable. Even the small code bits you write should be reusable such that later on you may be able to reuse that piece of code. That is very important. And notice here that I am saying words equals input dot split because the variable names are up to my choice. You can type slightly verbose names there is no harm in doing. So now it is very clear that I am saying words is input dot split. Then I am doing learn of words I am returning that no problem. So we have solved the simplest possible problem. Let us consider problem 2 which is actually even simpler than this problem but there are some peculiarities you have to worry about. So problem 2 is let us go back to problem 2 is the following. I will give you 5 minutes to solve this problem. Write a function that is given a string and the function returns the number of lines in the string. Please make sure that again you write a clean function with documentation, with good argument values, good name of the function whatever you are using inside should be written cleanly. The second thing is just like how I did the approach I wrote the function, gave it some test input and checked. This has 1, this has 2. Try to think of all the corner cases. Try to think of all the corner cases what if the file has only has no lines? What if it is an empty file? Should I return 0? Should I return 1? What if it has only one line? What if there is a line at the end which is empty? How do you deal with all of these cases? Take each case come up with a suitable test problem saying oh this file will have this or this string and remember I am not giving you a file because if you have a file you have one more file to create instead create a suitable string. So if I want to say this is the contents of a file I can say data is triple coated string, simple space. So this is a file which contains just one space not a single new line. What will you consider this? How many lines does this file have? So you will have to make sure all these corner cases are taken care of. How will you solve this problem? As a hint please note that if I give you a string try out the number of methods that that string has. There are certain methods that might be convenient. I want you to explore string methods so and part of programming is doing this. Take a problem come up with okay if I give it this what will it do? If I give it that what will it do? You have to know how to do this. Please solve this problem and then I will come back to answering any questions you have about the various both these problems. The other thing is the coordinators who are with you are already gone through parts bulk of this course. So rather than asking me the question simple questions you please ask your coordinators to check and if they are not able to answer your question please get back because it is impossible for me to handle 600 people. That is the reason we did this in two stages. You have a coordinator please request your coordinator to help you okay and then you if the coordinator is unable to ask you get back. The other thing is I do not want you to please start sending me the solutions on the chat session. You start just typing randomly on the chat session I will start giving you more harder problems. You have to think please remember programming is not learning a syntax. If I teach you the language I can teach you 20 words or 100 words in one day and ask you to buy hard that does not mean you know how to write or speak in that language. You have to use you have to think you have to learn to form sentences. You have to take a problem solve that problem. If you do not solve a problem there is no point in attending a programming course. For any programming you must program okay so and you must do it you cannot have a proxy or a friend to do it for you or a student nothing. If you want to learn programming you have to do it even God cannot teach you programming you have to program okay. Please remember you have to program so try your best to program try to run these programs test it give it test input and think do not just immediately give you a first answer like yesterday many of you when you are doing the function and I asked you to read the function many of you just gave the answer straight away without even thinking for two seconds please do not do that think and please remember think of all the corner cases some of you are typing even now do not type me the answer because your answer is wrong I will give you 5 minutes think of all of the corner cases okay. So I have given you enough time it is a very simple problem I am just giving many of you time some of you may become frustrated or a simple problem if not time I am spending the time problem is many of you may be new to programming and many of you may not be able to not have thought about these things before and I want to make sure that everybody get something out of this if not I just go quickly I can go very fast. So it is important that everybody gets time to think about this okay. So before we answer this question some people are having some difficulties what I will do is I will get another terminal. So someone seems to be getting F apples oranges banana you are getting the answer 4 again I went through the whole process of explaining why you will get 4 if you give a space after the comma okay on some systems the answer is 3 and some system it is 4 is because you probably missed a space. So these kind of issues I think you should be able to solve locally with your coordinators okay. So please make sure that it is not some silly typographical error that you have made in your code and it will just unnecessarily take up time. So make sure you are checking with your coordinators and this problem is very simple okay. So the first thing is there are various string methods that you have seen there are 2 possible ways of doing remember that every problem there may be multiple ways of solving some are simple some will be very very complicated. One way of doing this you count the number of new lines okay right. So is there a string method I can use to find the number of new lines the answer is yes there is string dot so if I have input okay now I can do input dot count very good. So if I just count the number of new lines I may get the answer okay but the key to solving this problem is not in just saying oh I can have one way to solve it it is to get the solution correctly for all possible corner cases which is why I am asking you to think about the corner cases. So what I am going to do now is I am going to randomly pick some college pick somebody randomly inside that college and I am going to ask you to explicitly give me one corner case that you can think of. So I am now saying my solution should be def count lines input I am not going to give a dock string I will simply return return input dot count slash n and by the way everybody every one of you should be comfortable typing in code like this and getting correct answers typing syntactically correct code if you are stuck there you have a very serious problem and you have to do a lot of work make sure you are comfortable typing functions creating functions returning values all of that indenting all of this should be easy for you to do. So if I do this will it always work will it not work I am going to test if you guys have actually thought if all of you folks have actually thought about corner cases I am going to randomly pick some center okay I am going to switch to some other center because that is not going to work. So let me pick VIT yes please so yes now it is better hello yes we can hear you. So you are saying that if you have three lines this program will only return two because there are only two new lines correct am I understanding correctly fine that is one good case can you yes so can you pass the mic to the lady behind you and ask her for one more case when I use raw input can I just give multiple lines sir you are not supposed to ask me a question you are supposed to answer my question. So please answer the for everybody else I am just telling her she should not ask me a question she should answer the question I have asked her. So please answer the question can you give me one more test case I am talking English so that everybody can understand there are many people all over India they will not understand Tamil. So please give me one more test case which will fail for this and I gave you five minutes to think about this can you speak a little loudly yes. I gave eight lines sir I gave eight lines as my input stream but my output came as seven present triple quotes I gave in okay so but that is the same reason that that is the same reason the other can you give me another input simple case. Okay so I will answer the question myself I will give you a very simple thank you for your interaction simplest case will fail so if I say count lines a b c this is one line it is going to give me 0 so just counting this new lines is not going to work. So what if we say default should always be what if I return plus 1 right this may work okay so let us try this now it works now if I put a new line here that also works now this will also fail. So you should be able to think of one more case that breaks this part of the art of good programming is trying to think of oh this will not work that will work this may not work so you have to think exercise your brain a little bit how do we do that exercise your brain where will this fail fail for the simplest of all inputs if I give no if I give an empty line it is still telling me one nothing I am giving you an empty file it is saying it has one line the same error will happen if I did this following if I said a b slash n c slash n this will give me three it actually has only two lines okay so I cannot do it so if there is any line yeah or if you have two slash n's together someone is suggesting that this will give me three when I actually have I have two lines in the file if you do w c minus l it will tell you two lines but it is going to give me three the problem comes with the simplest case if I have an empty line it is treating it as a full line so we have a problem so you will either have to look at all empty lines remove all empty lines or you have to rethink our strategy somebody came up with the simplest possible solution that was correct is to use input dot split lines so now let us try this solution so look at how I am solving the problem I I know that I can solve it like this I know we can use split lines split lines will basically ensure that it splits on the lines I know I can count the number of new lines there are all these different approaches that I can take but now comes the problem how do I test this I now have to again say okay let us try count line zero correct empty line works a b c correct returns one line okay so what if I give a slash n b slash n c three correct right what if I give two new lines it is two so it is perfect so now remember what the whole process was I thought about the problem I wrote some implementation that I thought was correct remember we are going with the assumption that we are going to make mistakes I start with the problem I say okay I am going to try something we will first try it experiment make sure it works the second step is I need to think of corner cases first you need to know the language of course so that we are all assuming that we have some basic understanding now I need corner cases so I take empty file or empty string empty string should work this is one case second case if I have a line with just no new line but just some content that should give me one second test case third case is okay I give you some normal input this should give me three right then fourth case is similar to the first case slash n slash n it was giving me three now it gives me two so for all the known inputs that I have thought about my function returns the correct answer this is a common pattern that you will also have to start learning how to use and how to think in this fashion write your code think about what the solution should be express those cases okay but this input yes for that input yes for this input yes that means the function is correct so it is a common pattern where you take your function test cases that is cases that you know the answer for run the code against those cases and determine whether your program is functioning correct or not correct does this look like a sensible way to program supposing for example I were to write a function that calculates the square root of a floating point number of a positive floating point number so let us say I define square root of some x and I returned some solution how would I know that this function is correct the only way I have is I will say okay square root of 2 I know the answer square root of 4 should be 2 so I should say square root of 4 it is giving me 4 I know it is wrong okay so now I will say okay square root of 4 should be 2 so I should make sure that it returns 2 okay similarly there will be other inputs that I can supply I give it 0 it should not die it should give me 0 I give it 1 it should give me 1 okay so there are various ways by which you can write these little test cases and make sure that your function is so this is the way you should think about writing a program at the basic level the next level is how to make this function clean how to make it how do you write your code little better these are the next things that you need to think about okay so the question is you did s is equal to raw input remember that raw input will only give you one line as soon as you hit return on raw input it will accept that so let us see notice that it has escaped my slash n it is no longer a new line it has put slash slash n so if I print this s it will be exactly that string that literal string okay so now if I do count lines s it will only give me one which is correct so remember that when you do raw input if you give it a slash n it will stringify your slash n it is not going to treat it as a new line if you give it as a triple quote it will work okay that is to answer the question that was posed okay so what we will do now is we will switch tracks we will take one more simple problem and we will try to solve it so let us take problem number 3 print out the Fibonacci sequence less than 30 I hope this is visible print out the Fibonacci sequence less than 30 I will type it out again for you here again very very simple problem not even asking you to write a function right code just type it out straight on the interpreter many of you may not be familiar with the Fibonacci sequence but the Fibonacci sequence is as follows you start with 0 and 1 okay then the next number is the sum of the previous two numbers so it is 0 plus 1 is 1 then 1 plus 1 is 2 2 plus 1 is 3 3 plus 2 is 5 5 plus 3 is 8 so on and so forth I want you to write the code to generate this for all numbers for the first 30 that is the number that you get should be less than 30 the Fibonacci sequence till you hit 30 should be printed by you on screen so print out the Fibonacci sequence this is the Fibonacci sequence so I want you to print it out less than 30 I will give you 10 minutes to solve this problem it is a very simple problem please take the effort to try to get it right we will use this problem again we will build on this problem it start with just write the pure python code to solve this problem and take five minutes okay so I have given you plenty of time again very simple problem how do we solve this so the first thing you can do is you have to start somewhere okay we need the first two values you have to start at 0 so let us say a is equal to 0 and we also need some b equals 1 because we need two values to start the calculation okay now I need to do some kind of a check okay but first these first two values I can print should I print or no what we will do is we will say while what what should we check for while a is less than 30 first we will do print a comma why what am I doing now so maybe I should not use the words a is 0 and b is equal to 1 so first mistake I am making is what is a and what is b the meanings of the words are not that clear so what I can say is current equals 0 next equals 1 simple while current is less than 30 print current now I need to get the next now I have to do a swap right I want the current to become 1 the next to become 1 plus 0 is that correct so how does this proceed you go back this is current this is next right I want to first swap I want to make current 1 and proceed produce the next which is previous plus current so what is the I want current to become next which is the next value which is already 1 and I want next to become current or rather I want current to become next and next to become current plus next so how do I do this I have to use either a temporary variable but in python I can do it much simpler I can simply say current comma next equals next comma current plus next correct what am I doing now in this one line I am saying current is becoming next and when the right hand side is evaluated none of the variables have changed so it is first evaluating current plus next which is what 0 plus 1 which would be 1 current is becoming next anything else should I change anything nothing finished there is my solution notice that the solution is extremely simple I know the answer so obviously first time you are doing it you will struggle you will make mistake you will do something or the other but eventually you should arrive at this solution and notice how simple it is I am simply saying current is 0 next is 1 while current is less than 30 print current current comma next is next comma current plus next so the key is here which is saying set the current to the next and set the next to the current plus next and the important fact to remember is the right hand side is evaluated at the current point at before this current is set to next it is evaluated if not there is no point in doing this you are essentially using the same idea that we use for swap swapping of variables another way of doing this perhaps the more simple way to do it is temp is next yeah current is next and next is equal to temp plus next obviously this won't do anything current is 0 so current comma next I can now simplify this as current comma next is 0 comma 1 run this code why did not this work in fact the answer is wrong the answer is wrong because I said temp is next temp should be current so let us repeat this this is correct so obviously you will make some mistakes but notice that when you are trying to save a temporary variable it is always a little tricky which one do you say which one do you not say you obvious trick is you are setting current here so you want to save that that is the reason you are doing but notice how much simpler this code is it is essentially doing the following that is it so it is syntactically easier it is much easier cognitively follow and the pre produces the right output so very good so now we have our little Fibonacci function that produces a code now the next thing is to make this into a function so the next little assignment is this is very very easy all you need to do is introduce a function okay however what I will do now is I will show you I will switch to writing this in a separate file so that you are comfortable writing programs and learning how to do it as a in a development workflow so in some directory that you are want to keep all your code ideally your home directory I am going to create this file called fib I will write my original solution same exact solution I have put it into a file now what I will do now is I will keep the file active I will switch back to my ipython interpreter first thing I need to make sure is I am in the same directory that this file was created so ls yes it is there if not find some other crazy directory you do cd whatever full path to the directory slash blah blah okay normal cd command that you know how to use already use it to cd into that directory now that this file is there I want to run this code to run it ipython has this built-in called run so do percentage run fib dot py what percentage run does is it executes that code in a separate python like environment to produce the answers so for example if I do dell current next I run fib dot pi notice that I have the variables from my execution so this is a very convenient mechanism by which you can execute your code and also see what is going wrong for example let me make that mistake I made before I say and now I will run this now let's print current 32 wrong so I have the opportunity to inspect any variables print next it's 64 okay so it's doing something wrong come back to this edit it make whatever corrections I want it's gone so how do I stop control c obviously this won't work because so if you get stuck you hit a control c now run it again it runs okay so the strategy is if you want to write little more complicated code create a file call it something nice remember it helps to make this file that you don't call it 1 dot pi 2 dot pi don't use numerals make sure that the variable names that you use the file names that you use are valid variable names don't put minus use underscore don't use spaces in your file names for python files there's a reason for this I'll explain the reason in a little while but the general rule is and valid variable names are what anything starting with an alphabetical character it can contain letters digits and underscore it cannot contain arbitrary punctuation marks no comma no space no minus no and no hash valid variable names so anything alpha numeric plus underscore is the valid variable names use something that's a valid variable names so don't use 1 dot pi it's not a variable name one is a literal okay use 1 dot pi if you really must say 1 okay fine so now I have a mechanism by which I can write the code in a nice editor and execute it inside python inside ipython and be able to debug and figure out what's going on so let me change it back to the nice clean version okay now what is the next problem convert this to using a function so let's go back to our file and now we have the arduous task of notice that I'm using spaces for all of this notice that now it didn't do anything why because I'm not called that function my code has only defined this function it has not called the function so there are two ways to do it one is I either don't call it there and call it here or I add a line here saying okay however this is inconvenient every time I run it it will automatically run that function if I put two functions which function do I call it becomes a bit of a headache so let's not call it here let us call it explicitly from within ipython okay so I want you to do this create a file called fib.py write this function but now modify this function to take one argument called n and it should print Fibonacci sequence less than n that you supply very simple modification please make this modification write a file save it and learn how to run it from ipython like so like the fashion I have shown you okay so please do this five minutes it won't even take you two minutes to do it please do it five minutes as soon as you do this we will break for lunch okay we'll join a little bit we'll give you additional ten minutes at lunch break but I want you to go through this exercise of creating a file and running it in ipython okay please do it hello so we're back so the answer is extremely simple you must have just taken time to type out the code and all of that so let's say n and I will just say change the documentation string as well now this has become a nice function I go back into my ipython run it now I can call if I call fib now it will tell me it's an error why because fib takes exactly one argument you have given me 0 so let's now call it with 30 that works 300 that works 3000 that works 3000 that also works so the advantage is because it's a nice function I can just have it I can call it with different arguments and get the results okay so this is your this is the general workflow that you will employ when you are writing your own codes you'll have an editor editing your python file writing your code there and you will have an ipython session when you'll be trying out these stunts okay call this function okay now this is correct this is not correct so it gives you a clear way by which you're able to edit your code in a nice environment test your code interactively by using ipython's run command okay and every time you change that file it's a good idea to do a run command okay so with this we'll stop we'll pause we'll break for lunch after lunch we will resume solving these little little problems making them more and more complicated and then we'll do some test driven development and some other advanced notions based on we'll build on these small little chunks of code that we are writing so most of the session rest of the day will be mostly interactive we'll probably have a test again we want we'll make some modifications to our server to make sure you don't get these connection errors we'll intermediate we'll have you all login again and take the test once again we'll try that so we'll join for lunch a post lunch at about 215 sometime at 215 to 220 we will join okay see you soon