Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You have to write functions to perform reading and writing of messages using file operations and write functions for generating a cipher code and ciphering
You have to write functions to perform reading and writing of messages using file operations and write functions for generating a cipher code and ciphering input messages.
All functions for file operations are to be written in fileiopy and functions for ciphering in cipherpy
Messages to be ciphered and the ciphercode is to be read by your program from input files.
The ciphercode is provided in a text file which will have the following structure:
K: This is just a test
V: Line two is new to m
K: indicates the key of the cipher code while V: indicates the associated value.
In this simple cipher code, each character in the key maps to a corresponding character in value,
where the correspondence is based on sequential ordering.
In the above example, the letter T is mapped to letter Lh to ii to ns to e and so on
You are to write a file operation function readkeyvaluefilename that reads the file with the ciphercode and
generates two lists of singlecharacter strings, one from the key line and another from value line.
You are to write ciphering operations to include the function makedictionaryl l that takes two equallength lists
of singlecharacter strings. It creates and returns a dictionary using the elements of l as keys and the elements of l as values.
Another ciphering operation and corresponding function to write is ciphers d The parameter s is a string and d is the dictionary created by makedictionary
Function ciphers d uses dictionary d to replace characters in s that are keys in d with the corresponding key value.
Characters in s that are not dictionary keys are left unchanged in the ciphered version of the string s
The function is to return the ciphered string.
The string s is to be read from an input file. This should be accomplished by a function readmessagefilename
as part of file operations, which should open the input file and read all lines in the file treating each
line as the input message to be ciphered. Each line read should be used as s in the function ciphers d
Each ciphered message returned by the function ciphers d should be written to an outfile file.
To do so you are to write the function writecipheredmessagescipheredmessage, filename where
cipheredmessage is the string returned by ciphers d and filename is the name of the file where
ciphered messages are written.
All code for file operations is to be implemented in the file fileiopy
All code for ciphering operations is to be implemented in the file cipherpy
You are already provided with the files and template function definitions. Make sure to add comments to your program.
You are also given code with the main program in file capy This includes all function calls and implements the ciphering system.
Note:
Please do not change the code structure.
Usage:
python capy kv m
Example: python capy kv inputciphercodetxt m inputmessagestxt
Please make sure the code runs when you run the above command from promptterminal
All the output files are saved to "output folder
A file with ciphercode ciphercodetxt and a file with input messages messagestxt are
provided for testing: inputciphercodetxt and inputmessagestxt
PS Please do not change: capy requirements.txt
Use K: and V: to identify key and value of
cipher code linestartswithK
Extract word by linesplit and extract
character by for XXX in YYY
Remove K: and V: by using list slicing :
Check list length by using len
For dictionary, values of the same key usually
only the last value would be kept
Use terminal instead of console
If python does not work, try python
Close file after use
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