Question
) encryptCaesarCipher(plainText, key) - return cipherText. Write a function to perform encryption using the Caesar cipher. The first few lines of this function need to
) encryptCaesarCipher(plainText, key) - return cipherText. Write a function to perform encryption using the Caesar cipher. The first few lines of this function need to create your alphabet using the helper function createAlphabet() you created earlier.
You will have to use a for loop to go through the characters of your plaintext and for each character you will have to choose the character from the alphabet that is key positions forward in the alphabet. Be very careful for the cases where you will have to cycle around the ends of the alphabet. (Hint: You can use your getCharacterForward(char, key) from the previous lab.)
If the key is negative, you will have to choose the character from the alphabet that is key positions backward in the alphabet. Be very careful for the cases where you will have to cycle around the ends of the alphabet. (Hint: You can use your getCharacterBackward(char, key) from the previous lab.)
Do not hard-code any values in your code, use functions instead - we will be testing your code with the alphabets of different lengths.
import string def createAlphabet(): createAlphabet returns a string that includes all lowercase letters, followed by all upper-case letters, then a space, a comma, a period, a hyphen('-'), a tilde('~'), and a pound symbol ('#'). NOTE: the order of characters in the alphabet is very important! In other words, the string being returned will be 'abcdef...XYZABCDEF...XYZ ,.-~#' (the ellipses ... hide the alphabet letters) lc = string.ascii_lowercase uc = string.ascii_uppercase return lc + uc + " ,.-~#" def getCharacterForward(char, key): Given a character char, and an integer key, the function shifts char forward `key` steps. Return the new character. If 'char' is not a single character, return "None. If 'key' is not an integer, return -1. if len(char) != 1: return None if type (key) != int: return -1 whole_chars = createAlphabet() char_index = -1 for i in range(0, len(whole_chars)): if whole_chars[i] == char: char_index = i if char_index != -1: index_final_char = (char_index + key + len(whole_chars)) % len(whole_chars) return whole_chars[index_final_char] def getCharacterBackward(char, key): Given a character char, and an integer key, the function shifts char backward `key steps. Return the new character. If 'char' is not a single character, return "None. If 'key' is not an integer, return -1. if len(char) != 1: return None if type(key) != int: return -1 whole_chars = createAlphabet() for i in range(0, len(createAlphabet())): if whole_chars[i] == char: return whole_chars[(i-key) % len(whole_chars)] import string def createAlphabet(): createAlphabet returns a string that includes all lowercase letters, followed by all upper-case letters, then a space, a comma, a period, a hyphen('-'), a tilde('~'), and a pound symbol ('#'). NOTE: the order of characters in the alphabet is very important! In other words, the string being returned will be 'abcdef...XYZABCDEF...XYZ ,.-~#' (the ellipses ... hide the alphabet letters) lc = string.ascii_lowercase uc = string.ascii_uppercase return lc + uc + " ,.-~#" def getCharacterForward(char, key): Given a character char, and an integer key, the function shifts char forward `key` steps. Return the new character. If 'char' is not a single character, return "None. If 'key' is not an integer, return -1. if len(char) != 1: return None if type (key) != int: return -1 whole_chars = createAlphabet() char_index = -1 for i in range(0, len(whole_chars)): if whole_chars[i] == char: char_index = i if char_index != -1: index_final_char = (char_index + key + len(whole_chars)) % len(whole_chars) return whole_chars[index_final_char] def getCharacterBackward(char, key): Given a character char, and an integer key, the function shifts char backward `key steps. Return the new character. If 'char' is not a single character, return "None. If 'key' is not an integer, return -1. if len(char) != 1: return None if type(key) != int: return -1 whole_chars = createAlphabet() for i in range(0, len(createAlphabet())): if whole_chars[i] == char: return whole_chars[(i-key) % len(whole_chars)]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