The constructor for the CaesarCipher class in Code Fragment 5.11 can be implemented with a two-line body
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 a combination of the join method and an appropriate comprehension syntax. Give such an implementation.
Transcribed Image Text:
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
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 69% (13 reviews)
Review the list comprehension syntax ...View the full answer
Answered By
Charles mwangi
I am a postgraduate in chemistry (Industrial chemistry with management),with writing experience for more than 3 years.I have specialized in content development,questions,term papers and assignments.Majoring in chemistry,information science,management,human resource management,accounting,business law,marketing,psychology,excl expert ,education and engineering.I have tutored in other different platforms where my DNA includes three key aspects i.e,quality papers,timely and free from any academic malpractices.I frequently engage clients in each and every step to ensure quality service delivery.This is to ensure sustainability of the tutoring aspects as well as the credibility of the platform.
4.30+
2+ Reviews
10+ Question Solved
Related Book For
Data Structures and Algorithms in Python
ISBN: 978-1118290279
1st edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Question Posted:
Students also viewed these Computer science questions
-
a. Carlys Catering provides meals for parties and special events. You created an Event class for the company. The Event class contains two public final static fields that hold the price per guest...
-
a. Sammys Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. You created a Rental class for the company. The Rental class contains two public...
-
Create a TaxReturn class with fields that hold a taxpayers Social Security number, last name, first name, street address, city, state, zip code, annual income, marital status, and tax liability....
-
Write a nonrecursive function that takes the first Node in a linked list as an argument and reverses the list, returning the first Node in the result.
-
A potential project has an initial capital investment of $100,000. Net annual revenues minus expenses are estimated to be $40,000 (A$) in the first year and to increase at the rate of 6.48% per year....
-
Hawthorn and Privet have carried on business in partnership for a number of years, sharing profits in the ratio of 4 : 3 after charging interest on capital at 4 per cent per annum. Holly was admitted...
-
How is a service department charge rate determined?
-
The Thiel Company reports the following deferred tax items at the end of 2007: Required Show how the preceding deferred tax items are reported on the Thiel Companys December 31, 2007 balancesheet....
-
Review the potential issues involving funding requirements and revenue streams from very different businesses - such as the fixed investments in the meat business in South America and the dairy...
-
The article "Should You Report That Fender- Bender?" (Consumer Reports, Sept. 2013: 15) reported that 7 in 10 auto accidents involve a single vehicle (the article recommended always reporting to the...
-
Our DynamicArray class, as given in Code Fragment 5.3, does not support use of negative indices with _ _getitem_ _. Update that method to better match the semantics of a Python list. Code Fragment...
-
Answer the previous question for the case when T is a proper binary tree with more than one node.
-
a. Over what output range does Sues Surfboards enjoy the benefits of increased specialization and division of labor? b. Over what output range does the firm experience diminishing marginal product of...
-
You are reading an analyst reports that claims that banks collectively are cheap, because they are trading at 0.80 times book value of equity. You believe that the truth is that banks are perceived...
-
Lola Metals has two decentralized divisions, Stamping and Finishing. Finishing always has purchased certain units from Stamping at $54 per unit. Stamping plans to raise the price to $66 per unit, the...
-
Consider design features for unmanned aircraft systems (UAS) or optionally piloted aircraft (OPA) since they have limited design airworthiness regulations for these aircraft configurations. Pick a...
-
Explain the following code in detail; using System; class Program { static void Main() { Console.Write("Enter the number of times to roll the dice: "); int numberOfRolls =...
-
Consider a mid-sized suburban locale where the market for modified electronic cigarettes, mod e-cigs, is described by the following demand and supply equations: Q D = 448 - 3.5P , and Q s = 2.5P -...
-
Define the followings: 1. Edge effect 2. Riparian zone 3. Black grass bugs 4. Coyote getter
-
What is a content filter? Where is it placed in the network to gain the best result for the organization?
-
In what order are positions visited during a postorder traversal of the tree of Figure 8.6? 3 3 5 4
-
Justify Table 8.2, summarizing the running time of the methods of a tree represented with a linked structure, by providing, for each method, a description of its implementation, and an analysis of...
-
Draw an arithmetic expression tree that has four external nodes, storing the numbers 1, 5, 6, and 7 (with each number stored in a distinct external node, but not necessarily in this order), and has...
-
Redwood Industries has EBIT of $450 million, interest expense of $175 million, and a corporate tax rate of 35%. The total of Rosewood's net income and interest payments is closest to:
-
Jayden is evaluating purchasing a sandwich shop from his friend. Currently, the shop sells sandwiches for $10 each with a variable cost of $3 per sandwich. The fixed costs for the shop are $1,400....
-
To create a sample program personnel budget and to evaluate your budget. Scenario: You are the director of a small early childhood program, forecasting for your upcoming year. Your program will have...
Study smarter with the SolutionInn App