Answer all four (4) questions.
You have 110 minutes to complete the exam.
This is an open-book exam. All written material is allowed.
No electronic devices other than a flash drive and the lab PC may be used during the exam.
No communication with others (electronic, verbal, written or otherwise) is allowed during the exam. Violations will result in a mark of zero and disciplinary action.
Add the Python code described below in the cells provided.
Test your code using the menu item Cell ► Run All
The message "Solution to question N may be correct." at the bottom of the notebook means that your answer to question N may be correct.
When the exam time is over, save the notebook (the .ipynb file) and upload it to the appropriate dropbox on the course web site.
The code below generates two lists (x1
and x2
) of numbers.
Write code to create a list y
that has the same number of
values as x1
and where each element is the sum of the
corresponding elements of x1
and x2
. For example, if x1
is
1,3,4 and x2
is 4,5,6 then y
should contain 5,8,10.
from random import randrange as rr
x1=[rr(10) for i in range(rr(4,10))]
x2=[rr(10) for i in range(len(x1))]
y=[a+b for a,b in zip(x1,x2)]
The following code sets the variable d
to a dictionary where
the key and value are both numbers. Write python code that
creates a dictionary d2
containing only those entries in d
where the product of the key and value are greater than 10. For
example if d
is {1:3, 4:7, 30:-1} then d2
should be
{4:7}. Hint: d.items() is an iterator that yields (key,value)
tuples for the entries in d
.
import random
d=dict(zip(random.sample(range(10),10),random.sample(range(10),10)))
d2 = {k:v for k,v in d.items() if k*v>10}
print(d,d2)
Write a function called pick(l,s)
that returns a list of the
items in the list l
that are included in the set s
. The
values in the returned list should be in the same order that they
appear in l
. For example, if l=[1,3,4,5] and s={5,2,4} the
function should return [4,5].
def pick(l,s):
return [i for i in l if i in s]
The ISO-8601 standard for writing dates consists of a 4-digit
year, a 2-digit month and a 2-digit day, each separated by
optional dashes. For example, "2017-05-18" or "19811231". Set
the variable isore
to a regular expression that matches an ISO
date string. Hint: ?
isore=r'\d{4}-?\d{2}-?\d{2}'
# exam validation code; do not modify
def examcheck():
from random import randrange as rr
import re
try:
if not any([a-b-c for a,b,c in zip(y,x1,x2)]):
print("Solution to question 1 may be correct.")
except:
pass
try:
if all([k*v>10 for k,v in d2.items()]) and \
all([k*d[k]<=10 for k in set(d.keys())-set(d2.keys())]):
print("Solution to question 2 may be correct.")
except:
pass
try:
l=random.choices(range(10),k=15)
s=random.sample(range(10),4)
p=pick(l,s)
if (set(l) & set(s)) == set(p):
print("Solution to question 3 may be correct.")
except:
pass
try:
if [len(re.findall(isore,s)) for s in ['19000101', '1200-10-30', '999-99-99', '']] \
== [1,1,0,0]:
print("Solution to question 4 may be correct.")
except:
pass
examcheck()