BYU logo Computer Science

Encode 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 over the cipher activity. So given a dictionary that maps letters to other letters, we call that a codex. We want to encode a message. The codex only has lowercase letters, but we could do uppercase also and preserve casing. We’ll deal with that maybe as a second feature.

[00:00:19] Let’s look at the data. So here I’ve got a codex. Here’s a dictionary that maps every letter to a different other letter, right? And so you can imagine replacing all A’s with D’s and all B’s with E’s and all C’s with Zs, et cetera. And we’re going to get this encoded message now that doesn’t look anything like its language of origin. And you can now pass secret notes in class and the instructor won’t know what you’re saying. Great.

[00:00:44] And so all we need to do is pass that code exam and some phrase from the terminal and encode it. So how do we do that? How are we going to map every letter here into its corresponding letter? And what do we do with letters that we don’t have a mapping for?

[00:01:02] Well, mapping pattern on a string. We’ve seen this one before so we can kind of set something up, right? New phrase equals empty, for a letter in phrase, pass for a minute. And then we’re going to return the new phrase. Great. So what do we need to do in this middle? Well, for every letter that I have in the phrase, if I have a mapping, use it. If I don’t have a mapping, then just leave it alone. Well, we have a pattern for that.

[00:01:37] You have a letter in codex, the letter is equal to codex letter. We’ll look up the letter in the codex, get its corresponding value. So if I have an A right now, A is in here, so I’m going to look it up, it’s going to return a D and I’m going to use that as the letter instead as a new phrase plus equals letter. And away we go. You could also do this where you could say new phrase plus equals this else, the phrase plus equals letter. Same effect, slightly different style.

[00:02:23] So let’s run it, let’s try that out. Clear up from our last video. What do we have here? There’s encode, right? So Python encode dot py and we need a phrase. I like fish. Nothing happened.

[00:02:45] Why not? Well, we can look up here, main, returns a new phrase, main, and we don’t actually print that phrase out. So here we’ll say new phrase equals this, print new phrase. Try that again.

[00:03:00] There we go. I like fish. You can see the I’s, each I gets replaced with an A. It’s encoded. Now, what would we do to preserve casing? So like right now capital I isn’t in our dictionary and it comes straight through. So we want to fix that. Let’s come up here and there’s a number of ways you can do it, but because this only contains lowercase at some point, we’re going to check that.

[00:03:26] So here we could say the letters in the codex. I know I have a lowercase letter and if the letter dot lower is in codex, then I know I have an uppercase letter now. And so I could say new phrase plus equals codex letter dot lower. But we’re going to make the result of that uppercase because that’s what we had up here. It was an uppercase letter. Otherwise, we’re just going to it straight through.

[00:03:56] All right. So now I can say I like fish. Now I get a capital A corresponding to the I and lowercase A to the other I’s, et cetera, right? And that’s how you write a simple encoding program. Have fun with that one.