Starting from:
$30

$24

Lab 2: A Polynomial Class Solution

Instructions



This lab will cover some fundamentals of Python programming and basic object-oriented programming. You will need to be familiar with how to write a class in Python, as well as operator overloading.




Try to write a completed implementation by the end of the lab time. It is a good idea to think carefully about how to solve the problem and plan your approach carefully. You do not (and probably should not) begin writing code immediately. It is good practice to work with pen and paper, working out some sample inputs and outputs by hand, and possibly drawing diagrams.




As added practice, it can be a good idea to try writing your code out on pen and paper rst. In addition to being a useful approach for planning out a solution, it is good practice for exams, where you will have to write some code on paper.




When you type up your code, write some test cases. Try multiple test cases for each function you have to implement, and consider edge cases carefully.




1.1 What to submit




If you believe you have a working solution, have a TA or the professor check your solution.




If you don’t nish by the end of the lab, you will may submit a solution on NYU Classes by Sunday June 9, 11:55 PM.




Either when a TA has checked your code, or by the Sunday submission deadline, submit a single \.py" le to the \Lab 1: Polynomial" assignment on NYU Classes. The \.py" le should contain your implementation of the Polynomial class described below. Place any test code you wrote in a \main" function.




The Polynomial Class



For this lab, we wish to implement a class to represent a polynomial. We will represent a polynomial by storing a list with its coe cients, as a data member of







1















the class. The rst element of the list (index 0) will represent the constant; the second (index 1) will represent the coe cient of the x term; and so on, with each next element representing the coe cient of the next power of the polynomial.




Example. The coe cient list of p(x) = 5x3 17x + 42 would be [42,-17,0,5].




Note that the length of the coe cient list is one more than the degree of the polynomial. (Recall that the degree is the largest power with a non-zero coe cient.)




The class you implement should include the following methods:




Constructor: The constructor should take in a list of coe cients and initialize a polynomial with the coe cients given in the list. If no list is given, initialize the polynomial p(x) = 0.




repr: You should implement the repr special method to return a string representation of the polynomial. You may use ^ instead of super-script to represent powers.




eval: This method should take a value and evaluate the polynomial for







that value. For example, with the polynomial in the example above (i.e., p(x) = 5x3 17x + 42), calling eval(1) should return 30.




Addition: You should implement the addition operator (using operator overloading) to add two polynomials. Recall that adding two polynomials means adding the coe cients of the same power. Beware of cases where




the two polynomials being added have di erent degrees. For example:

(3x4 10x2 + 5x + 7) + (2x12 + 3x2 2) = 2x12 + 3x4 7x2 + 5.




Multiplication: You should implement the multiplication operator (us-ing operator overloading) to multiply two polynomials. In order to multi-ply polynomials, you will have to multiply each pair of terms | each pair of terms when multiplied gives a new term whose coe cient is the product of the two terms, and whose power is the sum of the powers of the two terms (e.g., 2x4 7x3 = 14x7) | and group terms of the same power.




A simple example: (x + 1) (2x + 3) = 2x2 + 5x + 3.




And a more complicated example: (3x3 + 2x) (2x11 + 5x3 + x) = 6x14 + 15x6 + 3x4 + 4x12 + 10x4 + 2x2 = 6x14 + 4x12 + 15x6 + 13x4 + 2x2.




A function polySequence which takes a start, end, and step, and re-turns a generator. The generator will evaluate the polynomial for the value start, then start + end, and so on, up to end and yield these values one at a time. If no step is given, a step of 1 should be used (similar to the range method). As an example, if p represents the polynomial 2x + 1 (coe cient list [1,2]), the code




for val in p.polySequence(0,5):




print(val)




should print the values 1, 3, 5, 7, and 9 on separate lines.







2















* Extra Challenge: Derivative




For an extra challenge, write a list comprehension to construct the coe cient list of the derivative of a given polynomial. Use this list comprehension to implement a new member method, derivative that computes the derivative and returns it as a new Polynomial object.




Note: The derivative of a polynomial is computed by using the power rule: the derivative of p(x) = xn is nxn 1 (for any number n), and you can apply this rule to each term of a polynomial. For example, the derivative of 2x3 + 4x + 1 is 6x2 + 4.








































































































































3

More products