CS 111: Introduction to Computer Science

Winter 2017

HW04: Caesar cipher

Due: Wednesday, 01/18 at 22:00

If you are pair programming, do NOT start coding without your partner. You should read the assignment first, feel free to think about it, but do not actually start coding. Also, please read pair programming guidelines before your first meeting.

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 & Zebra! would be transformed to the ciphertext message Bdn & Cheud!. Note that letters near the end of the alphabet wrap around to the beginning (e.g., Y + 3 = B), uppercase and lowercase is maintained, non-alphabet symbols are fixed. You can, of course, perform a Caesar cipher transformation using other values than 3 for the shift. Yak & Zebra! with a shift of 4, for example, becomes Ceo & 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.

Your task

Write a program that:

Your caesar function should look like this:

def caesar(plaintext, shift): """ Returns ciphertext obtained by transforming every letter in the plaintext by a Caesar cipher by the specified shift. Uppercase and lowercase should be maintained. 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$ python3 caesar.py
What is your plaintext? follow the elk
What shift do you want to use? 3
Your ciphertext is: iroorz wkh hon
prompt$

A little help

Optional extension

Suppose you have written a program called something.py, and you type python3 something.py pig cow goose in the terminal window. In this situation, something.py, pig, cow, and goose are all referred to as command-line arguments of the program something.py. Before the Python interpreter starts executing your program, it creates a list called sys.argv to contain the command-line arguments. Experiment with allowing the user to specify the shift value as an optional command-line argument.

Grading

Please name your file caesar.py. This makes it a little easier for the grader.

Start early, have fun, and discuss questions on Moodle.