CSC207H1S March Midterm 2005
Duration — 50 minutes Student Number:
Aids allowed: none
Last Name: First Name:
Do not turn this page until you have received the signal to start.
(Please ﬁll out the identiﬁcation section above,
and read the instructions below.) Good Luck!
# 1: /10
This midterm consists of 4 questions on 7 pages (including this one). When
you receive the signal to start, please make sure that your copy is complete.# 2: /15
Comments are not required except where indicated, although they may help # 3: /15
us mark your answers. They may also get you part marks if you can’t ﬁgure
out how to write the code. # 4: /10
If you use any space for rough work, indicate clearly what you want marked.
Total Pages = 7 Page 1 cont’d... Question 1. [10 marks]
Consider these ﬁles:
e1.txt, e2.txt, and e3.txt – lists of e1, e2, and e3 marks.
marks.txt – A summary ﬁle containing all marks.
gather.py – Produces the ﬁle marks.txt, a summary of the marks in e1.txt, e2.txt, and e3.txt.
Part (a) [5 marks] Write a makeﬁle that has two targets:
clean – Removes marks.txt.
marks.txt – Runs gather.py if marks.txt is older than any of e1.txt, e2.txt, or e3.txt.
Part (b) [5 marks]
Assume that you have been asked to generalize gather.py so that it works with any number of individual
marks ﬁles rather than working explicitly with e1.marks, e2.marks, and e3.marks. It should do this by
reading the names of those ﬁles as command line arguments using getopt. It also should read the name
of the ﬁle summarizing all the marks that way as well, rather than assuming the name will be marks.txt.
1. Assume that the individual marks ﬁles are called m1, m2, m3, and m4, and the summary ﬁle is called
total.txt. Circle the true statement below:
Only total.txt can be speciﬁed using an option parameter.
Only m1 through m4 can be speciﬁed using option parameters.
All ﬁve ﬁles can be speciﬁed using option parameters.
None of the ﬁles can be speciﬁed using option parameters.
2. Call gather.py on those ﬁles so that it matches your circled choice. (Pick any option names you
Student #: Page 2 of 7 cont’d... Question 2. [15 marks]
This question has you write regular expressions for a marks ﬁle in the format used by many CS instructors.
Part (a) [9 marks]
At the beginning of the ﬁle is a set of marks deﬁnitions, each on a separate line. Each mark name must
start with an upper- or lowercase letter, and contain only alphanumeric characters (’a’ through ’z’, ’A’
through ’Z’, 0 through 9).
There are the three kinds of mark deﬁnitions:
• String mark: the name of the mark, whitespace, a double-quote, whitespace.
• Numeric mark: the name of the mark, whitespace, a forward slash, whitespace, an integer (which
is what the mark is out of), whitespace.
• Computed mark: the name of the mark, whitespace, an equals sign, and a list of 1 or more mark
names and weights, where each name/weight pair is separated by a colon, and the pairs are separated
Here is an example that contains a string mark, two numeric marks, and a computed mark.
e1 / 100
e2 / 100
final = e1:50 e2:50
Write regular expressions that will match these mark deﬁnitions. In case it helps, \d matches digits, \s
matches whitespace, . matches anything except the end of a line, and \t is the tab character.
Part (b) [4 marks]
After the marks deﬁnitions come student lines. Each student line in the grades ﬁle has a 9-digit id, exactly
4 spaces, the name of the student (which must have at least one character and may be arbitrarily long; any
character except a tab is allowed), and any number of marks, each preceded by a tab. Here is an example
(there are tabs before pgries, the 80, the 40, and the 60; this example corresponds with the examples of
mark deﬁnitions from Part (a)):
999888777 Gries Paul pgries 80 40 60
Write a regular expression that will match any student line following this format and extract the student
number as a group, the name as a group, and the entire list of grades (including ta