Python Institute PCAP – Control Flow Part 6
- Section 4 Assignments: Part 2
Assignment number four. Given a non-empty string like code, return a string like CCO C-O-D-C-O-D-E. So it’s basically spelling this out one letter at a time. It’s basically doubling up. So, for example, in this second case, ABC, it has a first, right? And then it has a B, and then it has ABC for AB. If we were to pass this as an argument, it would take A, and then it would do AB. All right, so this method is called grow string. And so why don’t you try this out on your own and then you can resume to watch my solution. Okay, welcome back. Hopefully this wasn’t too difficult. What we need is A for loop. We don’t even need an if statement or anything. All we’re doing is basically compounding this string in a way. So we do four I in range, and then we do the length of this string, which is the STR, the argument that’s passed in, and we can concatenate to a variable called result plus STR at the I plus one index position. Okay? And I have to define this result outside of this loop. And we’ll do that in just a moment. But I want you to understand what is going on here.
Okay? So I is going to take the place of every single character of the string that’s passed in. And so when we’re slicing the string, we can leave the first part empty because we want to start from the beginning of the string, right? Because we are concatenating the string every iteration, but we have to go up to but not including I plus one. So this allows us to go up to each character like that. All right? If we just had I, this wouldn’t work because the second value in the slice, of course, remember, is always up to but not including. So that is why we need a plus one.
There I plus one. We want to go one over for the second section of the slice, and then outside of this loop, we just return the result. All right? And so, of course going to complain. It says, how do we get this result? We need to define this result out here. We’ll define it or initialize it with an empty string value, and then we basically concatenate to this result every iteration so we can test this functionality out. I can invoke this in a print statement print function. We could do grow string. And the string that I want to pass will just be my name, imtiaz. And you’ll see that. Let’s run this notice. It starts with I, and then it goes to IMIM, T, Imti, Imtiaz, and so on.
Hopefully you know how to spell my name by now. All right, so that’s that. Let’s move on to the next assignment, assignment number five. So for this assignment, let’s review the requirements. Define a method that accepts a list as an argument and returns true if one of the first four elements in the list is a six, okay? The list length may be less than four. Okay? So the caveat here is one of the first four elements. If it contains the number six, then it’s supposed to return true. Otherwise return false. The list length may be less than four. So let’s go over this example. First three is the method name or function name and we’re passing in this list.
And so this is going to return true because six is in fact one of the four first elements in the list. In this case, in the second invocation, six is at the end of this and the first four elements do not contain the letter, the number six. And that’s why it’s returning false. And then in this case, in the third invocation, there’s no six anywhere anyway. So this is going to return false. So why don’t you pause the video, try this out on your own, and then you can resume to watch my solution. All right, welcome back. Hopefully you were able to solve it. If not, you were able to struggle through it and you put an effort to try to solve this on your own if you’re just moving on to the solution. But the key thing to keep in mind in this puzzle is that we only need to take into consideration the first four elements of the list. So what we could do is I’m going to define a variable called end and say that the length in the list of numbers, which is right here, the length of that is going to be the end.
That’s what we start with. And then if the end is greater than four, right, then we assign end to four because that’s all we really care about. We only want to look at the first four elements. So now since end is four, outside of this if statement, we’re done with that. If statement, we can do a four. I in range and then do end. Okay? So this let me just put a comment here. This is going to loop over index positions 0123, okay? So inside of this loop, we can have an if statement. If the if index position of the list, if it contains a six, right? Then we’re just going to return true. All right? Otherwise we continue to loop through this list, make sure this is numbers, because the argument here, the list argument variable is numbers. So unless it finds a six, it’s going to continue to go up to the end. And in this case, we only consider up to the first four.
If it finds a six, it returns true. Otherwise, outside of this for loop, if we got out here, if we get here, the loop is over and we didn’t find a six. And so in that case, what do we do? We’ll return false and that’s it. Hopefully you were able to solve this. Now again, there’s many ways of doing this, but I think this is a pretty easy way to understand how to solve this problem. We first decide what the ending position is going to be. And since we only are interested in the first four, we set end to be four if it’s greater than the number four. If it’s not greater than number four, then we don’t have to worry because we know that it’s only going to consider the first section of the list, which may only be one or two or three elements. So let’s do a few trial runs here.
I’m going to print the invocation of this function we’ll pass in the list. So let’s do 12630. Put another zero there. I’m going to run a couple of different examples here. Okay, so in the first one we have a six in the first four. In the second one, let’s change this to and make a six at the end. So this is supposed to return false. So this is going to return false. This is going to return true. Okay, in the third case, I’m just going to have the number six here. So this is also supposed to return true. And in the last one let’s just have yeah, we could just leave it empty.
This should also work. So this is going to return false. So let’s run this right click and run the file. And so we get the first invocation to be true, the second invocation to be false. The third invocation here is true, and then finally the empty list is also false. So this code is working as expected. Make sure to run your code through these trials. That’s really the only way to find out if your code is absolutely correct. Let’s move on to the next assignment, assignment number six. Okay, so this assignment is saying that we want to create a method called Last two. And by the way, this is interchangeable for function. Method is used to refer to a function that’s in a class. And a function is something that could be as part of a script that does not belong to a class.
So let’s simulate that this is probably a method in some class somewhere, okay? That’s why but again, the correct way of actually referring to this would be a function. Let me change that right now. I don’t like the way this is worded. So create a function and I’m just going to rename it here as well. Okay, so this term, the function and method, is often abused in Python all the time and we should be more specific. But let’s go over the rest of this assignment. So create a function called last two that accepts a string argument. The function should return the count of the number of times that the last two characters appear in the rest of the string. You should not count the last two characters as an occurrence. Okay, the last two characters is just the sequence your function should look for in the remaining string. Okay? So here’s an example. So this string right here, where it has high XXX, high should yield one. So it gives a comment here. We won’t count the end substring. So this end substring right here should not be double counted. There’s only one occurrence of this string in the remaining string, which is right here. Okay? That’s why it returns one. So let’s go over these examples. In the first, invocation the last two characters is high, and that is only repeated once in the remaining string, which is right here, okay? In the second, invocation, Xx is only repeated once in the remaining string. So that occurrence is right here. So in this third example right here, this is giving a two. So let’s see what’s going on. We have two X’s, right? That’s the substring, the last two characters. And this is being repeated two times, apparently. So let’s go through this.
In the remaining string, we have Xx here, and then we also have Xx here. So even though there’s three X’s, but the substring of Xx’s is there twice, right? The middle X is being counted for each of those occurrences. So we have xx and then Xx. So that returns two. Okay, so why don’t you pause the video and try this assignment out on your own and then you can resume to watch my solution. Okay, welcome back. So let’s get started. The first thing we want to do is to rule out really short strings, okay? So let’s say if the string has only one letter and what do we do? That should just return a zero. So in that case, we could say if length of this string is less than two, right? We should just return a zero. And this perhaps should be less than or equal to two. Because if there’s only two letters in there, we don’t see a repeat in the rest of the string. So that it just should just return zero. Now we move on to the real stuff. So let’s extract the last two characters of a given string.
So I’ll create a variable called last two, and we can say that we need to slice it. So we do length of STR and then we do minus two to go back to places in the end of the string. And then we do a colon. And then we leave the second part of the slice empty because that will mean that we go all the way to the end of the string. So this gives us the last two characters of any given string. Now another way to do the same thing, I’ll leave that in the comments here is let’s just say we’ll say last two characters can also be extracted with STR minus two colon. You leave the second part empty right here. So this does the same thing. It’s just that this approach that we’re using here gives a positive index position of where to start the end substring, whereas this one uses a negative index position. But in both situations, we go back two places and to the end of the string. Okay? So now we need a counter variable. I’ll just put that on the next line here. Count is equal to zero.
And this count variable is the thing that’s going to be incremented every time we see another occurrence of this last two characters. Okay? So for that, we’re going to need a loop. So we could do four I in range, and the range could be Len STR two. Because we only care about the remainder of the string, we’ve already extracted the last two characters out here, so we don’t need to include them in the looping. We’re only concerned with the rest of the string. So we do a colon. Next, we can have a variable called sub, and this will actually be a substring range of two characters as we loop through the string. So for that, we’re going to need a slicing. And we say that I to I plus two, and this will give us two characters. And we start from the beginning of the string and we compare each of those two characters with the last two that we’ve already extracted. So we could say if sub is equal to last two, then we increment. Count is equal to count plus one.
Okay, and by the way, why did I do this I to I plus two? Well, remember, when you’re slicing, the second part here is non inclusive. It goes up to but not including. So if we just did I plus one like that, that would just give us I, the single character. But we want two characters. So for that, we need to do I plus two. So this loop will continue searching and the count will be incremented every time we encounter a substring that is equal to the last two characters that we’ve extracted up there. And it goes up to but not including the last two characters of the string.
Okay? And then outside of this loop, outside of this if statement, we just return the count because that’s what this method calls for. It wants the number of occurrences in which the last two characters were encountered in the remaining string. Okay, so that’s it. If you got this great job, there’s, of course, many ways of doing the same thing. This is one approach to solving it.
So let’s actually test this functionality out to make sure it’s working. I’m going to just copy these lines of code up here and paste it. This arrow notation is not part of Python, so I’m just going to comment that out and we’ll wrap this in a print statement so that we can actually see the result of invoking each of these functions and like that. And there we go. So let’s run it and make sure that we get the expected values. So in the first occurrence, we get one. All right. In the second invocation, we get one, and in the third one, we get two. Perfect. Let’s test this out by adding another X in here. Now, it should return one. Let’s count it out. Two, three. It should get three occurrences. So let’s change this to a three. That’s what we expect to see when we run this. So let’s run this, and there we go. We get three. So it’s working as expected.
- Section 4 Assignments: Advanced Assignment
Real world, you’re going to have problems that require a lot more instructions to deal with rather than just a small task. And programming is all about breaking the problems down into smaller chunks. And we solved various chunks in all of these different assignments. Now assignment number nine is a little bit more involved and I wanted to include that as part of this assignment. And I want you to really smash your head against the screen struggling with trying to solve this problem. That’s where I want you to be right now, because trust me, the more pain you go through and trying to master programming at this point, the easier it’s going to be later. Trust me, okay? And so it’s all about the struggle. For some of you this might not be as challenging, and for others it will be, all right? So I’m just preparing you for that. And so I don’t want you to skip this assignment either, okay? I want you to really work on this. Don’t just move on to the solution that I’m going to present after I give you a chance to work on it. So let’s get started. So here it says we have two variables, fr and D, all right? And so if I scroll down, that’s these guys right here, fr and D.
So notice fr is a list of strings and D is a dictionary of strings. We have the key and then we have the value, key value and so on. So what is it saying? Fr is a list of strings, these dictionary with email addresses as keys and numbers as values, numbers in string format. As we saw in the dictionary down here, the values here are numbers and the emails are keys. So let’s keep going through this write code to replace the email address in each of the strings in the fr list with the associated value of that email looked up from the dictionary, D. So what is it saying? Basically it’s saying that in the fr list of strings we’ve got email addresses and those are right here. This email, this email, these are all emails, right? This first piece of each of these strings is an email address and it’s saying that we need to look up this email in this dictionary and if we find it, we need to replace it with the value. So for example, this seven@component. com, this email does exist in the dictionary, which is right here. So I need to replace in fr, I need to replace this email with the actual value that’s associated with that email, which is right here, right? So 199 is actually supposed to go in fr, and then for this as well, this is the same email being repeated. So this would also have 199.
Now for 13 at compone. com, that’s down here, this has the value 205. So for that it would have a 205 here. All right? And then this is an email that does not exist in the dictionary. So we’ll go over what we need to do there. So hopefully you understand what we’re trying to do here. So let’s scroll back up and complete the rest of the instructions. So again, it’s saying write code to replace the email address in each of the strings in the fr list with the associated value of that email looked up from the dictionary. D, that’s what we just discussed. Now, if the dictionary does not contain the email found in the list, add a new entry in the dictionary for the email found in the fr list. So meaning if we scroll down this 26 at. Compone. com, this email does not exist in the dictionary, so we don’t have anything to look up. So it’s asking us to actually in this case, when we come across this particular email and we try to look up in the dictionary and we don’t find it, we actually need to add this email to the dictionary, okay, as a fourth item in this dictionary.
And so for the value for that email, I actually discussed what we need to put there. Let’s scroll back up. So if the dictionary does not contain that email found on the list, add a new entry, okay, the value for this email, the value for this new email key will be the next highest value number in the dictionary in string format. So this particular email that we did not find in the dictionary, we need to, first of all, add it to the dictionary and then add the next highest value, the numeric value, as the value for that key, all right? So essentially what we are trying to do is we have to put this as a new entry, and then the value for that was going to be 206, because 205 is the largest number. So this email will be added to this dictionary with 206 as the new value for that, all right? Now, when we do that, what I also want you to do is after you add that new value to the dictionary, I’d like you to add that 206 back up here, replace this email address with the number 206, okay? Just like we’re doing with the rest of the emails here, all right? And that’s what I discussed doing.
Once the dictionary is populated with the new email key, add a new number value, replace that email’s occurrence in the fr list with the number value meaning. So once we see that, okay, this email does not exist in this dictionary, we have to first of all, add it as the key and the value we give it is a new number, and then that number needs to be added back to this fr, right? We have to replace this email with that 206. All right? So essentially at the end, when we run our code, we will only see numbers here, right? In the fr list, we’ll only see numbers and those numbers will be, of course, coming from the dictionary down here. And this dictionary will have this new email added to it. Okay? So take your time to really understand this problem. What you need to do. Most of the effort required in programming is really trying to understand the problem. I can’t tell you how many times people just start writing the lines of code and they don’t fully understand the requirement, and they start typing code, and then they get stuck in the middle and they’re like, oh, wait, I had to do this, I had to do that.
You have to first understand the problem. And I gave this sort of a long project type of problem to you for you to parse in your head and understand what needs to be done. And then I’d like you to execute the code to make this kind of a situation possible. So here it says the output of running your completed code should be the following. And now you can see that for the fr value of fr contains the numbers, right? 199, 199, 205, and those are the numbers for this email, this email and this email looked up from the dictionary one, 9200, and then it also contains the number 206. Where did this 206 come from? Well, 206 is the next highest value from the dictionary. So what needs to be done is we need to add this email in the dictionary first with the new value two. And once we do that, we look up the 206 and we replace it. We replace this email with the 206. So fr should no longer have email addresses, should only have numbers looked up from the dictionary, and the dictionary should contain all of the email addresses, and there are numbers, and that’s what this result is showing. So the fr list, the output of that is going to look like this, and the output of the dictionary D is going to look like this, which contains all the email addresses with the numbers, all right? And so the way this code is right now, I don’t want you to touch any of this. Don’t touch the fr or the D manually. I’d like you to do that through code. So this code right here should not be touched by you.
Don’t touch that code. That’s part of the assignment. The code that you’re supposed to write is after this line right here. All right? Notice I started it up for you. We’re looping over to the fr list and we’re working with each of the lines, and the rest is up to you to solve. And I created this line list, which is going to contain each of the lines. And then later you’ll have to reassign this line list back to the fr once it’s completed. And the D, the dictionary is also going to be modified in the code that you write. So really the code that you’re writing is from this line to this line right here, only in between these lines. Of course, you can make the code as long as you want, but don’t change the code down here because this is the actual output. Don’t change it below. This is the actual output that is going to give you the result that’s shown here.
All right. So hopefully this is more of a comprehensive project that requires some logical thinking for you to plan breaking this problem down to smaller chunks and try to solve it yourself with dictionaries and lists and so on looping. So I set it up for you. Why don’t you take a moment now? Well, it might not take a moment. It might take several minutes or several hours, or it might even take days for some that’s okay. I want you to struggle with this, try to solve this yourself and then you can resume to watch my solution. Pause the video now. All right, welcome back. Hopefully that wasn’t too difficult. So step number one was to of course understand the problem.
I hope you read through this again and understood what I was trying to do here. And then once you understood this problem and you understood what the output should look like, only then you should have resumed to actually writing your code. And so if you’re at that step and you did this, I’m going to start filling out the code now and we’ll go through what needs to be done. So the first thing is I’m going to create a variable called columns, and I’m going to split do line split, because each of the lines in here are the strings from the fr list. And I’m going to split it on the pipe, because notice, each of these little data elements are kind of like columns separated by these pipes, right? And I’m only interested in getting the first thing right here, which is the email.
So I do that split and this gives us the columns. Now if I do lookup value, I’ll call it lookup value is going to be the columns at the 0th index position. So this is the actual lookup value that I need to look up in the dictionary to see what the number is. And if I find that number, that of course needs to be replaced. But we won’t do that directly to the Fr. We’ll put that data in this line list result and only then towards the end when we’re all done, we’ll assign line list back to fr. Okay, so we get the lookup value and now we can have an if statement to check. So if d get lookup value.
So we’re now doing a lookup in the dictionary. There’s two ways to do this. One is to equal none like that. This is no good. This is actually an antipattern. You don’t want to be using equal equals with none. The correct way of doing this comparison is is. So if, for example, we don’t find the lookup value in the dictionary. If we don’t find it, it’s going to give a none D get lookup value if the email that we found from this fr does not exist in this D, right, the D get is going to return a none. All right? And so if it’s unknown, then that means let me just put a comment here can’t find in dictionary, okay? Now another way to do this if statement and I’m going to just put another comment here. We could have done if lookup val not in D keys, right? This is another way of doing the same thing that we’re doing here, okay? So I’ll just leave that in the comments.
Now after that, what we need to do is get the next highest number in the dictionary. So meaning that we don’t find that email in the dictionary, we need to add that email into the dictionary. And so we could do next number variable and this will be the max of D values, all right? And I’m going to turn this into an int because although it’s going to be able to get the max value doing the D dot values, it’ll be smart enough to know that this is a string integer. But ideally we want the end result to be an integer because we need to first increment it. So I have to do plus one. So what this is going to do is it’s going to take 205. It’s going to first do a maximum D dot values and that’s going to give us a max across each of these. And obviously the maximum number is 205. So that’s what this int max D value is going to give us, right? It’s going to give this 205 in an integer format. And then I do a plus one, I increment it. So I get a 206. And then we give that 206 to the next number. And so now we add to the dictionary the lookup value that we couldn’t find. So this lookup value was retrieved from the fr, right, from the line in the fr list. We don’t find that email.
So we’re adding that email to the dictionary and we assign it as a value. We assign it to next number and we also have to do an STR around it because we’re saving these numbers as strings. Okay? So what this is going to do, this code is going to take this email that it did not find the dictionary. It’s going to add it in this dictionary and give a 206 as the value in the D. That’s what this line of code does. The next thing we need is this columns at the 0th index position. This is the column that’s coming from the line, right? That’s coming from the fr list. We need to obviously put that number in the line. So for that we would do columns at the index position zero. We assign it to strum right, same thing. And once we do that we can do line list append and then we can append it with pipes and then we can use the dot join function. Remember the join join columns like that.
So columns is a list containing each of the items in the given line. So each of these items is going to be a list item in the columns variable because it’s split across this. And so then we’re joining all of that into one string and we’re appending it to line list. So each line here is going to be added as a string. Because of this, each line is going to be added with the first value changed from an email to an actual number that’s looked up from the dictionary and it’s being appended to this line list variable. Now later I’m going to assign line lists back to the fr because we don’t want to be changing fr directly in a loop. That’s an antipenner, never do that. You should never be changing the contents of a list that you’re actually looping through. That’s why I have this line list variable that we can assign to later. So we’re essentially creating a new list here called line list and attending to it the correct values that we want. All right, now after the Sift statement we need an else.
And in the else, that basically means that we were able to find a lookup value in the dictionary. Okay, meaning that columns at zero, which is the email address, which is the 0th index position in the list known as columns. The first value is the email and if we go to else clause here, that means we were able to find that in the dictionary. So in that case we could just simply do columns at the 0th index position and do D get and then do columns at the 0th index position like that. We could do this, or we could also do this lookup value because we’ve already got the lookup value up there so we could also do this. All right, so we’re getting the actual numerical value from the dictionary for a given email that we were able to find. Remember in this else clause we were able to find the email. Let me just put a comment here. We were able to find email in dictionary, okay, that’s with the else clause and here can’t find email in the dictionary, all right, so after we sign it back to columns at 0th index position, we could do line list and just basically do the same thing that we’re doing up here. I’m just going to copy this line of code and paste it here like that and that’s it, that’s the answer. So now towards the end, what we need to do is since we’re printing fr and D, they are basically going to be modified with the new values. So we need to just reassign the line list back to fr like that.
And now when we run this, we should see values that are like this. So let’s run this. I’m going to right click here, run assignment nine. Whoops. I think we’ve got an error. What is it saying? So line number 104, what’s going on here? Oh, I know why I had the solution typed up down here. So that’s why it basically ran that code twice. I’m going to comment that out. I’m going to leave the solution here at the end of the file for you just so you have the code, because you’re going to see this file empty when you receive it. So let’s rerun that and you’ll see that it should be working. And there you go, it is working. So let’s see if the data is correct. So 199, that’s coming from the dictionary for seven@component. com. That is in fact, 199. 199. For 13. Comp one, we get two five. So we get two five as the third value here, and then for the fourth value is 206, which does not exist here yet. But we know that if we were to add this particular email address to this dictionary, the next value that it will receive is 206. And so we would add that back to the Fr, and that is why we see the 206 there. And then in the dictionary, you can see all of the emails are in fact there. Okay.
As well as 26. All right, so there we go. That was it. That’s the assignment. And the challenge behind this assignment was for you to thoroughly go through these instructions, because there’s a handful of them, and to understand exactly what the instructions say, and then for you to figure out how to break this problem down into smaller pieces and solve it. And you were able to hopefully, if you struggled with this, hopefully you were able to do it. This is going to be a challenging problem for many. So if you struggle with this, that’s totally fine. Even if you struggled with the other problems, that’s totally understandable. My job is to make sure I give you the proper instruction so that you can understand what’s going on and you practice yourself and struggle with it yourself, because most of the learning is not going to be done when I’m teaching it to you.
Trust me, it’s going to be done when you want to smash your head on the screen struggling to try to solve it yourself. That’s what programming is all about. And if you struggled with this, great job, and if you were able to do it even better now, if you weren’t able to do it and you struggled with this, come back next week to try to solve this problem on your own again. By then you would have probably forgotten most of the instructions that I gave you in this video, and you’ll be able to approach it fresh and try to solve it, struggle with it again yourself and the more you forget and then you relearn forget. Relearn. That’s what really.