ICS33: InLab Programming Exam #2
Name (printed): Name (signed):
PRINT AND SIGN YOUR NAME ABOVE NOW
This inlab programming exam is worth a total of 100 points. It requires you to write a class with methods that overload some operators, as other doubleunderscore methods: methods likeinit , len , iter , getitem , and call .I will supply a short script for calling these methods and printing their results, so that you can check them visually for correctness. These are followed by calling the driver with an extensive batch selfcheck file that is similar to the one we will use for grading purposes. You can test each method independently: none will depend on whether any of the other methods that you must write work correctly. Although, sometimes using previously written methods (if you wrote them correctly) can simplify your code in later methods.
You will have approximately 100 minutes to work on the exam, after logging in and setting up your computer. You will write test, and debug the methods in the module that defines the class: it will be in a folder that you will rename like PattisRichard (your last name followed by your first name). You may writeon/annotate these pages., as we will collect them from you at the end of the exam.
We will test you methods only for correctness; each test you pass will improve your grade; methods that produce no correct results will earn no credit. This means that your methods must define exactly the parameters specified in the descriptions below and must work on all the example arguments; your methods should also work on any other similar/correct arguments. To aid you writing and debugging these methods
1. Write clear, concise, and simple Python code (there are no statement restrictions).
2. Choose good names for local variables.
You do not need to include any comments in your code; but, feel free to add them to aid yourself. We will not grade on appropriate names, comments, or Python idioms: only on correctness. You may also call extra print methods in your code or use the Eclipse Debugger to help you understand/debug your methods.
You may use any functions in the standard Python library and the goody and prompt modules (which will be included in the project file you will download). Documentation for Python’s standard library and these modules will be available during the exam. I have written all the standard import statements that I needed in the module in which you will write your methods; feel free to include other imports, or change the form of the included imports to be simpler to use.
If you are having problems with the operating system (logging on, downloading the correct folder/files, accessing the Python documentation, submitting your solution) or Eclipse (starting it, setting it up for Python, running Python scripts, running the Eclipse debugger, turning on line numbers) please talk to the staff as soon as possible. We are trying to test only your programming ability, so we will help you with these other activities. But, we cannot help you understand, test, or debug your programming errors. I have asked the TAs and Tutors to NOT ANSWER ANY QUESTIONS about the exam itself: read it carefully and look at the test cases for clarification.
You should have a good understanding of the solution to Programming Assignments #1#2; you should also have a good understanding of the material on Quiz #3 and questions 1,3, and 4 on the Midterm exam. You should know how to read files; create, manipulate, and iterate over lists, tuples, sets, and dictionaries (dict and defaultdict) in 3 ways: by keys, values, and items; know how to call the functions all, any; min, max, sum; join, split; sort, sorted; enumerate, zip. You may need to use *args as a parameter specification: we use it to define a method call that will match any number of positional (non named) arguments. You are free to use or avoid various Python language features (e.g., lambdas and comprehensions): do what is easiest for you, because we are grading only on whether your functions work correctly.
The Poly class
A Poly(nomial), as defined here, represents the coefficients and powers of a polynomial. Write a class that represents Poly objects and defines methods for various operators for Poly objects: operators will typically be defined to work on Polynomials, or between Polynomial and single numeric (int or float) values. Whenever we perform arithmetic on Polynomials, the result is another Polynomial. The Poly class will be be immutable except for one helper method _add_term (and the setitem and
delitem methods). All other methods will not mutate the Polynomial; methods that must return a Polynomial (like the arithmetic operators) will return a newly constructed Poly object.
You can write simple code to test your class in its script (I have put a few simple tests at the front), and/or use the large bsc.txt file supplied with the project. If you want to see the exceptions raised, and their messsages, uncomment the defaults before the call to driver.driver().


8. The following three methods are all similar in structure. You might find it useful to call these methods implicitly (using indexing) in the operators you are asked to define below; but doing so is not necessary.
· Define a getitem method whose argument is any power; it returns the coefficient associated with that power. If the argument is not an integer or is < 0, raise a TypeError exception with an appropriate message. If the argument is not a power in the Polynomial’s dictionary, return the int 0.
· Define a setitem method whose arguments are any power and its coefficient; it associates the power with the coefficient, whether or not that power is already in the Polynomial dictionary. If the power argument is not an integer or is < 0, raise a TypeError exception with an appropriate message. If the coefficient argument is equal to 0, don’t create/change that term, but instead delete it from the dictonary if it is present, because no coefficients of 0 should appear in the Polynomial. This method can mutate the state of a Polynomial.
· Define a delitem method whose argument is any power; it deletes that power (and its associated coefficient) from the Polynomial if it is present (if the power is not present, the Polynomial remains unchanged). If the argument is not an integer or is < 0, raise a TypeError exception with an appropriate message. This method can mutate the state of a Polynomial.
9. Define the relational operator ==. This operator must work correctly when one operand is a Poly and the the other operand is a Poly or a numeric (int or float). For any other operands, raise the TypeError exception with an appropriate message.
Two Poly objects are considered equal if they have exactly the same powers, with each power having equal coefficients. A Poly object is considered equal to a numeric if the Poly has only a constant term whose value is equal to the numeric.
10. Write this method before implementing addition and multiplication
Define the helper method _add_term which take a Poly object (as the self parameter) and a coefficient and power as arguments that specify the term: the coefficient must be numeric (int or float) and the power must be an int that is >= 0; otherwise raise a TypeError exception with an appropriate message. It mutates the Poly object to include (add in) the specified term according to the following rules: (1) if the power is not in the Polynomial and the coefficient is not 0, it is added along with the coefficient; (2) if the power is in the Polynomial, the coefficient is added to existing coefficient for that power, but (2a) if the resulting sum/coefficient is 0 that power is deleted from the Polynomial. Here are some examples of these rules

You can call the _add_term helper method when definiing the + and * operators specified below.
11. Define methods for the + operator, where one operand is a Polynomial and the other operand is a Polynomial or a numeric value (int or float): + produces a new Polynomial result; its arguments do not change. For any other operands, raise the TypeError exception with an appropriate message.
Recall that this operator does not mutate its operands. It produces a new Polynomial and returns it as a result: we can compute such a Polynomial by constructing an new empty Polynomial and then iterating over the operand Polynomials and adding terms (see _add_term, which has all the needed properties) to the newly constructed Polynomial. You can use an int/float value directly, or you can convert it to a Polynomial first: 5 is equivalent to Poly( (5,0) ) in that its coefficient is 5 and its power for x is 0.
· When adding two Polynomials, the result has all the powers of each operand; if a power appears in both operands, its coefficient in the resulting Polynomial is the sum of the operand coefficients (although if the coefficients sum to 0, that power will not appear in the Polynomial).

Note that + is commutative: for Polynomials a and b, a+b produces the same Polynomial as b+a. Commutivity can make programming these methods simpler.
12. Write these methods last (others are simpler)
Define methods for the * arithmetic operator, where one operand is a Polynomial and the other operand is a Polynomial or a numeric value (int or float): * produces a new Polynomial result; its arguments do not change. For any other operands, raise the TypeError exception with an appropriate message.
Recall that this operator does not mutate its operands. It produces a new Polynomial and returns it as a result: we can compute such a Polynomial by constructing an empty Polynomial and then iterating over the operand Polynomials and adding terms (see _add_term, which has all the needed properties) to the constructed Polynomial. You can use an int/float value directly, or you can convert it to a Polynomial first: 5 is equivalent to Poly( (5,0) ) in that its coefficient is 5 and its power for x is 0.
When multiplying two Polynomials, the resulting Polynomial has terms for every term in the first operand multiplied by every term in the second operand. Iterate over terms in each operand and determine the coefficient and power for adding the resulting term they produce.

Note that * is commutative: for Polynomials a and b, a*b produces the same Polynomial as b*a. Commutivity can make programming these methods simpler.