As usual, create two directories for today's class. Create a
directory called lecture25
under activities, and
a directory called lab25
under labs.
For the following function header, write some tests cases that you
could execute on that code. Write these test cases in a file
called base_test_cases.py
in your lecture25
directory.
#Pre: to_convert is an integer >= 0, the number to have its base # converted. # base is an integer in the range [1,36], the base to convert # to_convert to. #Post: Returns a string, the base 'base' encoding of the to_convert # integer. def convert_base(to_convert, base)
Once you have written your test cases, download
the base_conversion
module in the same directory.
You can use this file in your base_test_cases.py file by
typing import base_conversion
at the top of your file.
Remember, since this is a module you have to reference the function
by specifying which module the function is coming
from: base_conversion.convert_base(...)
.
Up to now, we have restricted ourselves to code we have written must
be contained in one file. So, for example, to use your rot13 code
from the last lab, you would copy/paste the functions into your
current file. You have seen that it is possible to include
code from other locations; This is exactly what
the random
and turtle
modules allow us to
accomplish. Today, I'll show you how you can include your code from
other files, to avoid having to copy/paste.
Execute the test cases you wrote in Activity 1 on the code you wrote last Wednesday for your base conversion program. Did you discover any problems?
On Monday, you wrote rot13, a variant of the Caesar Cipher. Today, I'll show you how easy it is to break the Caesar Cipher, and what makes it easy to break.
Another common technique for encryption is known as the Substitution Cipher. Instead of defining a linear equation between the plaintext and ciphertext, you can define a map that translates characters using a specified order. This is exactly what the Cryptogram puzzle I showed day 1 uses.
The key in the substitution cipher is a string of characters in the range [a, z]. For example, the string "abcdefghijklmnopqrstuvwxyz" is one possible key in the substitution cipher. It maps the letter 'a' to the letter 'a', the letter 'b' to 'b', and so on. A more interesting example would be "zyxwvutsrqponmlkjihgfedcba", which maps 'a' to 'z', 'b' to 'y', and so on. For example, "hello world" would become "svool dliow" under this key. Notice that characters that are not alphabetic are not encoded, and their normal value is included.
In a file called ciphers.py
, define a function
called encode_substitution
. This function takes two
parameters: the plaintext as a string of lowercase characters, and
a key as a string of lowercase characters. Your function will
return a string of lowercase characters, which is the plaintext
with each character substituted with its corresponding letter.
Before you write the body of the encode_substitution
function, define another file called test_ciphers.py
,
which will define all of the test cases you will use to validate the
function you will ultimately write. Make sure you think about what
special cases your code might need to handle. You should also
specify your expected output NOW, so you know
whether your code ultimately works.
Write your encode_substitution
function, then execute
your test cases. Make sure you correct any issues, instead of
commenting the issues away!
When you have finished, create a tar file of your lab25
directory. To create a tar file, execute the following commands:
cd ~/cs120/labs tar czvf lab25.tgz lab25/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 25
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your files.
Do not forget to email your partner today's files!