CS 111: Introduction to Computer Science

Implementing the Caesar cipher

Due: 11:59PM Wednesday, September 23. Hand in as caesar.py.

The Caesar cipher is a very simple way of creating coded messages. The original cipher, attributed to Julius Caesar, involves shifting every letter in a plaintext message 3 positions further down in the alphabet to get the corresponding ciphertext message. For example, a Q in the plaintext message would be shifted by three postions to become a T in the ciphertext. A plaintext message of "YAK AND ZEBRA" would be transformed to the ciphertext message "BDN DQG CHEUD". Note that letters near the end of the alphabet wrap around to the beginning (e.g. Y + 3 = B). You can, of course, perform a Caesar cipher transformation using other values than 3 for the shift. "YAK AND ZEBRA" with a shift of 4, for example, becomes "CEO ERH DIFVE".

In this assignment, you will write a function that implements the Caesar cipher. This will give you some practice encapsulating a collection of operations into a Python function. This assignment will also introduce the use of command-line arguments in Python programs.

Your task

Write a program that:

Your caesar function should look like this:

def caesar(shift, plaintext): '''Returns ciphertext obtained by transforming every letter in the plaintext by a Caesar cipher by the specified shift. Non-letter characters should remain unchanged in the ciphertext.''' [Your code goes here] return [something]

A typical session with your completed program might look like this:


prompt> python caesar.py 3 "follow the elk"
iroorz wkh hon
prompt>

Note that if you want your plaintext to include spaces, you need to enclose it in quotation marks so the shell will consider the plaintext to be a single command-line parameter.

A little help

We will discuss all of these ideas in class on Monday (9/21).

Start early, have fun, and ask questions.