当前位置:天才代写 > 编程代写 > 编程考试代写 Programming Exam代写 ICS­33代写

编程考试代写 Programming Exam代写 ICS­33代写

2022-01-13 14:56 星期四 所属: 编程代写 浏览:381

编程考试代写

ICS­33: In­Lab Programming Exam #2

PRINT AND SIGN YOUR NAME ABOVE NOW

编程考试代写 IMPORTANT: Store a Polynomial as a dictionary (dict) of terms: using the power as the key, and its coefficient as the value associated with a power.

This in­lab programming exam is worth a total of 100 points.

It requires you to write a class with methods that overload some operators, as other double­underscore methods: methods like __init__,__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 self­check 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 write­on/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().

Details   编程考试代写

1.Define a class named Poly in a module named poly.py

2.Define an __init__ method that has an arbitrary number (zero or more) of 2­tuple arguments: each will store the coefficient and power of one term in the Polynomial. Note that the coefficient is the first value in the tuple; the power is the second value in the tuple. For example, writing Poly( (3, 2), (­2, 1), (4, 0) ) represents the Polynomial 3x2­ 2x + 4; recall that x^0 is 1). Finally, note that the terms can appear in any order: writing Poly( (4, 0), (­2, 1), (3, 2) ) represents the same polynomial.

IMPORTANT: Store a Polynomial as a dictionary (dict) of terms: using the power as the key, and its coefficient as the value associated with a power. For the Polynomial above, the dictionary would be {2: 3, 1: ­2, 0:4}; of course the keys/values in a dictionary can print in any order. You must name this dictionary attribute self.terms, because I have defined a __str__ method that uses this name and many bsc.txt tests assume I can set this dictionary. Objects in the Poly class should define no other attributes. Calling Poly() should set self.terms to an empty dictionary.

This dictionary should never have a coefficient of 0 for any power; if a coefficient of 0 is specified for a power, omit it from the dictionary.

The __init__ method should raise an AssertionError with an appropiate message if the argument violates any of the following conditions:

Each coefficient must be an int or float value.

Each power must be an int whose value is >= 0.

A power cannot appear as a later term if it appears (with a non­zero coefficient) as an earlier term: writing Poly( (3, 2), (­2, 1), (1, 2) ) should raise an AssertionError because the first and last term specify a power of 2. It is legal to wrie Poly( (0, 2), (­2, 1), (0, 2) ) which results in the Polynomial ­2x. Hint: as each term is processed, check whether the power already appears in the dictionary.

For example, in my code writing Poly( (3, ‘abc’) ) raises AssertionError with the message Poly.__init__: illegal power in : (3, ‘abc’).

3.I have defined a __str__ method that returns a string that nicely displays a Polynomial, using the standard abbreviations for coefficients and powers (and ordered by decreasing powers).   编程考试代写

For p = Poly( (3, 2), (­2, 1), (4, 0) ), calling str(p) returns the string ‘3x^2 ­ 2x + 4’; note that no coefficients in a polynomial will be 0, but Poly() (a Polynomial constructed with no terms) will print as 0.

IMPORTANT: Most of the self­checks in the bsc.txt file check the string representations of a Polynomial, so you must store the self.terms dict correctly in __init__ and in any other methods that create/return Polynomials.

4.Define a __repr__ method that returns a string, which when passed to eval returns a newly constructed Poly with the same terms as the Poly object __repr__ was called on.

For p = Poly((3, 2), (­2, 1), (4, 0)), calling repr(p) returns something like ‘Poly((4, 0), (­2, 1), (3, 2))’; note that the order of the terms doesn’t matter, so long as calling eval on the string produces the correc results.    编程考试代写

5.Define a __len__ method that returns the higest power in the Polynomial. For the Polynomial 3x52x2­ 1, its length is 5. As a special case, a Polynomial with no terms represents 0 and should return a length of 0.

6.Define a __call__ method that takes one numeric (int or float) argument: it returns the value produced by evaluating the Polynomial with that value substituted for x. For example if p is 3x2­ x4, then p(2) returns 3*22­ 1*2 + 4 which is 14

7.Define an __iter__ method that produces 2­tuples (containing a coefficient followed by its power), such that the powers of the tuples are decreasing: for example if p is 3x2­ x + 4, then iterating over the p produces the tuple (3, 2) followed by (­1, 1) followed (4, 0).

编程考试代写
编程考试代写

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

(1)If the Polynomial is 3x2+ 4, then adding a term with coefficient ­2 and power 1 (­2x) would produce the Polynomial 3x2 ­2x + 4.

(2)If the Polynomial is 3x2 + 4, then adding a term with coefficient 2 and power 2 (2x2) would produce the Polynomial 5x2 + 4.

(2a) If the Polynomial is 3x2 + 4, then adding a term with coefficient ­3 and power 2 (­3x2) would produce the Polynomial 4.

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

For example, adding 3x3 + 2x2 + 4 with x3 ­ 2x2 + x + 2 produces 4x3 + x + 6.

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.

For example, multiplying 3x2 + 2x + 1 by x + 2 produces 3x3 + 8x2 + 5x + 2. This calculation is illustrated in the table below.

Term Term in Left Term in Right Resulting Term Added into
Operand Operand
# Product
3x2 + 2x + 1 x + 2
编程考试代写
1 3x2 x 3x3
2 3x2 2 6x2
3 2x x 2x2
4 2x 2 4x
5 1 x x
6 1 2 2

In the result, 3x3 comes from the 1st term added to the product, 8x2 comes from the 2nd and 3rd terms (6x2 and 2x2) added to the product, 5x comes from the 4th and 5th terms (4x and x) added to the product, and finally 2 comes from the 6th/last term added to the product.

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.

编程考试代写
编程考试代写

 

更多代写:国际经济学网课代上  托福代考  管理学assignment代写  管理学essay代写  管理学research proposal代写  人文社科网课代修

合作平台:essay代写 论文代写 写手招聘 英国留学生代写

 

天才代写-代写联系方式