Most homework and lab assignments are taken from the M2 textbook. This page
lists the assignments not from the M2 textbook.
Lab08: due 13/14/15 Nov
Write a library that provides robust user input:
For each basic Python data type (int, float, bool, str), provide a function
that reads input from
the keyboard and returns the value in the proper type.
E.g., input_str(), input_bool(), etc.
- If the user types an invalid input (e.g., a string when an
integer is expected), then an error message is printed, and
the function keeps prompting the user until
the user does enter a valid input.
- Make sure to handle if the user just presses "Enter" without any input.
- Also provide a way for the user to cancel.
- Each function should be able to take
either zero or one parameters; the parameter, if present, should be the
prompt string (just like with regular input()).
- Each function should return a value of the proper type, or
None if the user cancelled.
- For input_bool(), allow the user to type any of
(1, T, t, True, true, TRUE, Y, y, Yes, yes, YES) etc. for True,
and any of (0, F, f, False, false, FALSE, N, n, No, no, NO) etc. for
- Hint: exception handling may be useful.
HW09: due Wed 15Nov
Create a wrapper for the open() function. When a program
opens a file successfully, a file handle will be returned. If the file open
fails, rather than generating an error, return None to the callers so that they
can open files without an exception handler.
HW10: due Mon 20Nov
Calculate a knight's
tour for a 3x4 board, starting at the corner (0,0). In addition to the
completed board, also show all your backtracking, using a tree diagram or a
list of moves or whatever you see fit.
Lab09: due 20/21/22 Nov
Write a Python class Complex implementing the
abstract data type.
Try to emulate the built-in complex type as closely as possible.
- Obviously, you may not use the built-in complex type. :)
- Overload at least the following numeric operators:
+, -, *, /, unary -, unary +, abs. Make sure to do
reflected versions, too (radd, rsub, etc.).
- Provide a way to
numbers by defining the __cmp__ method.
Define your own reasonable way to compare complex numbers.
- Define __str__ to print out your complex number in the same
format the built-in Python complex numbers use.
- Define the
__complex__ method to convert from your type to a
built-in Python complex number.
- Write your initializer method such that:
- Complex() returns 0+0j
- Complex(r) returns r+0j (r is int or float)
- Complex(c) works properly when c is a Python complex number
(this provides a way to convert from Python complex to your type)
- Complex(r,i) returns r+ij
- Write a test-bed program to show off your complex number library.
(design/pseudocode not necessary for test-bed)
Lab10: due 27/28/29 Nov
Choose one of your previous Labs 02-07 and rewrite it in Modula-2, with a full
You may reuse parts of your old lab writeup, although some refinements of
your pseudocode may need to be revisited before you start doing your Modula-2
code. You may wish to refer to
M2 tutorial and the M2 text.
HW11: due Fri 1Dec
Given the following simple Python class definition for a doubly-linked
(i.e., bidirectional) list, write a function delete(head, n) which
takes a reference to the head of the list and a positive integer index
(head is n=0) and returns a reference to the list with the nth entry deleted.
If n is beyond the end of the list, return None. Make sure to handle the
special case n=0 (deleting the head).
def __init__(self, data=None, prev=None, next=None):
self.data = data
self.prev = prev
self.next = next