Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PA 4. Caesar Cipher Topics 1. Data Collections: Lists, Tuples, Arrays, Dictionaries, and Sets 2. Strings 3. Exceptions 4. Files 5. Functions Instructions (You Must

PA 4. Caesar Cipher Topics 1. Data Collections: Lists, Tuples, Arrays, Dictionaries, and Sets 2. Strings 3. Exceptions 4. Files 5. Functions Instructions (You Must Read Everything!!!) Caesar Cipher Objective: practicing with data collections (lists, tuples, arrays, sets, dictionaries), strings, exceptions, and files Description In 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. Dis the fourth letter in the

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. Dis 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. 50 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

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', '8', etc.) hce is the index of a letter in the alphabet for char in texti if char in alphabet: i alphabet. Index(char) letter - alphabet (1. 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

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.bct) and writing (plain.txt), then the output is slightly different, ciphertext is printed before plaintext: ce Ciphertext KHOOR! KRZ OUH BRX) Plaintext: HELLO: HOW ARE YOU? Would you like to encode or decode the message? Type E to encode, to decode, or 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! Or The 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 Approaches Please read

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

amests te the neareit amt Q. Whave sr ovar ketr

Answered: 1 week ago