Caesar Cipher

The questions below are due on Thursday July 15, 2021; 11:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

Description

Caesar's Cipher is a kind of simple plain-text cipher that has been around since (at least) the time of Julius Caesar. It takes in a plain-text string and translates it into a new string by shifting the alphabet being used. This produces an encrypted message that cannot be easily read by someone who doesn't know the "shift" value.

In this question, we'll write a program to implement a cipher of this form.

Consider laying out the alphabet along a line and assigning a number to each character ("a" is 0, "b" is 1, "c" is 2, and so on). In a Caesar Cipher:

  • the numerical value of each character is determined,
  • a constant value (the "shift") is added to the number associated with each of the characters in the original sequence
    • if the shifted value goes beyond 25, it should wrap back around to 0
    • if the shifted value goes below 0, it should wrap back around to 25
  • the resuling numbers are converted back into characters.

Define a function caesar_cipher, which takes two arguments:

  • a string of characters to be encrypted, and
  • an integer representing the "shift" value.

caesar_cipher should return a string representing the encrypted form of the input string.

Slight variations on this technique exist, but in our version we will:

  • shift letters as described above, after converting upper-case letters in the input string to lower-case, and
  • leave all non-letter characters unchanged in the resulting string1

Below are several examples:

caesar_cipher('abcd',1)  # returns 'bcde'
caesar_cipher('ABCD',1)  # returns 'bcde' (convert to lower-case first)
caesar_cipher('abcd',-1)  # returns 'zabc'
caesar_cipher('abcd',-3)  # returns 'xyza'

caesar_cipher('this is pretty neat!',2)  # returns 'vjku ku rtgvva pgcv!'

caesar_cipher("don't you feel like a roman emperor?",13)  # "qba'g lbh srry yvxr n ebzna rzcrebe?"
caesar_cipher("don't you feel like a roman emperor?",2)  # "fqp'v aqw hggn nkmg c tqocp gorgtqt?"
caesar_cipher("don't you feel like a roman emperor?",-24)  # "fqp'v aqw hggn nkmg c tqocp gorgtqt?"

Helpful Resources

  • This exercise isn't focused on functions as first class objects, but rather a review of course content - specifically strings, looping, and conditionals. If you want to review how we iterate over strings and make new strings, you can check out week 2 assignments
  • The string module contains a string consisting of all the lower-case English letters, available as string.ascii_lowercase. To use this, you must first import the string module by writing import string. Then you can try printing out string.ascii_lowercase to see what it looks like.
  • You can create a string consisting of all the characters in a string x but converted to lowercase by calling x.lower().
  • You could consider making a mapping from letters to a numeric value and a mapping from numeric value back to letter. This would allow you to go from a letter "c" to a value such as 3, add an amount (7 for example to get 10) and then convert back to a letter, in this case "j".
  • Recall that you can use the modulo operator % in order to get the positive remainder. For example 29 % 26 will return 3.

Testing

Make a plan before you write any code, and sketch out your plan on paper. Make sure to write and test your code by hand for some simple examples (simpler than those shown above!) before using Python. Draw environment diagrams for a few different test cases. Then, only once you're convinced it will work, run your code with Python on your own machine as a test.

Submission

When you are ready (once you have simulated by hand and tested on your own machine and are convinced that your program will do the right thing), upload your file below for testing:

  No file selected

Back to exercises


 
Footnotes

1This is maybe not the most secure thing in the world, but that's okay for now... (click to return to text)