I210: Information Infrastructure I (Intro to
Programming) — Midterm Exam Review
Perform the requested actions for the following three test sections. In the real Midterm Exam you would submit the programs from Sections 2 and 3 ("sum.py" and "sieve.py") to Oncourse when you were finished with both. You would submit to your lab section, the "Midterm Exam" assignment. You would probably have to click "Submit" twice, as you do with labs. You will be responsible for making sure your work is submitted. There will be no late submissions.
á Click here
to take the quiz. In the real
exam you would click "Grade & Submit" (only once) after you have
carefully checked your answers. Be
sure to put your username in the "ID" field, not your student ID number, or you will
not get credit for this portion of the test. (This study guide does not have an ID field, so this only
applies to the real test.)
Section 2 – Debugging (25 points):
á
Download
the "sum_bad.py" program here. (You must
unzip it after downloading.)
á
Download
the "numbers.txt" file here.
á
Rename
"sum_bad.py" to "sum.py" and fix it. There are five errors that need to be
fixed. When working correctly,
this program should read the "numbers.txt" file, which has one number
per line, and calculate and display the sum of all numbers found there. (The correct sum is 70.0.)
á
In
the real test you would submit "sum.py" together with the next
section's "sieve.py" to Oncourse when you were finished with both.
Section 3 – Programming (50 points):
á Download the
"integers.txt" file here.
á Download the
"evens.txt" file here.
á Download the
"odds.txt" file here.
á Write a program called
"sieve.py" that asks for one filename to read and asks for two more
filenames to write to, and then copies the even numbers out of the first file
into the second file and copies the odd numbers out of the first file into the
third file. You can test it by reading the downloaded "integers.txt"
file, writing to filenames of your choosing, and comparing the files you wrote
to the downloaded "evens.txt" and "odds.txt". The files you wrote should be identical
to "evens.txt" and "odds.txt". If this were the real test you could get up to 30 points
for writing a program that simply works, regardless of how it does so.
á You could get up to 10 more points
for correctly putting the file-opening code, the odd or even number extracting
code, and the main body of code into functions as specified below:
o Put the file-opening code into an
"open_file" function that has two parameters—the filename and
the access mode—and which returns the newly opened file object.
o Put the number extracting code into
a "copy_numbers" function, that has three parameters—the source
file object, the target file object, and a Boolean variable indicating whether or
not the function is to copy even numbers (if it is True, the function should
copy even numbers; if it is False, the function should copy odd numbers). The function need not return anything.
á Note that this means you will have
to call this function twice, once for even numbers, once for odd numbers. This is not an efficient way to solve
this problem; rather, it is designed this way to test your understanding of
functions and your ability to specify the logic necessary to make the right
decision about when a number should be written and when it shouldn't. Since you have to read through the
source file twice, you will either need to close and reopen it or you can
rewind it. The way to rewind a
file in Python is to "seek" to the first byte in the file: the_file.seek(0).
o Put everything else into a
"main" function, so that the only code that is not in any function
consists of the call to main() and the usual raw_input() call that waits for
the user to hit the enter key before exiting.
á You can get up to 10 more points
for correctly using try and except to protect against error conditions as
specified below:
o In the "open_file"
function you should trap the error condition associated with trying to open a
file that cannot be opened (such as trying to open a file in read mode when it
isn't there). If such an error is
encountered, you should print a succinct, informative error message (including
the name of the problematic file), and invoke sys.exit() to force a system exit.
o At the outermost level, you should
protect against a system exit being invoked by the code, printing a succinct
"Exiting" statement when this exception is trapped instead of
abruptly ending the program. This
would be somewhere before calling raw_input() to wait on the user to exit
normally.
á In the real test you would submit
"sieve.py" together with the previous section's "sum.py" to
Oncourse when
you were finished with both.
|
|
For more information, contact: |