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 a class that implements iterators,
by defining both the __iter__ and __next__ methods.
Finally it ensure that you know how to write iterator decorators by using
special generators (similar to functions, but using yield instead of
return) in a module.
There are three separate parts to this assignment. In the first part you will write a module defining a single, large numeric class that overloads many operators (including those performing arithmetic and relational comparisons). In the second part you will write a medium-size class, which defines various methods, including some double-underscore methods and specifically the iterator protocol methods (__iter__ and __next__). In the third part you will write a module that defines various iterator decorators implemented by generators: each iterator decorator has argument(s) that are iterators and is itself used an iterator.
You should download the program2 project folder and unzip it to produce an Eclipse project with three modules. You will write classes in the first two modules, which can be tested in the standard driver using the batch self-check files I supplied; you will write generators in the third module, which can also be tested by a batch self-check I supplied, but can are tested by a special generatordriver.py module included in this project. Eventually you will submit each of the three modules you write separately to Checkmate.
It is recommend that you 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.
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 programmingIf you do not know what the terms cooperatively and/or rules for pair programming mean, please read about Pair Programming before starting this assignment. Please 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 example
x = 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
|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 dictionaries (use defaultdict) whose elements are keys and whose values are the number of times the key occurs in the bag. You must store Bags using this data type as specified
TestingThe bag.py module includes a script that calls driver.driver(). The project folder contains a bsc2.txt file (examine it) to use for batch-self-checking your class. These are rigorous but not exhaustive tests. Incrementally write and test your class; check each method as you write it.
Note that when exceptions are raised, they are printed by the driver but the Command: prompt sometimes appears misplaced.
You can write other code at the bottom of your bag.py module to test the
Bag class, or type code into the driver as illustrated below.
Notice the default for each command is the command previously entered.
|Problem #3: Module of Decorators (iterators)||
Problem Summary:Write the following iterator decorators (iterators that operate on iterators) using Python generators. Write each function to be self contained, not calling any other functions or generators and do not use itertools.py module (or any other module that would trivialize this code); you may also not call the zip function. Note that the argument passed to these generators can be anything that we can iterate: that includes strings, lists, tuples, dictionaries, and other generators: so, you cannot compute the len of the parameter because although strings, lists, tuples, and dictionariess have simple to compute lengths, generators don't.