## 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 like** init **,

**,**

**len**

**,**

**iter**

**, and**

**getitem**

**.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**

**call****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.**

**driver**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

**methods in your code or use the Eclipse Debugger to help you understand/debug your methods.****print**

You may use any functions in the standard Python library and the ** goody **and

**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.**

**prompt**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 ** list**s,

**s,**

**tuple****s, and dictionaries (**

**set****and**

**dict****) in 3 ways: by**

**defaultdict****,**

**keys****, and**

**values****; know how to call the functions**

**items****,**

**all****;**

**any****,**

**min****,**

**max****;**

**sum****,**

**join****;**

**split****,**

**sort****;**

**sorted****,**

**enumerate****. You may need to use**

**zip****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.**

***args**### 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

**objects: operators will typically be defined to work on Polynomials, or between Polynomial and single numeric (**

**Poly****or**

**int****) values. Whenever we perform arithmetic on Polynomials, the result is another Polynomial. The**

**float****class will be be immutable except for one helper method**

**Poly****(and the**

**_add_term**

**and**

**setitem**__ __** 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

**object.**

**Poly**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

**, raise a**

**< 0****exception with an appropriate message. If the argument is not a power in the Polynomial’s dictionary, return the**

**TypeError****.**

**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

**, raise a**

**< 0****exception with an appropriate message. If the coefficient argument is equal to**

**TypeError****, 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.**

**0****· **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

**, raise a**

**< 0****exception with an appropriate message. This method can mutate the state of a Polynomial.**

**TypeError**9. Define the relational operator ** ==**. This operator must work correctly when one operand is a

**and the the other operand is a**

**Poly****or a numeric (**

**Poly****or**

**int****). For any other operands, raise the**

**float****exception with an appropriate message.**

**TypeError**Two ** Poly **objects are considered equal if they have exactly the same powers, with each power having equal coefficients. A

**object is considered equal to a numeric if the Poly has only a constant term whose value is equal to the numeric.**

**Poly**### 10. Write this method before implementing addition and multiplication

Define the helper method ** _add_term **which take a

**object (as the**

**Poly****parameter) and a coefficient and power as arguments that specify the term: the coefficient must be numeric (**

**self****or**

**int****) and the power must be an**

**float****that is**

**int****; otherwise raise a**

**>= 0****exception with an appropriate message. It**

**TypeError****the**

**mutates****object to include (add in) the specified term according to the following rules: (1) if the power**

**Poly****in the Polynomial and the coefficient is not**

**is not****, it is added along with the coefficient; (2) if the power**

**0****in the Polynomial, the coefficient is added to existing coefficient for that power, but (2a) if the resulting sum/coefficient is**

**is****that power is deleted from the Polynomial. Here are some examples of these rules**

**0**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 (

**or**

**int****):**

**float****produces a new Polynomial result; its arguments do not change. For any other operands, raise the**

**+****exception with an appropriate message.**

**TypeError**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****value directly, or you can convert it to a Polynomial first:**

**float****is equivalent to**

**5****in that its coefficient is**

**Poly( (5,0) )****and its power for**

**5****is**

**x****.**

**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

**and**

**a****,**

**b****produces the same Polynomial as**

**a+b****. Commutivity can make programming these methods simpler.**

**b+a**### 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 (

**or**

**int****):**

**float****produces a new Polynomial result; its arguments do not change. For any other operands, raise the**

*******exception with an appropriate message.**

**TypeError**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****value directly, or you can convert it to a Polynomial first:**

**float****is equivalent to**

**5****in that its coefficient is**

**Poly( (5,0) )****and its power for**

**5****is**

**x****.**

**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

**and**

**a****,**

**b****produces the same Polynomial as**

**a*b****. Commutivity can make programming these methods simpler.**

**b*a**