Learning Goal: I’m working on a python project and need an explanation and answer to help me learn.Caesar CipherTopicsData Collections: Lists, Tuples, Arrays, Dictionaries, and Sets
Strings
Exceptions
Files
Functions
Instructions (You Must Read Everything!!!)Caesar CipherObjective: practicing with data collections (lists, tuples, arrays, sets, dictionaries), strings, exceptions, and filesDescriptionIn this assignment you can implement a simple encoder-decoder program that uses a Caesar cipher. A Caesar cipher is based on shifting a code for each letter by some number. For example, let’s choose a shift that equals to 3 and a letter from the English alphabet that is A, then the encoded symbol is a letter that follows A and is located at the position that is shifted from A by 3, so it is D. D is the fourth letter in the alphabet, whereas A is the first letter, and the distance between them is 3. In this encoding we use the right shift, so A becomes D, B becomes E, C becomes F, and so on. It is also easy to decode the cipher because we can use the left shift to reverse the encoding, so F becomes C, E becomes B, and D becomes A. The only problem is how to encode the last letters in the alphabet because they are not followed by any other letters. This problem can be easily solved if we assume that the letters in the alphabet are arranged in a circle, so Z is followed by A. Then using the right shift 3, letter Z becomes C, letter Y becomes B, and letter X becomes A.To achieve this functionality, we can use a circular array. A circular array is a sequence of items (that can be implemented as an array, list, or string) where items are accessed by an index that is calculated using the modulo operation. In your code, you can use the following implementation where alphabet is a sequence of uppercase letters (it can be implemented as a list, tuple, array, or string):# alphabet = (‘A’, ‘B’, etc.)# i is the index of a letter in the alphabetfor char in text: if char in alphabet: i = alphabet.index(char) letter = alphabet[(i + shift) % len(alphabet)]NOTE: For simplicity of coding, ALL LETTERS in text should be converted into uppercase!!! Also, encode and decode ONLY LETTERS, not other characters such as special symbols, punctuation marks, and digits!!!Here are the snippets of the program output, note that your program output should match this format exactly. In the beginning your program should ask the user to choose from the menu one of the following operations: encode, decode, or quit in the following way:Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit:If the user types ‘e’/’E’ or ‘d’/’D’, then the program asks the user to enter a filename for reading:Please enter a file for reading:Assume that the user has already created a file named message.txt that has the following content “Hello! How are you?”, and the file is located in the same directory as the main program cipher.py. When the user types the filename message.txt and hits ‘Enter’, the program asks the user to enter a new filename for writing:Please enter a file for writing:Assume that the user enters the filename code.txt. When the user enters the filename, the program produces the following output:Plaintext:HELLO! HOW ARE YOU?Ciphertext:KHOOR! KRZ DUH BRX?Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit:Please note that the program reprints the same prompt after writing plaintext and ciphertext. Also, it writes the ciphertext to the file previously chosen by the user. You should check the content of the selected file code.txt.If the user previously selected ‘d’ and entered files for reading (code.txt) and writing (plain.txt), then the output is slightly different, ciphertext is printed before plaintext:Ciphertext:KHOOR! KRZ DUH BRX?Plaintext:HELLO! HOW ARE YOU?Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit:The program also writes the plaintext to the file previously chosen by the user. You should check the content of the selected file plain.txt. If the user enters ‘q’ from the menu, then the program prints the following message and terminates:Goodbye!If the user does not enter a valid input (a file name) the program writes a message that notifies about the wrong input:The selected file cannot be open for reading!OrThe selected file cannot be open for writing!Remember that first you have to create a text file (for example, message.txt) that will be used for reading. The file should be located in the same directory as the main program cipher.py. A file that will be used for writing (for example, code.txt or plain.txt) does not have to be created in advance, it can be created when you open it for writing.Programming ApproachesPlease, read the following code carefully. You can copy it to the Python IDLE or other editor and add the missing functions and the main program. Some functions are already implemented. As always write a header in the beginning of the program:# assignment: programming assignment 4# author: (put your full name here)# date: (put the date you finished working on the program)# file: cipher.py is a program that (put the description of the program)# input: (put input description)# output: (put output description)# read text from a file and return text as a list of strings# the function should contain an input statement, open file statement,# and try-except statementdef readfile(): write your code here return message# write a list of strings (message) to a file# the function should contain an input statement, open file statement,# and try-except statementdef writefile(message): write your code here # DO NOT RETURN ANYTHING!# An optional helper function# make a tuple of letters in the English alphabetdef make_alphabet(): alphabet = () for i in range(26): char = i + 65 alphabet += (chr(char),) #print (alphabet) return alphabet# encode plaintext letter by letter using a Caesar cipher # plaintext is a list of strings# return a list of encoded stringsdef encode(plaintext, shift = 3): return ciphertext# decode ciphertext letter by letter using a Caesar cipher# ciphertext is a list of strings# return a list of decoded stringsdef decode(ciphertext, shift = -3): return plaintext# An optional helper function# convert a list into a string# for example, the list [“A”, “B”, “C”] is converted to the string “ABC” or# the list [“H”, “O”, “W”, ” “, “A”, “R”, “E”, ” “, “Y”, “O”, “U”, “?”] # is converted to the string “HOW ARE YOU?”def to_string(text): s = “” write your code here return s# main program if __name__ == ‘__main__’: write your code hereAfter you implement your cipher.py you can decode the following message:”ZRXOG BRX WHOO PH, SOHDVH, ZKLFK ZDB L RXJKW WR JR IURP KHUH?””WKDW GHSHQGV D JRG GHDO RQ ZKHUH BRX ZDQW WR JHW WR,” VDLG WKH FDW.”L GRQ’W PXFK FDUH ZKHUH -” VDLG DOLFH.”WKHQ LW GRHVQ’W PDWWHU ZKLFK ZDB BRX JR,” VDLG WKH FDW.”- VR ORQJ DV L JHW VRPHZKHUH,” DOLFH DGGHG DV DQ HASODQDWLRQ.”RK, BRX’UH VXUH WR GR WKDW,” VDLG WKH FDW, “LI BRX RQOB ZDON ORQJ HQRXJK.”This message is written in the file secret.txt and is located in Files/Scripts/PA4 folder on Canvas.Testing and Evaluation ScriptsYou can test the program functions encode() and decode() by running the tester-cipher.py file located in Files/Scripts/PA4. Please notice that the input and output of these functions are lists of text lines (a list of strings, not one string!!!)To test you program first run it in the Python shell. If it runs correctly, test it in a terminal (or the bash shell on the Unix server). You need to have all files (cipher.py, eval_pa4, ex1, ex2, ex1.out, ex2.out, message.txt, secret.txt) in one working directory (such as CSE20/PA4). You can download them from Files/Scripts/PA4. Then type in the shell the following commands:python3 cipher.py < ex1python3 cipher.py < ex2All lines of the output should be printed on the separate lines as shown in the files ex1.out and ex2.out. So, do not forget to add ‘n’ at the end of all input statements! After that you can test your program with the evaluation script. The evaluation script and supplementary data files can be downloaded from Files/Scripts/PA4 on Canvas. You can run the evaluation script using the following command:sh eval_pa4The example of the output in the IDLE shell is shown below (the input is the same as in the ex1 file and output is very similar to ex1.out, the only difference is the absence of input data). Remember extra newlines are not shown and should not matter!Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit: ePlease enter a file for reading: message.txtPlease enter a file for writing: code.txtPlaintext:HELLO! HOW ARE YOU?Ciphertext:KHOOR! KRZ DUH BRX?Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit: dPlease enter a file for reading: code.txtPlease enter a file for writing: plain.txtCiphertext:KHOOR! KRZ DUH BRX?Plaintext:HELLO! HOW ARE YOU?Would you like to encode or decode the message?Type E to encode, D to decode, or Q to quit: qGoodbye!Grading RubricPlease use the evaluation script to evaluate your program performance and specifications. The evaluation script and supplementary data files are available on Canvas/Files/Scripts/PA4. Your program will be graded manually; however, the evaluation script helps you to debug and test your program. It is your responsibility to improve and adjust your program in order to receive more points or the full credit.CriteriaPoints 60ExcellentGoodSatisfactoryUnsatisfactorySubmitted on time60 possible pointsyesnoRuns without errors16yesruns without errors for one input8 pointsnoProduces right output16yesminor format mismatch14 pointspartially correct output8 pointsnoHas a comment block5yesminor format mismatch3-4 pointsformat mismatch 1-2 pointsnoSource named correctly3yesnoUses lists2yesnoUses string methods2yesnoUses while loops2yesnoUses if-else or elif2yesnoUses exceptionsIOError or others2yesnoUses files2yesnoUses functions:readfile, writefile, encode, decode84 functions8 points3 functions6 points1-2 functions2-4 pointsno
Requirements: 100
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments