In this assignment, we are going to implement a custom list called CauchyList. A CauchyList is...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
In this assignment, we are going to implement a custom list called CauchyList. A CauchyList is a standard Python list with some arithmetic operations associated to it. A description of the CauchyList class is in the following section. Your program is going to be one Python file, called assignment3.py, that contains the CauchyList class. CauchyList class Instance variables • p: an integer that is used as the modulus. This means that all operations are done mod p. • content: a list of integers. Methods • _init_: accepts an integer p. Initializes the instance variable p to p and the instance variable content to an empty list. • generate_random: accepts an integer n. Populates content with n random integers in the range [0, p - • length: returns the length of this CauchyList. 1). • get: accepts an integer i. If i is smaller than the length of the list then returns the i-th element. Otherwise, returns 0. • -_add__: returns the sum of this CauchyList and the input CauchyList. The sum of two CauchyLists is defined component-wise. That means if a, b are two CauchyList then we define c = a + b by setting c.content[i] = a.content[i] + b.content[i] where the addition is done mod p. This is a special method that is called to evaluate expressions of the form a + b. We have seen other special methods such as str_ and _lt__ in the lectures. For example, lt__ is called to evaluate expressions of the form a < b. This method should be able to handle CauchyLists of different lengths. When adding two lists of length m and n, where m > n, the resulting list should be of length m and the higher elements of shorter list is assumed to be zero. Hint: you can use the get method for this. If the two CauchyLists have different values of p, this method should raise a ValueError exception. -_sub__: similar to add_ except that it is called to evaluate expressions of the form a b. -_mul : returns the product of this CauchyList and the input CauchyList. Suppose that a, b are two CauchyLists. Then the productc = a * b is defined as follows: cli] = a[0] * b[i] + a[1] * bli - 1] + a[2] * b[i - 2] + ... + ali] * b(0] where we denoted a.content[k] by a[k] for simplicity. Remember, cli] must be reduced mod p at the end. Product of two CauchyLists of lengths m and n is a CauchyList of length m + n - 1. So the index i in c[i] can be larger than the lengths of a and b. You can use the get method to avoid IndexError. If the two CauchyLists have different values of p, this method should raise a ValueError exception. If the second argument is an integer, not a list, Then this method performs a scalar product which is much simpler. That is, if b is an integer then cli] = a[i] * b. This allows us to be able to, for example, evaluate an expression of the form a * 65. • --str_: returns a string representation of the CauchyList in the following format: p: p length: length content: content In this assignment, we are going to implement a custom list called CauchyList. A CauchyList is a standard Python list with some arithmetic operations associated to it. A description of the CauchyList class is in the following section. Your program is going to be one Python file, called assignment3.py, that contains the CauchyList class. CauchyList class Instance variables • p: an integer that is used as the modulus. This means that all operations are done mod p. • content: a list of integers. Methods • _init_: accepts an integer p. Initializes the instance variable p to p and the instance variable content to an empty list. • generate_random: accepts an integer n. Populates content with n random integers in the range [0, p - • length: returns the length of this CauchyList. 1). • get: accepts an integer i. If i is smaller than the length of the list then returns the i-th element. Otherwise, returns 0. • -_add__: returns the sum of this CauchyList and the input CauchyList. The sum of two CauchyLists is defined component-wise. That means if a, b are two CauchyList then we define c = a + b by setting c.content[i] = a.content[i] + b.content[i] where the addition is done mod p. This is a special method that is called to evaluate expressions of the form a + b. We have seen other special methods such as str_ and _lt__ in the lectures. For example, lt__ is called to evaluate expressions of the form a < b. This method should be able to handle CauchyLists of different lengths. When adding two lists of length m and n, where m > n, the resulting list should be of length m and the higher elements of shorter list is assumed to be zero. Hint: you can use the get method for this. If the two CauchyLists have different values of p, this method should raise a ValueError exception. -_sub__: similar to add_ except that it is called to evaluate expressions of the form a b. -_mul : returns the product of this CauchyList and the input CauchyList. Suppose that a, b are two CauchyLists. Then the productc = a * b is defined as follows: cli] = a[0] * b[i] + a[1] * bli - 1] + a[2] * b[i - 2] + ... + ali] * b(0] where we denoted a.content[k] by a[k] for simplicity. Remember, cli] must be reduced mod p at the end. Product of two CauchyLists of lengths m and n is a CauchyList of length m + n - 1. So the index i in c[i] can be larger than the lengths of a and b. You can use the get method to avoid IndexError. If the two CauchyLists have different values of p, this method should raise a ValueError exception. If the second argument is an integer, not a list, Then this method performs a scalar product which is much simpler. That is, if b is an integer then cli] = a[i] * b. This allows us to be able to, for example, evaluate an expression of the form a * 65. • --str_: returns a string representation of the CauchyList in the following format: p: p length: length content: content
Expert Answer:
Answer rating: 100% (QA)
import random class CauchyList def initselfp selfp p selfcontent def generateRando... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
In this question, you are going to implement a human vs. human version of Notakto. Notakto is a tic-tac-toe variant. It is played across three 3 x 3 boards: Board A, board B and board C. When you...
-
In this question, you are going to implement an artificial intelligence (AI) vs. human version of Notakto. The AI will be the Player 1, i.e., the AI will always start. Here the AI means that the move...
-
Consider a quasi-one-dimensional steady advection-diffusion flow with the boundary conditions as illustrated in Figure 1: u(x) x=0 p=0 in = puA = 1 x 10-4 -4 kg/s Figure 1 In Figure 1, the velocity u...
-
Maicom Construction Materials Inc. , hereinafter referred to as "MCM", is a construction materials company established in Moncton, New Brunswick. Its facilities (warehouse, store and offices) are...
-
What are the privacy settings on your Facebook page? Do you think that your employer or prospective employer should be allowed to look at it?
-
The adjusted trial balance of Sturdy Office Systems at March 31, 2025, follows: Requirements 1. Journalize the required closing entries at March 31, 2025. 2. Set up T-accounts for Income Summary;...
-
Input the DECHEMA coefficients in Aspen Plus from Table 13-A1 in the appendix. Then in Properties, have Aspen print out the triangular diagram for the triethylamine, carbon-tetrachloride, acetic-acid...
-
Carlsbad Enterprises has a capacity to produce 400,000 computer cases per year. The company is currently producing and selling 320,000 cases per year at a selling price of $40 per case. The cost of...
-
What is Kraljic matrix? Could you use one example to explain the four categories?
-
The cash account for Highlander Diamond Co. on April 1, 20Y5, indicated a balance of $35,400. During April, the total cash deposited was $120,255, and checks written totaled $109,200. The bank...
-
How are objects passed to functions as arguments? Give an example of what it ooks like when we pass an object as an argument? Give an example of what the subroutine header will look like when we pass...
-
A light rope is used to lift a 10-kg mass and give it an acceleration of 2.0 m/s [up] (g = 9.81 m/s [down]). What is the net force?
-
Evaluate the guidelines that will ensure the ethical management program is operated in a meaningful way.
-
An object is moving in the x-direction at 1.4m/s when it undergoes an acceleration a=0.54j^m/s2 a) Find its velocity vector after 3.8s.
-
What are some of the Methods and Challenges in Measuring Team Performance in a Performance Management System?
-
1. Starting from home, you bicycle 23km north in 2.4 hand then turn around and pedal straight home in 1.4h. What are your (a) displacement at the end of the first 2.4h, (Express your answer using...
-
The results of the preparation of the statement of cash flows will result in Cash flows provided for or used by operating, investing and financing activities. Cash Flows provided or used from...
-
Danielle has an insurance policy with a premium of $75 per month. In September she is in an accident and receives a bill worth $2990 for the repair of her own property. Her deductible is $250 and her...
-
Calculate the cathode potential (versus S.C.E.) needed to reduce cobalt(II) to 1.0 M in each of the following solutions. In each case, Co(s) is the product of the reaction. (a) 0.10 M HClO 4 (b) 0.10...
-
(a) Prepare a family of graphs for the titration of 50.0 mL of 0.0200 M HA (pKa 4.00) with 0.100 M B (pKb = 3.00, 6.00, and 9.00). (b) Write the acid-base reaction that occurs when acetic acid and...
-
The acid HA has pKa = 4.00. Use Equations 9-17 and 9-18 to find the fraction in the form HA and the fraction in the form A- at pH = 5.00. Does your answer agree with what you expect for the quotient...
-
The price at the intersection of the market demand curve and the market supply curve is called the _________ price, and the quantity is called the _________ quantity.
-
A situation where quantity supplied is greater than quantity demanded is called a(n) _________.
-
The supply of a good _________ if the price of one of its substitutes in production rises.
Study smarter with the SolutionInn App