CSC 120H1S Practice Final Exam
Instructor: Elizabeth Patitsas; Term: Winter 2015
Last name:
First name:
Your utorid:
Student number:
•
•
•
•
•
•
•
•
•
You have 2 hours to write the exam.
You are not permitted any aids, notes, books, or calculators.
This exam is out of 50 points. 51 points are possible.
There are 6 one-point questions, 2 ten-point questions, and 5 five-point questions.
There are 16 pages in this booklet.
Remember to put your name on the back of the booklet.
As always, you are expected to adhere to the U of T student code of academic conduct.
We will deduct marks if we cannot read your writing. We have provided generous amounts of space.
You must get 35% or more on the exam to pass the course. Otherwise your final course grade will be
at most 47%.
• Good luck! :)
Grade earned
Total
1-6
6
7
5
8
5
9
5
10
5
11
5
12
10
13
10
Total
51
1
1
1-point questions. (6 points possible.)
Each question in this section is worth 1 point.
1. What will this code print?
amino = "T"
if amino == "A" or "C" or "T" or "G":
print("amino acid!")
else:
print("nope!")
2. Philip has the code below. How can he write it in six lines of code instead of 10?
if condition_one:
if condition_two:
call_function_A()
else:
call_function_B()
else:
if condition_two:
call_function_A()
else:
call_function_C()
2
3. What will this turtle draw?
turtle.Turtle()
for i in range(8):
turtle.left(45)
turtle.forward(30)
4. What will this code print?
planet_name = "Saturn"
print(planet_name[3:])
5. What will this code print?
planet_name = "Saturn"
planet_name[A] = "A"
print(planet_name)
6. What is the purpose of if
name
== ’ main ’?
3
2
5-point questions.
Each question in this section is worth 5 points.
7. Recall the Game of Life from Lab 4: we have an array of 1s (alive) and 0s (dead), representing a grid
of cells.
In a given iteration of the simulation:
(a) Any live cell with fewer than two live neighbours dies, as if by underpopulation.
(b) Any live cell with more than three live neighbours dies, as if by overcrowding.
(c) Any live cell with two or three live neighbours lives, unchanged, to the next generation.
(d) Any dead cell with exactly three live neighbours becomes a live cell.
Complete the function game of life that, given an array grid, and a number of iterations n. It will
run the Game of Life simulation on grid for n many iterations and return the resulting grid.
import numpy as np
def game_of_life(grid, n):
’’’(array, int) -> array
Run the Game of Life on grid for n iterations.
>>> X = np.array([[0, 0, 1], [1, 1, 1], [0, 1, 0])
>>> game_of_life(X, 1)
array([[ 0., 0., 1.],
[ 1., 0., 0.],
[ 1., 1., 1.]])
>>> game_of_life(X, 2)
array([[ 0., 0., 0.],
[ 1., 0., 1.],
[ 1., 1., 0.]])
’’’
4
8. This problem uses the toronto monthly temps.csv file that we’ve used in lecture and lab. It starts
like this:
year,month,high,avg,low,rain,snow
1937,11,8,3.3,-1.4,33.8,0
1937,12,0.1,-4.4,-8.8,7.6,33.8
1938,1,-2.6,-7.2,-11.7,13.5,26.9
1938,2,0.8,-3.7,-8.2,63.2,13.2
Complete the function rainfall which is given a filename and a threshold. This function returns
an array of the years where the summer rainfall was greater than threshold cm of precipitation,
according to the data in filename.
For our purposes, the summer is May, June, July and August (months 5-8). You may not use any
loops, if statements, or lists. You may find it helpful to know that there are 74 years worth of data
for summer months in toronto monthly temps.csv.
You may find it helpful to know that you can sum by an axis rather than the whole array: np.sum(arr,
axis=0) will give an array of that is the sum of each row; np.sum(arr, axis=1) will give the sums
of each column.
For example: a threshold of 400 cm should return [ 1986. 2008.]. A threshold of 390 cm should return
[ 1945. 1986. 1992. 2008. 2010.].
import numpy as np
def rainfall(filename, threshold):
’’’(str, num) -> array
Return the years where the rainfall in the data from filename
is greater than threshold cm.
>>> rainfall(’toronto_monthly_temps.csv’, 400)
array([ 1986. 2008.])
>>> rainfall(’toronto_monthly_temps.csv’, 390)
array([ 1945. 1986. 1992. 2008. 2010.])
’’’
data = np.genfromtxt(filename, delimiter=’,’, skip_header=True)
5
9. Recall that in assignment 3, we had lists of ballots. One type of ballot was the ranked ballot; for
example [’NDP’, ’GREEN’, ’LIBERAL’, ’CPC’] would mean this voter prefered the NDP the most,
followed by the Greens, then the Liberals, then the Conservatives. We wrote two functions for tallying
ranked ballots, tally borda and tally copeland.
In this problem, we’ll tall the votes with the Oklahoma primary electoral system:
(a) Count all the first choice ballots. Each first choice ballot is worth 1 point.
(b) If one party has a majority: we have a winner. Return how many points each party got.
(c) Count all the second choice ballots. For every party, add 1/2 points for each second choice ballot.
(d) If one party has a majority: we have a winner. Return how many points each party got.
(e) Count all the third choice ballots. For every party, add 1/3 points for each second choice ballot.
Return how many points each party got.
(The first test case in the docstring below is worked out more fully on page 14.)
PARTY_STRINGS = [’NDP’, ’GREEN’, ’LIBERAL’, ’CPC’]
def tally_oklahoma(ballots):
’’’(list) -> num, num, num, num
Return the how many points each party got under the Oklahoma primary system.
>>> bloc1 = [’NDP’, ’GREEN’, ’LIBERAL, ’CPC’]
>>> bloc2 = [’LIBERAL’, ’GREEN’, ’NDP’, ’CPC’]
>>> bloc3 = [’CPC’, ’NDP’, ’GREEN’, ’LIBERAL’]
>>> tally_oklahoma([5*[bloc1] + 6*[bloc2] + 2*[bloc3]])
(8, 6.166666666666667, 7.666666666666667, 2)
>>> tally_oklahoma([5*[bloc3]])
(5, 0, 0, 0)
>>> tally_oklahoma([6*[bloc1] + 6*[bloc3]])
(9, 3, 0, 6)
’’’
6
Use this page for any extra work you need. This page also features a worked example for the Oklahoma
system:
Example:
• 5 people in the riding vote [’NDP’, ’GREEN’, ’LIBERAL, ’CPC’].
• 6 vote [’LIBERAL’, ’GREEN’, ’NDP’, ’CPC’].
• 2 vote [’CPC’, ’NDP’, ’GREEN’, ’LIBERAL’]
This means that in our first count, there are 5 points for the NDP, 0 for the Greens, 6 for the Liberals,
and 2 for the CPC. The Liberals do not have a majority of the points, so now we count the secondchoices.
NDP now gets 5+(2/2)=6 points, Green gets 0+((5+6)/2)=5.5 points, Liberal gets 6+(0/2)=6 points,
and Conservative gets 2+(0/2)=2 points. We still do not have a party with a majority of points, so
we tally the third-choices.
NDP now gets 6+(6/3)=8, Green gets 5.5+(2/3)=6.166666666666667 points, Liberal gets 6+(5/3) =
7.666666666666667, Conservative gets 2+(0/3)=2.
The NDP has the most points, so they win. Here we should return how many points each party got:
(8, 6.166666666666667, 7.666666666666667, 2)
7
10. What will this code print out? Write your answer along the right side of the page.
def f1(s):
for elem in s:
if s:
print(’v: ’, elem)
def f2(s):
for elem in s:
if elem:
print(’v: ’, elem)
def f3(s):
if s:
for elem in s:
print(’v: ’, elem)
def f4(s):
if elem:
for elem in s:
print(’v: ’, elem)
if __name__ == ’__main__’:
my_list = [’apple’, 0, False, ’’, 42, True]
print(’fun1’)
f1(my_list)
print(’------’)
print(’fun2’)
f2(my_list)
print(’------’)
print(’fun3’)
f3(my_list)
print(’------’)
print(’fun4’)
f4(my_list)
8
11. I have
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
this code:
def g1(x):
# z = g2(x)
print(’in g1:’, x + y)
def g2(x):
y = 72
z = 2
return z
if __name__ == ’__main__’:
x = 5
y = 3
a = -1
b = -1
# a = g1(9)
# b = g2(1)
print(’x:’, x)
print(’y:’, y)
# print(’z:’, z)
print(’a:’, a)
print(’b:’, b)
(a) What will this program print out to begin with? If it has an error, what type and why?
(b) Let’s say I uncomment line 15. What will the whole program print out now? If there’s an error,
what type and why?
(c) Let’s say I uncomment lines 15 and 1. What will the whole program print out now? If there’s
an error, what type and why?
(d) Now I uncomment line 16 (lines 15 and 1 are commented again). What will the whole program
print out now? If there’s an error, what type and why?
(e) And finally I uncomment lines 16 and 20. What will the whole program print out now? If there’s
an error, what type and why?
9
3
10-point questions.
12. Using the Function Design Recipe, write a docstring for function num vowels (you will implement
the function on the next page). It is given a parameter text; it will return the number of vowels in
text. We will treat ‘y’ as a consonant for this problem. The code you write should never throw any
exceptions; if given incompatible input it should return 0.
Your test cases should cover as many different cases as possible. For full credit, you’ll want to write
at least six test cases.
This specification may not tell you what to do with every case you may want to consider. For any such
cases, make a decision about how you think the code should behave and write that into the docstring.
(Make sure your code on the next page is consistent with your decision(s)!)
def num_vowels(text):
’’’
10
Write your code here:
def num_vowels(text):
’’’
Imagine your docstring from the previous page were here.
’’’
11
13. Recall from Lab 2 that you saw the Heliocentric longitude (lM ) part of the Mars24 algorithm, which
is given as:
lM = Ls + 85.061◦ − 0.015◦ sin(71◦ + 2Ls ) − 5.5◦ × 10−6 ∆tJ2000
For this problem, first write a docstring for the heliocentric longitude function. You will want to
include at least six test cases, and they should be as comprehensive as possible.
The NASA website has some worked examples, that note:
• For Ls = 277.18677◦ , and ∆tJ2000 = 4.50074, lM = 2.26270◦ .
• For Ls = 327.32322◦ , and ∆tJ2000 = 1463.07471, lM = 52.37469◦ .
Not all possible values for the arguments make sense from a physics perspective:
• Ls should between 0 and 360 inclusive, as it is in degrees. If Ls is not in this range, the program
should raise a ValueError with the message that ‘L s should be between 0 and 360’.
• ∆tJ2000 should be greater than zero, and finite. If it isn’t, the program should raise a ValueError
with the message that ‘Dt J2000 should be >0 and finite’
import math
import numpy as np
BAD_LS = "L_s should be between 0 and 360"
BAD_DELTAT = "Dt_J2000 should be >0 and finite"
def heliocentric_longitude(L_s, Dt_J2000):
’’’
12
Write your code here:
import math
BAD_LS = "L_s should be between 0 and 360"
BAD_DELTAT = "Dt_J2000 should be >0 and finite"
def heliocentric_longitude(L_s, Dt_J2000):
’’’
Imagine your docstring from the previous page were here.
’’’
13
There are no more questions in this exam. Use this page for any extra work you need.
14
There are no more questions in this exam. Use this page for any extra work you need.
15
.
Last name:
First name:
Your utorid:
16
© Copyright 2026 Paperzz