Implementing a Caesar Cipher
00:13 The idea is that all of the letters of the alphabet are shifted by some fixed amount. So for example, if we’re going to be shifting all letters by 4, we take the standard alphabet in the natural order and then the letter A, if we shift by 4 becomes the letter E, B becomes the letter F, C becomes G, and so on.
01:04 If you think about what this is going to be doing to a string, which is an iterable, we’re going to be mapping this function that takes A to E, B to F, C to G, and so on at each character of the string.
We’re going to import from the
string module, we’re going to import all of the ASCII lowercase characters, and we’re going to import this and call it the
alphabet, and I’ll show you in a second what the
alphabet is going to be.
So go ahead and type
alphabet, and the
alphabet just contains all of the lowercase letters of the alphabet. We’re going to use that in our cipher, and you could import this right into the function. That way, if you send this function to somebody else, it’s all self-contained.
02:32 And we’re going to define in the function the amount that we want to shift by. Now, this may be a parameter that instead you want to pass into the function, but just for illustration purposes, let’s just define it right in the function.
So let’s just say we want to shift by
3. Now, because we’re only interested in the lowercase characters, we want to make sure that if the message comes in with uppercase letters, we want to first convert the input character into lowercase, so we’ll call the
.lower() method in the
This is going to be an integer between
alphabet is a string object, which is an iterable, and the index starts at
0 and the number of letters in the
26, so this will be an integer from
The number of letters in the alphabet is 26 and we can just hard-code that in, but maybe to make it a little bit more programmatic, we’ll just call the
len() (length) function on the
alphabet. That way, if you are using a different alphabet, you’re not hard-coding in
26 and you’re just using the length of the
alphabet that you’re using.
So, for example, if we take the letter
'a' and we’re shifting it by
3, that’ll give us
'd'. If we shift the letter
'b', that should give us
'e'. Now, the letter
'x' is going to be the first letter that wraps around the alphabet, so
'x' will get mapped to
'b', and then
'c'. All right, so why don’t we give this a try with a string?
Let’s clear that out and let’s map our Caesar cipher to an iterable, which is going to be a string, and maybe this is the secret message that we want to encode and so why don’t we write
"My secret message goes here". And we know we’re going to get an iterator.
So if we want to see this as a string, let’s join the elements that are returned by the
secret iterator with an empty string. And this’ll be okay because remember, the blank spaces in the original string are kept intact, so we’ll have the whitespaces already built in to the encrypted string.
06:36 All right! So this is, again, another good example of this general idea of wanting to map a function at every element of an iterable to return a new iterable that yields the transformed characters.
All right, this wraps up the lesson. In the next lesson and the one after that, we’re going to take a look at how we can combine the
map() function with two other important functions in functional programming.
Become a Member to join the conversation.