Assignment 5
Due: 3:00 PM on Wednesday, March 5, 2008
Question
Recall the Python script for operating on sparse integer-valued 2D-matrices
given in Assignment #4. We can define operations of single
matrices that apply a particular function to each elemnt of that
matrix, e.g., op1 * -1 (negation); similarly, we can define
operations (besides addition and subtraction) on pairs of same-dimensioned
matrices that apply a function to each pair of matched elements,
e.g., (3 * op1) - op2. Each such operation can be specified by a
symbol or string denoting that operation, an integer 1 or 2 indicating
whether that operation is monadic or dyadic, and the Python expression
associated with that operation (phrased in terms of op1 for monadic
operations and op1 and op2 for dyadic operations). Given this, we can define
a very simple scripting language consisting of the following statements:
- import file -- read in operation-specification file
file.s2mop
- read file -- read in sparse 2D-matrix file from
file file.s2m
- print file -- print sparse 2D-matrix file to the
terminal
- write file -- write out sparse 2D-matrix file to
file file.s2m
- res = mop op1 -- apply mondaic operator
mop to sparse 2D-matrix op1 and store result in sparse
2D-matrix res.
- res = op1 dop op2 -- apply dyadic
operator dop to sparse 2D-matrices op1 and op2
and store result in sparse 2D-matrix res.
Let such scripts be stored in files with a ".s2ms" extension.
Write and document a Python script s2m.py which takes as a
command-line argument an s2m script and interprets the
commands in this script. Where possible, define your own functions to
simplify code structure; when you do, make sure these functions are
documented with their own doc-strings and (where necessary) internal
comments. Your script must work on s2m script file
script1.s2ms, s2m operatio-specification file
op1.s2mop, and s2m datafiles
m1.s2m and m2.s2m
to produce the output given in typescript-file
s2m.script. You may assume that all given
datafiles are formatted correctly and are readable.
Hints
You may find it useful to store matrices manipulated by the program as
dictionaries nested inside a top-level dictionary.
You may find it useful to store operation-specifications in a dictionary.
You may find both lambda functions and the apply() function useful in
dynamically interpreting operation-specifications.
Submission
Please hand in printed copies of all of your Python script files.
You must also submit these files electronically using the
submit-assignment command.
At the top of each script, please include a triple-quoted docstring
with your name and student number, the name of this course and the
assignment number, and a brief description of your program; also include
brief comments throughout your program describing various code-blocks.
You do not have to develop your code on our CS departmental systems.
However, as your code will be compiled and tested on our CS departmental
systems (which use Python Version 2.4) as part of the
assignment marking process,
you should ensure that your code compiles and runs correctly on at
least one of these systems.
- Feb 26, 6:00pm
Assignment #5 posted.
Created: February 26, 2008
Last Modified: February 26, 2008