CS 331 Spring 2013 > Assignment 5 |
Assignment 5 is due at 5 p.m. Thursday, March 28. It is worth 20 points.
E-mail
answers to the exercises below to
ggchappell@alaska.edu
,
using the subject
“PA5
”.
pa5.py
, from Exercise A.
This file (or an archive file containing it)
should be attached to your e-mail message.In this exercise, you will write some simple Python functions and a class.
Write Python 3.x code meeting the requirements outlined below.
Place all of this in the file pa5.py
.
The file should define the following.
Time
.
Objects of this class represent a time of day,
in hours, minutes, and seconds past midnight.
The following operations should be available.
hh:mm:ss
,
with the three numbers padded on the left with zeroes, as necessary.
Times are represented using 24-hour time,
so that hh
will be from 00
to 23
,
and mm
and ss
will be from 00
to 59
.
Other requirements:
Time(30,70,-2)
should return an object that prints as
“07:09:58
”.squares6
.
This function should be a generator that yields, one at a time,
in increasing order, the sequence of all squares
whose last digit, in base ten, is
6
:
16, 36, 196, 256, 576, 676, 1156, 1296, ....
It should take no parameters,
and it should produce its values in a reasonable
amount of time.foo_maybe
.
This function takes one parameter, x
.
It attempts to call x.foo()
.
If this is successful, it returns the return value of x.foo()
.
If x.foo()
cannot be done, then it returns None
(this is a special Python no-value value).get_331_char
.
This function takes 1 parameter, an integer.
It downloads the source for the web page for this class,
and returns the character with the given index.
It should return a string (not a byte).
The first character has index 0.
Thus, with the page in its current form, get_331_char(0)
should return “<
”.foo_maybe
,
whose purpose might be considered a form of type checking).__str__
”.
Once you write that, you should be able to use the
built-in print
capability.
Like so:
[Python 3.x]
x = Time(1, 2, 3) print(x)
self
”),
representing the object it is called on.
A test program is available:
pa5_test.py
.
This will test whether your package works properly.
To do the testing, place the test program and your file (pa5.py
)
in the same directory, and run the test program
(e.g., at a Unix prompt, type “python3 pa5_test.py
”).
Do not turn in pa5_test.py
.
ggchappell@alaska.edu