Write a Python function that wil be called Encrypt1. It\'s input will be a plain
ID: 3848859 • Letter: W
Question
Write a Python function that wil be called Encrypt1. It's input will be a plaintext message plaintext and its output will be the ciphertext after substitution via the chart below. The plaintext letter is BOLDED on the left, and its corresponding row-column value is on the right:
PYTHON: (I recommend storing this at the top of your code as a global varibale named keysquare.
keysquare = {'Q':'AA', 'C':'AD', '3':'AF', 'T':'AG', '6':'AV', 'W':'AX',
'M':'DA', 'O':'DD', 'E':'DF', 'H':'DG', 'N':'DV', 'L':'DX',
'8':'FA', 'A':'FD', '4':'FF', '2':'FG', '1':'FV', 'I':'FX',
'G':'GA', 'B':'GD', '5':'GF', 'Z':'GG', 'R':'GV', '7':'GX',
'S':'VA', 'X':'VD', '9':'VF', 'V':'VG', 'U':'VV', 'P':'VX',
'0':'XA', 'K':'XD', 'J':'XF', 'F':'XG', 'D':'XV', 'Y':'XX'}
Your job for Encrypt1 Python function is to use substitution to create a ciphertext where each letter in the plaintext is represented by its value in the keysquare. Ignore all spaces and punctuation. For example, the input *?H E L L O WORLD 123!!!~# would return DGDFDXDXDDAXDDGVDXXVFVFGFA.
It may have the following Python psuedocode:
INPUT: plaintext
OUTPUT: ciphertext
********************************
def Encrypt1(plaintext):
Initialize the ciphertext to an empty string.
for letter in plaintext:
If the letter is represented in the keysquare (A-Z, 0-9):
Append its corresponding keysquare value to the ciphertext
Return the ciphertext
Explanation / Answer
CODE:
# Encrypt1 function starts here.
def Encrypt1(plaintext):
# dictionary is defined here.
keysquare = {'Q':'AA', 'C':'AD', '3':'AF', 'T':'AG', '6':'AV', 'W':'AX',
'M':'DA', 'O':'DD', 'E':'DF', 'H':'DG', 'N':'DV', 'L':'DX',
'8':'FA', 'A':'FD', '4':'FF', '2':'FG', '1':'FV', 'I':'FX',
'G':'GA', 'B':'GD', '5':'GF', 'Z':'GG', 'R':'GV', '7':'GX',
'S':'VA', 'X':'VD', '9':'VF', 'V':'VG', 'U':'VV', 'P':'VX',
'0':'XA', 'K':'XD', 'J':'XF', 'F':'XG', 'D':'XV', 'Y':'XX'}
# get all the keys defined in dictionary
keyList = keysquare.keys()
# create a list to store the encrypted string.
output = list()
# iterate through the input string
for i in plaintext:
# if each character in string is part of the keys, then
# encrypt the same.
if i in keyList:
temp = keysquare.get(i)
output.append(temp)
# print the outputs.
print 'INPUT: ' + plaintext
print 'OUTPUT: ' + ''.join(output)
Encrypt1("*?H E L L O WORLD 123!!!~#")
OUTPUT:
>python encrypt.py
INPUT: *?H E L L O WORLD 123!!!~#
OUTPUT: DGDFDXDXDDAXDDGVDXXVFVFGAF
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.