Question
You must write a file named build map.py; it deals with building and checking maps for the substitution cipher. It must contain (at least) two
You must write a file named build map.py; it deals with building and checking maps for the substitution cipher. It must contain (at least) two functions:build map() and is valid map().
You must write a file named subs cipher.py; it actually does the encoding and decoding of the substitution sipher. It must contain (at least) two functions:encode() and decode().
You must write a file named unzip.py; it deals with decoding a compressed data stream. It must contain (at least) one function: unzip().
Required Function: build map()
buildmap() must take a single parameter, which is the location of a file. 1 You must open the file and build a substitution map from it; return the map.
The file format will be as described earlier in this file; as with the previous assignment, you must ignore any blank lines, or lines that contain comments.
You may assume that the file exists; you do not have to do any error checking to handle a situation where the file cant be opened.
7.1 asserts Your function must use asserts to make sure that each of the following is true, for every line (except blank lines and comments):
The line has exactly two words (we dont care about how much whitespace, or where) Both words on the line must be a single character The same from character in the map must not be used twice.
Note that there are many other things that might be interesting to check, such as confirming that the to characters are not duplicated. Dont do these checks in this function; tolerate silly maps.
8 Required Function: is valid map()
is valid map() does some more extensive error checking - more than is done bybuild map(). It is designed either to be used as a second step (sanity-checking something that build map() returned), or as checking a map that was build by hand, using some other process.
This function takes a single parameter (the map), and returns either True orFalse; it only returns True if all of the requirements are met. This function should never assert about anything; instead, return False if you find a problem.
This function must confirm that:
Every from character (the keys) and every to character (the values) are strings.
Also, every such string is exactly one character long. Also, none of those strings are the space, tab, or newline character.
HINT: What does this code do?
if c in " \t ":
There are no duplicate to characters.
The set of from characters is exactly the same as the set of to characters.
Required Function: encode()
encode(), in subs cipher.py, must encode a string. It takes two parameters: the substitution map, and the input string. It must return the cipher string.
9.1 asserts
This function must call is valid map() to check that the map is reasonable, and must assert that the function returns True.
You must not implement is valid map() in this file! Instead, you mustimport the build map.py file, and call the function that is defined in there.
10 Required Function: decode() decode(), in subs cipher.py, must decode a string. It takes works exactly like encode(), except that it must reverse the mapping.
Required Function: unzip()
unzip() takes a compressed stream as its only parameter, and returns the uncompressed string.
The compressed stream is encoded as described earlier in this spec; the stream is a list, and each element is either a string (with a single character), or a tuple with two integers.
This function must assert that:
-
Every element in the compressed stream is the proper type (string or tuple)
-
Every string is exactly one character long
-
Every tuple has exactly two elements, both of which are positive integers
-
The offset (the first element in the tuple) does not point back any further than the start of the uncompressed string.
Sample File:
a b b c c d d e e f f g g h h i i j j k k l l m m n n o o p p q q r r s s t t u u v v w w x x y y z z a
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started