CSC 108H1 F 2009 Test 1 Duration - 35 Minutes Aids Allowed: None
CSC 108H1 F 2009 Test 1 Duration - 35 Minutes Aids Allowed: None
Duration 35 minutes
Aids allowed: none
Student Number:
Last Name: First Name:
Lecture Section: L0102 Instructor: Gries
Do not turn this page until you have received the signal to start.
(Please ll out the identication section above, write your name on the back
of the test, and read the instructions below.)
Good Luck!
This midterm consists of 3 questions on 8 pages (including this one). When
you receive the signal to start, please make sure that your copy is complete.
Comments and docstrings are not required except where indicated, although
they may help us mark your answers. They may also get you part marks if
you cant gure out how to write the code. No error checking is required:
assume all user input and all argument values are valid.
If you use any space for rough work, indicate clearly what you want marked.
# 1: / 4
# 2: / 6
# 3: / 8
TOTAL: /18
Total Pages = 8 Page 1 contd. . .
CSC108H1 F Test 1 Fall 2009
Question 1. [4 marks]
Suppose these functions have been dened:
import media
def do_something(left, right):
left = right
for pixel in left:
media.set_red(pixel, 0)
def do_stuff(a, b, c):
a = b + c
c = a + b
return a
The following code runs without errors. Fill in the boxes below to show the output printed or answer the
question, as indicated.
a = 145
one = 20
two = 25
one = do_stuff(a, one, two)
print a
output:
print one
output:
print two
output:
pic1 = media.load_picture(media.choose_file())
pic2 = media.load_picture(media.choose_file())
do_something(pic1, pic2)
media.show(pic1)
Does the picture that was just displayed have any red in it? Circle one: yes no
media.show(pic2)
Does the picture that was just displayed have any red in it? Circle one: yes no
Student #: Page 2 of 8 contd. . .
CSC108H1 F Test 1 Fall 2009
Question 2. [6 marks]
Write a function called rotate_rgb that takes a Picture as a parameter and, for every pixel, sets the
red value to the original green value, the green value to the original blue value, and the blue value to the
original red value.
You will need to use one variable for the parameter and one variable for the pixel values in the for loop.
You can earn the full 6 marks if you use only one more variable. If you use more than one, you can earn
at most 5 marks.
import media
Student #: Page 3 of 8 contd. . .
CSC108H1 F Test 1 Fall 2009
Question 3. [8 marks]
Write a program that uses raw_input to prompt the user for his or her income and then prints the amount
of health premium owed on that income. (A health premium is a kind of tax that is used to pay for health
coverage.) This program consists of two parts: a function named health_premium on this page and a main
block on the next page.
The health premium for a given income is computed as follows:
1. For less than $25,000 income, no premium is owed.
2. For incomes of $25,000 to less than $50,000, the premium is 5% of income.
3. For incomes of $50,000 or more, the premium on the rst $100,000 is 7%, and the premium on any
income above that is 10%. The total premium owed is the sum of those two values.
Your program should ask the user for input using the string Please enter your income: and print
the health premium owed as the string Your health premium is $XXX.YY. The amount printed should
be rounded to the nearest cent. XXX can be as many digits as it needs to be, YY must be 2 digits long,
and the total amount must be preceded by the character $.
def health_premium(income):
Return the health premium owed for a given income amount. The parameter
income is an int, and the health premium returned is a float.