Question: Exercise 3 : Caesar Cipher With mapper, it is straightforward to write a general function to apply a Caesar cipher across any number of letters.

Exercise 3: Caesar Cipher
With mapper, it is straightforward to write a general function to apply a Caesar cipher across any number of letters.
Although there are a few ways to accomplish this, most students have found it more straightforward to use mapper directly on each letter. (This is more efficient for short messages. You could also ese mapper to prepare a dictionary mapping each letter, and then use the dict to map each letter as it is encoded. This is more efficient for longer messages, but seems to be too involved for the length of a lab.)
Compose a function caesar_cipher which accepts a string message and an integer offset. It should return message with each letter mapped by offset to another letter in the alphabet. offset should default to 1 in the case that the user does not supply it.
[]:
#grade
# define your function here
def caesar_cipher(message, offset=1):
# Encode the message (converted to upper case) Hint: use the mapper function
## YOU WRITE THIS BLOCK (USE THE ABOVE CODE IF YOU NEED HELP)
## Step 1: Convert to upper case
## Step 2: Encode message and add to a variable encoded
# Finally, return the encoded message.
pass
[]:
# test your code here. You may edit this cell, and you may use any sample text, but the following is provided for
# convenience.
text ="""The Sign of Fear, by Arthur Conan Doyle"""
caesar_cipher(text)
[]:
# it should pass this test---do NOT edit this cell
# test case with specified offset
test_text ="""A man should keep his little brain-attic stocked with all the furniture that he is likely to use, and the
rest he can put away in the lumber-room of his library, where he can get it if he wants it."""
code_text ="""F RFS XMTZQI PJJU MNX QNYYQJ GWFNS-FYYNH XYTHPJI BNYM FQQ YMJ KZWSNYZWJ YMFY MJ NX QNPJQD YT ZXJ, FSI YMJ
WJXY MJ HFS UZY FBFD NS YMJ QZRGJW-WTTR TK MNX QNGWFWD, BMJWJ MJ HFS LJY NY NK MJ BFSYX NY."""
result_text = caesar_cipher( test_text,5)
assert result_text == code_text
print( 'Success!')
[]:
# it should pass this test---do NOT edit this cell
# test default case
test_text ="""You know my methods, Watson."""
code_text ="""ZPV LOPX NZ NFUIPET, XBUTPO."""
result_text = caesar_cipher( test_text )
assert result_text == code_text
print( 'Success!')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!