234128 Introduction to Computing with Python Test: Caesar Cypher
Instructions. You can use any material to solve the test. However, to operate on strings, other than the functions that you implement or that are already defined in the test, you are allowed to use only the following functions: len, range, and enumerate. (This means not using advanced functions on strings, such as: index, count, split, and the like.) Make sure that your solution is clear, coherent, and well organized. The assumptions you make in your solution should be explicitly stated. Your submitted solution should be your work and your work alone. You should submit a single .zip le containing all the les necessary to compile and run your code (as well as instructions on how to do it).
Caesar Cypher
Problem. In cryptography, a Caesar cypher (also known as a shift cypher) is one of the simplest and most widely known encryption techniques. It is named after Julius Caesar, who used it in his private correspondence.
A Cesar cypher is a substitution cypher in which letters are shifted some fixed number of positions down the alphabet, wrapping back to the beginning if necessary. The shift parameter is used as the key. For example, with a shift of 3, the letter A becomes D, the letter B becomes E, and so on until X which becomes A, Y which becomes B, and Z which becomes C. Only those letters which occur in the (upper case) English alphabet are shifted; numbers, symbols, punctuation, whitespace, and all other characters are left unchanged.
For instance, here is a Caesar cypher using a shift of 23 (twenty three) places:
Plain A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Cypher X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
When encrypting, the algorithm looks up each letter of the message in the \plain" line and writes down the corresponding letter in the \cypher" line. In this way, a plaintext becomes a cyphertext.
Plaintext THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Cyphertext QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
To undo a Caesar cipher, the same algorithm is applied shifting in reverse. In the example above, this means shifting with -23 (minus twenty three) places.
Task. Accompanying this test there are les caesar.py, main.py, and README.md. Your task is to complete and to x the code in the les caesar.py and main.py in order to obtain a full working implementation of a Caesar cypher for encoding and decoding messages given a shared key. Morever, you should include a brief description on how to use this code in the le README.md
The code to complete is indicated with the keyword TODO included as a comment and requires for you to: (1) understand what a particular function does in order to write a comment; (2) determine the parameters and/or arguments of a particular function; (3) nish the de nition of a function. The code to x require you to identify and repair syntax errors.
Submission. You must submit the les main.py, caesar.py, and README.md all zipped into one le. Your program must take its input from the command line. The input to your program will be: (1) the key used for encoding/decoding messages; (2) a string which will be either encode (for encode) or decode (for decode) (3) two les which will serve to model the input/output behavior of the algorithm. The rst le will contain the input to which the Caesar cypher is meant to be applied. The second le will contain the result of applying the Caesar cypher to the input.
This means that you should be able to execute your program by using:
python3 main.py \
In any other case, the program should print:
error: incorrect number of arguments for main.py
use: main.py \
It is assumed that: (A) \
For instance, if the le plain.01 contains the line:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Then, executing
python3 main.py 23 encode plain.01 encoded.01
should result in a file encoded.01 with the line:
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Moreover, if the le encoded.01 contains the line:
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Then, executing
python3 main.py 23 decode encoded.01 decoded.01
should result in a le decoded.01 with the line:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG