Question: The constructor for the CaesarCipher class in Code Fragment 5.11 can be implemented with a two-line body by building the forward and backward strings using
The constructor for the CaesarCipher class in Code Fragment 5.11 can be implemented with a two-line body by building the forward and backward strings using a combination of the join method and an appropriate comprehension syntax. Give such an implementation.

1 class CaesarCipher: "'"' Class for doing encryption and decryption using a Caesar cipher.""" 3 def --init.-(self, shift): """ Construct Caesar cipher using given integer shift for rotation." encoder = [None] * 26 decoder = [None] * 26 for k in range(26): encoder[k] = chr((k + shift) % 26 + ord('A')) decoder[k] = chr((k shift) % 26 + ord('A')) self._forward =!' join(encoder) self. backward = " join(decoder) 4 # temp array for encryption # temp array for decryption 10 %3D # will store as string # since fixed 11 12 13 def encrypt(self, message): """ Return string representing encripted message." return self.transform(message, self.-forward) 14 15 16 17 def decrypt(self, secret): """ Return decrypted message given encrypted secret." return self._transform(secret, self._backward) 18 19 20 21 def _transform(self, original, code): """ Utility to perform transformation based on given code string." msg = list(original) for k in range(len(msg)): if msg[k].isupper(): j = ord(msg|k]) ord('A') msg[k] = codelj] return ' ' join(msg) 22 23 24 25 26 # index from 0 to 25 # replace this character 27 28 29 30 31 if --name-- '-_main__': cipher = CaesarCipher(3) 32 33 message = "THE EAGLE IS IN PLAY; MEET AT JOE'S." coded = cipher.encrypt(message) print('Secret: ', coded) 34 35 36 cipher.decrypt(coded) answer = print('Message:', answer) 37
Step by Step Solution
3.49 Rating (162 Votes )
There are 3 Steps involved in it
Review the list comprehension syntax ... View full answer
Get step-by-step solutions from verified subject matter experts
