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 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: Dimensional Class (operators)||
Problem Summary:Write a class that represents and defines operators for Dimensional numbers, which are represented by an int or float value and 3 integers representing the dimensions: length, mass, and time. With this class we can write scripts that perform numeric calculations on values of this new numeric type, which automatically keeps track of the dimensions for each value (and prohibits operations that mix dimensions in illegal ways).
For example, to represent the standard acceleration due to gravity at the Earth's surface we might write g = Dimensional(9.8,l=1,t=-2) which represents 9.8 l/t**2: the dimensions are length per time squared (where the unstated length unit is is meters and the unstated time unit is seconds: so this really represents 9.8 m/s**2). Each of the three integers represents the power of that dimension, with positive powers in the numerator and negative powers in the denominator. There are two important rules for operating on Dimensional values.
|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.