Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program must be done in python, cannot use ord(), chr(), or key(), also cannot use imports. Assignment Overview The Caesar cipher is named after Julius
Program must be done in python, cannot use ord(), chr(), or key(), also cannot use imports.
Assignment Overview The Caesar cipher is named after Julius Caesar who used this type of encryption to keep his military communications secret. A Caesar cipher replaces each plain-text letter with one that is a fixed number of places down (or up) the alphabet. The plain-text is your original message; the cipher-text is the encrypted message. The example shown below is a shift of three so that B in the plain-text becomes E in the cipher-text, a C becomes F, and so on. The mapping wraps around so that X maps to "A" and so on. XYZ ABCDEF A|B|C|VTJOHN Here is the complete mapping for a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC To encrypt a message simply substitute the plain-text letters with the corresponding cipher-text letter. For example, here is an encryption of the quick brown fox jumps over the lazy dog using our shift- three cipher (case is ignored): Plaintext: the quick brown fox jumps over the lazy dog Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ To decrypt the message simply reverse the process. The encryption can also be represented using modular arithmetic after first transforming the letters into numbers according to the scheme: A = 0, B = 1, etc. (which is the index of the alphabet if it is in a string or a list). A shift-three cipher will take the number of each letter (plaintextchar), add 3 (the shift), and then find the remainder after dividing by 26 to get the cipherText: cipherTextChar = (plaintextChar + 3) % 26 Your high level algorithm will be: 1. Input the cipher-text. 2. Find the most common character. 3. Find the shift from E to that most common character. 4. Use the shift to decode each character of the cipher-text and print the resulting plain-text. (Hint: stop here for a simple version to start with.) 5. If the plain-text isn't readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable. Your program must also meet the following specifications: 1. You must have and use at least these four functionsmore are fine. A proj04.py file with function stubs is provided. a. def decode_char (ch, shift) char i. The parameters are ch, a string character, and shift, an int. This function returns the shifted decoding of character ch, i.e. decode ch. If the shift is -3 and the cipher-text character is H, this function will return the plain-text character E. If the parameter ch is not a letter, simply return it (do not try to decode it) ii. Parameters: ch, int iii. Returns: ch iv. Display: nothing b. def get_shift(s, ignore) int, char i. The parameters s and ignore are both strings. Return the shift (i.e., the decode key) for string s, and the most common character (if more than one are equally common, return the first letter that occurs in the alphabet). If the text is long enough, we expect the most common character to be "E" so shift would be the shift to get "E" (hint: what is the difference between the position of the most common character and the position of E in the alphabet). For example, if the most common character in the cipher-text is H, the shift will be -3, the number of characters to get to E. In that case you would return -3 and H. The ignore parameter is the string of cipher-text letters to ignorethey have been tried but didn't result in readable English text. The ignore parameter allows you to find the next-most-common character by ignoring more common characters. Hint: You may set ignore = "" when testing the function with the function test case below. ii. Parameters: str, str iii. Returns: int, char iv. Display: nothing c. def output_plaintext (s, shift) i. The parameters are s, a string, which is the cipher-text and shift, an int. Output plain-text of the cipher-text using the key, i.e. using the shift. Print the output in upper case. You must use your decode_char function to decode each character of s. Begin your output with a blank line. ii. Parameters: str, int iii. Returns: nothing iv. Display: str d. def main() The main program that calls the other functionsdon't forget the call to main, i.e. main() - it is already called for you at the bottom of the skeleton code. Having main allows us to more easily create a set of test programs to test the other functions because we need to comment out main for those tests (this is what Mimir does when doing a function specific test case). The main function must do the following: i. Prompt for an input cipher-text string. ii. Get a key (a.k.a. shift) for the cipher-text string iii. Display the plaintext. iv. Ask the user if the plaintext is readable English (yeso) 1. If the response is not no, print a message (check the string.txt file) and end the program. 2. If the response is no, add the key letter to the ignore string and go back to step ii to get a new key for the string Notes and Hints: 1. To clarify the project specifications, sample output is appended to the end of this document. 2. Items 1-9 of the Coding Standard will be enforced for this projectnote the change to include more items. 3. In main, your prompt for yeso must accept any case for the characters, e.g. yEs is acceptable as "yes". 4. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 5. One way to decrypt is to use the two functions that are inverses of each other: ord() and chr(). These work because our letters are based on the ASCII subset of Unicode. However, you are forbidden to use ord() and chr() in this assignmentit is poor coding to be dependent on ASCII. Using those functions will result in a zero for the assignment. 6. You do not need to check for any input errors. 7. Do not hard code your solutionsthe result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution. 8. You may not use advanced data structures such as lists, dictionaries, sets or classes in solving this problem. 9. As this is your first program with functions, do not forget function headers! Assignment Overview The Caesar cipher is named after Julius Caesar who used this type of encryption to keep his military communications secret. A Caesar cipher replaces each plain-text letter with one that is a fixed number of places down (or up) the alphabet. The plain-text is your original message; the cipher-text is the encrypted message. The example shown below is a shift of three so that B in the plain-text becomes E in the cipher-text, a C becomes F, and so on. The mapping wraps around so that X maps to "A" and so on. XYZ ABCDEF A|B|C|VTJOHN Here is the complete mapping for a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC To encrypt a message simply substitute the plain-text letters with the corresponding cipher-text letter. For example, here is an encryption of the quick brown fox jumps over the lazy dog using our shift- three cipher (case is ignored): Plaintext: the quick brown fox jumps over the lazy dog Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ To decrypt the message simply reverse the process. The encryption can also be represented using modular arithmetic after first transforming the letters into numbers according to the scheme: A = 0, B = 1, etc. (which is the index of the alphabet if it is in a string or a list). A shift-three cipher will take the number of each letter (plaintextchar), add 3 (the shift), and then find the remainder after dividing by 26 to get the cipherText: cipherTextChar = (plaintextChar + 3) % 26 Your high level algorithm will be: 1. Input the cipher-text. 2. Find the most common character. 3. Find the shift from E to that most common character. 4. Use the shift to decode each character of the cipher-text and print the resulting plain-text. (Hint: stop here for a simple version to start with.) 5. If the plain-text isn't readable English, try the next-most common character and continue checking successive next-most-common characters until the resulting plain-text is readable. Your program must also meet the following specifications: 1. You must have and use at least these four functionsmore are fine. A proj04.py file with function stubs is provided. a. def decode_char (ch, shift) char i. The parameters are ch, a string character, and shift, an int. This function returns the shifted decoding of character ch, i.e. decode ch. If the shift is -3 and the cipher-text character is H, this function will return the plain-text character E. If the parameter ch is not a letter, simply return it (do not try to decode it) ii. Parameters: ch, int iii. Returns: ch iv. Display: nothing b. def get_shift(s, ignore) int, char i. The parameters s and ignore are both strings. Return the shift (i.e., the decode key) for string s, and the most common character (if more than one are equally common, return the first letter that occurs in the alphabet). If the text is long enough, we expect the most common character to be "E" so shift would be the shift to get "E" (hint: what is the difference between the position of the most common character and the position of E in the alphabet). For example, if the most common character in the cipher-text is H, the shift will be -3, the number of characters to get to E. In that case you would return -3 and H. The ignore parameter is the string of cipher-text letters to ignorethey have been tried but didn't result in readable English text. The ignore parameter allows you to find the next-most-common character by ignoring more common characters. Hint: You may set ignore = "" when testing the function with the function test case below. ii. Parameters: str, str iii. Returns: int, char iv. Display: nothing c. def output_plaintext (s, shift) i. The parameters are s, a string, which is the cipher-text and shift, an int. Output plain-text of the cipher-text using the key, i.e. using the shift. Print the output in upper case. You must use your decode_char function to decode each character of s. Begin your output with a blank line. ii. Parameters: str, int iii. Returns: nothing iv. Display: str d. def main() The main program that calls the other functionsdon't forget the call to main, i.e. main() - it is already called for you at the bottom of the skeleton code. Having main allows us to more easily create a set of test programs to test the other functions because we need to comment out main for those tests (this is what Mimir does when doing a function specific test case). The main function must do the following: i. Prompt for an input cipher-text string. ii. Get a key (a.k.a. shift) for the cipher-text string iii. Display the plaintext. iv. Ask the user if the plaintext is readable English (yeso) 1. If the response is not no, print a message (check the string.txt file) and end the program. 2. If the response is no, add the key letter to the ignore string and go back to step ii to get a new key for the string Notes and Hints: 1. To clarify the project specifications, sample output is appended to the end of this document. 2. Items 1-9 of the Coding Standard will be enforced for this projectnote the change to include more items. 3. In main, your prompt for yeso must accept any case for the characters, e.g. yEs is acceptable as "yes". 4. You can test functions separatelythat can be a huge advantage in developing correct code faster! If you cannot figure out how to do that, ask your TA for guidance. 5. One way to decrypt is to use the two functions that are inverses of each other: ord() and chr(). These work because our letters are based on the ASCII subset of Unicode. However, you are forbidden to use ord() and chr() in this assignmentit is poor coding to be dependent on ASCII. Using those functions will result in a zero for the assignment. 6. You do not need to check for any input errors. 7. Do not hard code your solutionsthe result is a zero. Hard coding means that your program for the specific tests rather than having a generic solution. 8. You may not use advanced data structures such as lists, dictionaries, sets or classes in solving this problem. 9. As this is your first program with functions, do not forget function headersStep 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