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 a list of numbers (x1
) and a list of
boolean (True or False) values (t1
). Write code to create a list y1
that has the same values as x1
except that they are negated if
the corresponding element of t1
is True. For example, if x1
is [1,-2,3] and t1
is [True, True, False] then y1
should
contain -1,2,3.
from random import randrange as rr
x1=[rr(0,20)-10 for _ in range(rr(6,10))]
t1=[bool(rr(0,2)) for _ in range(len(x1))]
y1=[-x if t else x for x,t in zip(x1,t1)]
x1,t1,y1
The following code sets the variable d2
to a dictionary where
the key and value are both single letters. Write python code that
creates a set x2
that contains the same number of elements as
d2
and where each member of the set is the concatenation of
a key and value from d2
. For example if d
is {'a':'x', 'z':'0'} then x2
should contain {"ax", "z0"}. Hints: d.items()
is an iterator that
yields the key,value tuples in d
and '+' concatenates two strings.
from random import randrange as rr
d2=dict(zip([chr(rr(97,123)) for _ in range(10)],[chr(rr(97,123)) for _ in range(10)]))
x2=set(k+v for k,v in d2.items())
d2,x2
Write a function called prod(x)
that takes a list argument x
and returns the product of the values in x
. For example, if x
= [1,3,4] it would return 12.
def prod(x):
return x[0] if len(x) == 1 else x[0]*prod(x[1:])
from random import randrange as rr
l=[rr(1,10) for _ in range(10)]
p=prod(l)
An IP address consists of four numbers of between one and three
digits separated by periods. For example, "10.0.0.1" or
"192.168.1.1". Set the variable ipre
to a regular expression
that matches an IP address. Hint: note that a period in a
regular expression matches any character.
ipre=r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
# exam validation code; do not modify
def examcheck():
from random import randrange as rr
import re
try:
if not any(x*y > 0 == (not t) for x,y,t in zip(x1,y1,t1)):
print("Solution to question 1 may be correct.")
except Exception as e:
print("In question 1:",e)
try:
if all(d2[v[0]] == v[1] for v in x2) and all(v[0] in [v[0] for v in x2] for v in d2.keys()):
print("Solution to question 2 may be correct.")
except Exception as e:
print("In question 2:",e)
try:
def unprod(p,l): return p if not l else unprod(p/l[0],l[1:])
l=[rr(1,10) for _ in range(10)]
if unprod(prod(l),l) == 1.0:
print("Solution to question 3 may be correct.")
except Exception as e:
print("In question 3:",e)
try:
if [len(re.findall(ipre,s)) for s in ['192.168.32.1', '10.0.0.1', '11111', '1.1.1']]==[1,1,0,0]:
print("Solution to question 4 may be correct.")
except Exception as e:
print("In question 4:",e)
examcheck()