Question: USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of question 1 (everything in the images) Here is what the code format looks like

USE PYTHON3; DO NOT IMPORT ANY PACKAGES

Please do all of question 1 (everything in the images)

USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of question1 (everything in the images) Here is what the code format lookslike in the text editor(with doctests): # Question 1 def polynomial_doctests(): """

Here is what the code format looks like in the text editor(with doctests):

# Question 1 def polynomial_doctests(): """ >>> p1 = Polynomial({0: 8, 1: 2, 3: 4}) >>> p2 = Polynomial({0: 8, 1: 2, 2: 8, 4: 4}) >>> p3 = Polynomial({0: 8, 1: 2, 3: 4, 2: 0}) >>> p1 {0: 8, 1: 2, 3: 4} >>> print(p1) 8 + 2 x + 4 x^(3) >>> p2 {0: 8, 1: 2, 2: 8, 4: 4} >>> print(p3) 8 + 2 x + 4 x^(3) >>> print(p1 + p2) 16 + 4 x + 8 x^(2) + 4 x^(3) + 4 x^(4) >>> print(p1 * p2) 64 + 32 x + 68 x^(2) + 48 x^(3) + 40 x^(4) + 40 x^(5) + 16 x^(7) >>> p1 == p1 True >>> p1 == p2 False >>> p1 == p3 True """ return

class Polynomial: """ A class that abstracts polynomial expressions. """ # YOUR CODE GOES HERE #

Question 1 Write a Polynomial class that abstracts polynomial expressions. As a refresher, polynomial expressions have this general form: do +ajx + a2x + ... + arxa In this question, you should implement the basic functionalities for the Polynomial class. Note that the starter file does not provide templates for methods, thus you need to determine which methods to implement based on the description below and the provided doctests. You are allowed to create any methods and attributes that you need. You can assume all arguments are valid, so you don't have to write assert statements. Your Polynomial class should support the following functionalities: (1) Initialization The constructor should take a dictionary as the argument, whose key-value pairs represent the terms of the polynomial. The keys are the powers (non-negative integers), and the values are the coefficients (any integers). The following examples show how a dictionary of terms represents a polynomial: Python statement Mathematical expression Polynomial({0: 8, 1: 2, 3: 4}) 8 + 2x + 4x Polynomial({0:8, 1:2, 2:8, 4:4}) 8 + 2x + 8x2 + 4x4 (2) String representations You should implement two kinds of string representations for a Polynomial instance. An example of these string representations is as follows: >>>p = Polynomial({0:8, 1:2, 3: 0, 4: 4, 2: 8}) >>>p {0: 8, 1:2, 3: 0, 4: 4, 2: 8} >>>print(p) 8 + 2 x + 8 x^(2) + 4x^(4) The first kind of string representation is simply the string representation of the dictionary of terms. You should be able to implement it in one line. The second kind of string representation displays the polynomial expression in a mathematical form. Your implementation should take care of the following requirements: (a) The terms in this representation should always be sorted by their powers in ascending order, regardless of their order in the dictionary of terms. In other words, terms with smaller power should always appear in the front. (b) Each two terms are separated by a plus sign (+). You should also take care of the whitespace. (c) You should always skip the terms with a coefficient of 0. In the example above, the term with a power of 3 is skipped due to this requirement. If all terms have 0 as coefficients, you should return an empty string. (d) In general, for any term with power p and coefficient a, you should express it with "a x^()". However, when power is 1, you should express it with "a x" (ignore "^()'); and when power is 0, you should only keep the coefficient "a" (ignore "x^)"). Edge cases: Polynomial instance 2nd string representation "10" "10 X" Polynomial({0: 10}) Polynomial({1: 10}) Polynomial({1: 10, 0: 10}) Polynomial({1: 10, 0: 10, 3:0}) Polynomial({1: -10,0:-10, 3:0}) "10 + 10 x "10 + 10 x" "-10 + -10 X" If you have any issues with tests failing even though the expected value of the doctest and the output look the same, it is likely due to additional whitespace at the end of the string that you need to remove. (3) Equality operator You should implement the equality operator (==) for the Polynomial class. For any two Polynomial instances, if their values are equivalent, the equality check should return True. This indicates that if the terms of two polynomials are identical (ignore terms with a coefficient of O), these two polynomial instances are equal. p1 == p2 Examples: Polynomial 1 (p1) Polynomial ({0:8, 1:4, 2:2}) False Polynomial 2 (p2) Polynomial ({1: 10, 0: 10, 3: 0}) p1 (the same object) True True Polynomial ({0:8, 1:4, 2:2}) Polynomial ({0: 8, 1:4, 2:2}) Polynomial ({0:8, 1:4, 2:2}) Polynomial ({0:8, 1:4, 2:2}) Polynomial ({0:8, 1: 4, 2:2, 4:0}) True (4) Arithmetic operators You should implement addition (+) and multiplication (*) operators for the Polynomial class. You can use a calculator to generate the expected output of your test cases. Note that the special methods behind these operators should return a new Polynomial instance to abstract the result of addition or multiplication. You can create a new dictionary to store the new terms, and initialize a new Polynomial instance with this new dictionary. The arithmetic operators should never modify the operand instances

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!