CS 111: Introduction to Computer Science
Winter 2017
HW04: Caesar cipher
Due: Wednesday, 01/18 at 22:00
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:
- Asks the user for an integer (the Caesar shift value) and a string (the plaintext),
- Calls a function named
caesar
(see below for details) to obtain the ciphertext, and - Prints the ciphertext.
Your caesar
function should look like this:
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
- To step one character at a time through a string, see the strings2.py sample from an in-class lab.
- There are several ways to shift a letter to another letter by some
integer amount. One way is to use the Python functions
ord
andchr
, which are described in Section 5.4.1 of your textbook. - Think about edge cases. The specification above specifies that the user should input an integer. Make sure your program does something sensible for any possible integer input, not just numbers like 3.
Optional extension
Suppose you have written a program calledsomething.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 filecaesar.py
. This makes it a little easier for the grader.
- Style/comments [3 points]
- All the style guidelines from previous assignments.
- Have a comment that specifies what the
caesar
function does. You may simply copy the one I provided above. In the future, when you write your own functions, always write a (short) comment describing the function. - Make sure your
caesar
function matches the description in the assignment: not changing the spelling or even the order of the arguments (plaintext, shift
). This allows others to use your function as long as they know the "specification" of the function (pass in a plaintext and a shift, get back a cyphertext) without caring about its "implementation" (the code that you write to create the resulting cyphertext). - Try not to have mysterious "magic" numbers appearing in your program.
I may understand what
26
is, but I may be confused as to what97
is, for example.
caesar
function [5 points]- Correctly performs the Caesar cypher for a variety of test cases.
main
function [2 points]- The rest of your code should go in a
main
function, which prompts the user for input and callscaesar
. - The last line of your file should be
main()
to run themain
function.
- The rest of your code should go in a
Start early, have fun, and discuss questions on Moodle.