BYU logo Computer Science

Word Swap Transcript

Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.

[00:00:00] Instructor: Hey! Let’s go through the swap problem together. What’s the job here? So we’re going to write a program that asks the user for a phrase, then two words or substrings to swap, then we swap those two words and print the result.

[00:00:15] So for example, it’s raining cats and dogs. I want to swap cats and dogs, so now it’s raining dogs and cats. Or do you like where you live? I want to replace “ke” with “ve” and so now it says, do you live where you like?

[00:00:29] So what’s the overall structure here? I’ve got some repeated process, right? So there’s a loop and at each round of the loop, I’m going to get a phrase and two words and I’m going to do something with that phrase and two words, in a loop. So let’s set that part up and then we’ll worry about the do something part.

[00:00:51] So I come in here. Word swap. We’ve got an empty file. Def main, pass, if name equals main, all main. Great. Again, when you see an empty file, just stick this in there. Then it’s not empty and it’s not so intimidating anymore. Awesome.

[00:01:11] So I’ve got my main and so now I’ve got this loop idea, right? I’m going to loop through grabbing these things over and over and over until you give me an empty phrase, right? So while true phrase equals input. If not phrase, break. And then I need a word. Word1 equals input. Word 1. Put that on that side. There we go. Word2 equals input. Word 2. There we go.

[00:01:53] And now we want to print out magic on my phrase with the word one and word two. Maybe magic’s not a great word there. You know, naming things can be tricky sometimes. But we want to do the swap thing, right? We could call it do swap. All right, maybe that’s a little bit better. So let’s create a function called do swap.

[00:02:19] So given a phrase, I want to replace it with word one and word two. I want to replace word one with word two, but I also want to replace word two with word one, right? And so we could say return phrase dot replace. So we want to change the phrase, right? You could say, well, I want to replace word one with word two and then I want to replace word two with word one.

[00:02:49] You could approach it that way. That’s maybe the first intuitive thing. But let’s draw this logic out for just a second. I go over here. So if I have, let’s say a two and a three and I want to swap them.

[00:03:06] So if I take two and I replace it with three, now I’ve got a three and a three. And now if I take the three and replace it with two, that three gets replaced with a two and this three gets replaced with a two and I end up with a two and a two. That’s not what we wanted. We wanted a three and a two. We wanted those trading places.

[00:03:26] And so you can see that first replace gets rid of the original, and so then everything gets changed that last round. We don’t want to do that. So I’m going to show you how we typically approach this using what’s called the swapping pattern.

[00:03:43] So we come in here and I’m going to create a temporary substring that we use as a placeholder. I use the variable name tmp, it means temporary or temp. You’ll see it a lot used in code to represent just kind of like a placeholder for something that I only need for just a moment.

[00:04:05] And so here I want to use a substring or symbol that is very unlikely to ever show up. So I could say, you know, something like this. And what I’m going to do is I’m going to replace word one with this temporary thing. Now I can replace word two with word one and then I can replace the temporary variable with word two.

[00:04:40] What does that look like? Well, if you come back over here, let’s close that up.

[00:04:44] So I have my two and my three in the string, right? Well, when I replace two with some series of symbols, so I’ve got like this ampersand and ampersand. So I’ve got this new symbol here that I’ve replaced and I’ve still got a three. Now, I can replace the three with a two. So I’ve got ampersand…that’s not an ampersand, that’s an @ symbol, and a two now. And now I replace the at with a three and now I have a three and a two.

[00:05:21] All right. Let’s draw that out in code. Maybe that’s a little bit easier, right? What’s the logic here? Get rid of these. So I can say I’ve got apple, pear, and then I’ve got ats, pear, and then I’ve got ats, apple, and then I got pear, apple, right? So I’ve successfully swapped and not apple pear, pear pear, apple apple. So approaching it this way when I replace apple with pear and now I have two pears and I replace pear with apple, all the pears get replaced.

[00:06:08] That’s not what we want, right? So I use this symbol to kind of hide that original value and tuck it out of the way so it doesn’t get replaced or mingled with anything. And so then later on, it stays there as I replace only the second apple with pear and then I go back and plug that in.

[00:06:27] So it’s an interesting pattern. We actually use it a lot in programming, this idea that I have two things that I need to swap specifically. You use this very same concept over and over and over. So take some time to feel comfortable with that. But now, we’ve got do swap, right? We should print those out. Let’s try it out. See what we think.

[00:06:49] So. Given a phrase, cats and dogs. Sure, right? Cats, dogs. Then we get dogs and cats. Cool. I could also take a phrase like, hello, how are you? And I can take a word like o and I can take a word like e and swap them around, right? All the e’s have now become o’s and all of the o’s have now become e’s. End game. Well, in that there it shows you how you can approach the word swap problem.