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 Question n OK. at the bottom of the notebook means that no errors were found in your answer to question n.
When the exam time is over, save the notebook (the .ipynb file) and upload it to the appropriate Assignment folder on the course web site.
Write a function named dotprod(l1,l2)
that takes two lists of integers of the same length and returns a list where each element made up by multiplying the corresponding elements of the two arguments. For example, if l1 = [ 1, 2, 3]
and l2 = [ 4, 5, 6]
then dotprod(l1,l2)
would return [4, 10, 18]
.
Write a function listorder(l)
that takes a list of two-element tuples and returns the same list with the elements of each tuple ordered with the smallest element first. For example, if l = [(1,2), (4,3), (5,6)]
then listorder(l)
would return [[1, 2], [3,4], [5,6]]
.
Write a function named dizzy(s)
that takes a string s
and returns the string s
with all vowels (a,e,i,o,u and y) converted to upper case. For example, if s = "Quick Brown Fox."
then dizzy(l)
would return "QuIck BrOwn FOx.
. Hint: You can iterate over the characters of a string.
Write a string named part
that is a regular expression that matches strings that consist of:
This regular expression will match, for example any of: 'EP4', 'EPM570', 'EPM240'
but not any of: 'E240', 'EPMX240', 'EP', 'EPM', 'EPC240', 'EP1234'
.
# exam validation code; do not modify
def examcheck():
import copy, random, re, string, types
from random import randint
from random import randint, choices, shuffle, random
def checkre(pat,ok,nok):
for s in ok:
assert re.fullmatch(pat,s), \
f"pattern '{pat}'\n did not match string '{s}'"
for s in nok:
assert not re.fullmatch(pat,s), \
f"pattern '{pat}'\n matched string '{s}'"
def randwords(n,chars=string.ascii_letters,nl=(2,5)):
l = set()
while len(l)<n:
l |= set((''.join([chars[randint(0,len(chars)-1)] for i in range(randint(*nl))]),))
return list(l)
def q1(): # dotprod(l1,l2)
n=randint(5,7)
l1=[randint(3,10) for i in range(n)]
l2=[randint(3,10) for i in range(n)]
ol1, ol2 = copy.deepcopy(l1), copy.deepcopy(l2)
r = dotprod(l1,l2)
#print(l1,l2,r)
assert len(r) == len(ol1) and all([n/ol1[i] == ol2[i] for i,n in enumerate(r)]), \
f"dotproduct({ol1},{ol2}) returns {r}"
def q2(): #listorder
n = randint(4,5)
l = [(randint(0,10), randint(0,10)) for i in range(n)]
ol = copy.deepcopy(l)
r=listorder(l)
# print(len(r) == len(ol), all([a in ol[i] and b in ol[i] for i,(a,b) in enumerate(r)]) ,all([a<=b for a,b in r]))
assert len(r) == len(ol) and all([a in ol[i] and b in ol[i] for i,(a,b) in enumerate(r)]) and \
all([a<=b for a,b in r]), \
f"listorder({ol}) returns {r}"
def q3(): #dizzy
s = randwords(1,"aeiouy bcrst .-",(15,25))[0]
os=copy.deepcopy(s)
r = dizzy(s)
assert len(s) == len(os) and s.lower() == os.lower() and \
all([c not in 'aeiouy' or c.isupper() for c in r]), \
f"dizzy({os}) returns {r}"
def q4(): #part
ok = ["EP"+(randwords(1,"M",(1,1))[0] if randint(0,1) else '')+
randwords(1,string.digits,(1,3))[0] for i in range(randint(5,7))]
nok = "E240 EPMX240 EP EPP EPM EPC240 EP1234".split()
checkre(part,ok,nok)
for s,i in [(s,s[1:]) for s in locals().keys() if re.search(r'q\d+',s)]:
try:
locals()[s]()
print(f"Question {i} OK.")
except Exception as e:
print(f"Failed check for question {i}: {e}")
examcheck()
Failed check for question 1: name 'dotprod' is not defined Failed check for question 2: name 'listorder' is not defined Failed check for question 3: name 'dizzy' is not defined Failed check for question 4: name 'part' is not defined