| Introduction |
This programming assignment is designed first to ensure that you know how to
write a simple (numeric) class that overloads many of the standard Python
operators by defining various double-underscore methods.
It also ensures that you know how to write various classes that implement
special iterators either by defining the __iter__ and __next__ methods or
writing special generators (similar to functions, but using yield not
return).
There are two parts to this assignment. In the first you will write a single class in a module. In the second you will write various small classes and generator in a module. You should download the program2 project folder and use it to create an Eclipse project. The first module can be tested in a driver I supply; you will create the second module in this project. Eventualy you will submit each module separately in Checkmate. You should work on this assignment in pairs, with someone in your lab section. Try to find someone who lives near you, with similar programming skills, and work habits/schedule: e.g., talk about whether you prefer to work mornings, nights, or weekends; what kind of commitment you will make to submit program early. If you believe that it is impossible for you to work with someone, because of some special reason(s), you should send me email stating them and asking for special permission to work alone (which I do grant, but not frequently). Only one student should submit all parts of the the assignment, but both student's names should appear in the comments at the top of each submitted .py file. It should look something like # Romeo Montague, Lab 1 # Juliet Capulet, Lab 1 # We certify that we worked cooperatively on this programming # assignment, according to the rules for pair programmingPlease turn in each program as you finish it, so that I can accurately assess the progress of the class as a whole during this assignment. Print this document and carefully read it, marking any parts that contain important detailed information that you find (for review before you turn in the files). The code you write should be as compact and elegant as possible, using appropriate Python idioms. |
| Problem #1: Rational Class (operators) |
Problem Summary:Write a class that represents and defines operators for Rational numbers, which are represented by an int numerator and denominator. With this class we can write scripts manipulating this new numeric type, which unlike floats performs exact numeric calculations. For examplex = 0 for i in irange(1,10): x += 1/10 print(x) # prints 0.9999999999999999 x = Rational(0) for i in irange(1,10): x += Rational(1,10) print(x) # prints 1/1 print(compute_e(100 )) # e ~ 1/0! + 1/1! + 1/2! + ... 1/100! print(compute_e(100).approximate(100)) # if you write approximate: extra credit (which prints... look for the / as the 10th character on the 3rd line) 42997789077987677528011991222420376346635182807847142751317828133465975238 70956720660008227544949996496057758175050906671347686438130409774741771022 426508339/1581800261761765299689817607733333906622304546853925787603270574 49521355920728670523629599959587319129243555798012243658052856289689600000 0000000000000000000 2.718281828459045235360287471352662497757247093699959574966967627724076630 3535475945713821785251664274 Details
|
| Problem #2: Bag Class (iterators) |
Problem Summary:Write a class that represents and defines methods, operators, and iterators for a Bag class. Bags are similar to sets, and have similar operations (of which we will implement just the most important) but they can store multiple copies of items. Fundamentally, we will store bags as dicts whose values are the number of times their key occurs in the bag. You must store Bags using this data type as specifiedDetails
TestingI provided a short driver that allows you to enter one-line Python statements. Note that exceptions are raised, they are printed by the driver but the Command: prompt sometimes appears misplaced.You can use this driver or write code at the bottom of your bag.py module to test the Bag class (as you are writing its methods). Try to incrementally test the methods you write, not write all methods in the class at once and then try to test them all; verify your progress as you write your code. |
| Problem #3: Module of Decorators (iterators) |
Problem Summary:Write the following iterator decorators (iterators that operate on iterators) using Python generators. You may not use the itertools.py module (or any other one that would simplify this code).
|