Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, we are going to implement a custom list called CauchyList. A CauchyList is a standard Python list with some arithmetic operations

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

Step by Step Solution

3.42 Rating (161 Votes )

There are 3 Steps involved in it

Step: 1

import random class CauchyList def initselfp selfp p selfcontent def generateRando... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Quantitative Chemical Analysis

Authors: Daniel C. Harris

8th edition

1429218150, 978-1429218153

More Books

Students also viewed these Programming questions

Question

Defi ne nursing informatics. AppendixLO1

Answered: 1 week ago