Double-Caesar Cipher, Redux

Table of Contents

This assignment is to be done individually. You can talk to other people in the class, me (Dave), the prefect, and lab assistants for ideas and to gain assistance. You can help each other debug programs, if you wish. The code that you write should be your own, however, and you shouldn't hand a printout of your program to others. See the course syllabus for more details or just ask me if I can clarify.

We will use anonymous grading on Moodle, which means that the grader won't see your name until after the grading is done. This is an easy way to help add an extra element of fairness to the grading. Therefore, make sure your name doesn't appear on your actual submission. When you submit via Moodle, it will know you are. Thanks!

1 Overview

One of the unsatisfying things (from a software design perspective) of the last cipher assignment is that you had to break up all your code into multiple programs. It seems that encoding and decoding should all be part of the same program, where each task is separated into its own appropriate subsection. That's what functions in Python are for, and here you'll redo the previous assignment but create your own functions.

2 Your assignment

In your cipher directory from your previous assignment, create a program called cipher.py. Write three functions within:

  • encode(text,key): This function takes a string of text to be encoded as well as a text key, and returns the string in encoded form. You may assume that every character is a lower case letter. This function should not print anything.
  • decode(text,key): This function takes a string of encoded text and a text key, and returns the string in decoded form. This function should not print anything.
  • main(): This function drives the entire program. It should first ask the user if they want to encrypt or decrypt; if the user enters an e, it will encrypt; whereas a d will decrypt. The program will then take a message and a key, and then display the result.

All code in your cipher.py file should exist inside of a function, with the exception of a single line at the bottom that calls the function main().

Here are some sample runs.

Do you wish to (e)ncrypt or (d)ecrypt? e
What is the text? helloyou
What it the key? dog
The resulting text is ksroceri
Do you wish to (e)ncrypt or (d)ecrypt? d
What is the text? ksroceri
What it the key? dog
The resulting text is helloyou

As with the last assigngment, you'll be faster at testing it (and we will test it this way) by redirecting from an input file. In other words, we will create a file that looks like

e
helloyou
dog

If that file happened to be named testinput1.txt, we would run your program as follows:

$ python3 cipher.py < testinput1.txt

3 The last point

If you complete the above successfully (and have good style in your code), you will receive nearly all the points for the assignment. If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the last point, continue reading this section.

First look at this addsubtract.py program, which is a simplified version of the cipher.py program that you've created. Like cipher.py, you can run it as follows:

$ python3 addsubtract.py

Then look at this test_addsubtract.py program, which runs a number of unit tests against the first program. The idea here is that if you have a bug in your main program, the unit tests will catch that. You can run the tests as follows:

$ python3 test_addsubtract.py

Update your cipher.py to mimic the above, where you are able to create a second file called test_cipher.py. When you invoke…

$ python3 cipher.py

… it should run exactly as described above. When you invoke…

# python3 test_cipher.py

… it should run a series of tests to see if the code is working correctly.

4 Style

You should make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a comment at the top explaining what the program does, and use comments above consecutive portions of Python code explaining what they do. You should also use meaningful and readable variable names. Furthermore, your code should be minimal where reasonable. In other words, your code shouldn't contain an excessive number of variables or lines of code, unless doing so adds to readability.

5 Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available in the evenings in CMC 102 and CMC 306 to help out if you need it, and you can attend prefect sessions as well.

If you want to feel super powerful: this assignment is anonymous, but if you use your program to encode your name with a key that you don't give us, you can put your encoded name in a comment and not violate the anonymity requirement. There's no reason or requirement to do this, but you might enjoy doing so.

Author: Dave Musicant

Emacs 24.5.1 (Org mode 8.2.10)

Validate