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 strings.
Write code to create a list y
that has the same number of
values as x1
and where each element is the concatenation of the
corresponding elements of x1
and x2
. For example, if x1
is
"a","bc","d" and x2 is "1","e","" then y
should contain
"a1","bce","de".
from random import randrange as rr
x1=[''.join([chr(rr(97,123)) for j in range(rr(2,7))]) for i in range(rr(3,7))]
x2=[''.join([chr(rr(97,123)) for j in range(rr(2,7))]) for i in range(len(x1))]
y=[x1[i]+x2[i] for i in range(len(x1))]
The following code sets the variable a
to a list of numbers.
Write python code that creates a list b
of the same length
where each element of this list contains a 3-element tuples
containing the corresponding value from a
number, its square
and its cube. For example if a
is [1,2] then b
should be
[(1,1,1),(2,4,8)].
from random import randrange as rr
a=[rr(1,100) for i in range(rr(4,10))]
b=[(v,v**2,v**3) for v in a]
Write a function called minlen(a,b,c) that takes three list arguments and returns the length of the shortest list. For example, if a=[1,2], b=[2,3,4] and c=[1], minlen(a,b,c) would return 1.
def minlen(a,b,c):
return sorted([len(a), len(b), len(c)])[0]
Until 2014 BC license plate "numbers" were composed of
three letters followed by three digits (e.g. 'ABC123')
or three digits followed by three letters (e.g. '123ABC'). Set the
string licre
to a regular expression that matches either pattern of
license plate serial numbers. Assume all letters are upper-case and
there are no separators within the plate "number". Hint: the vertical
bar (|) can be used to separate two regular expressions, either
of which can match.
licre=r'([A-Z]{3}\d{3})|(\d{3}[A-Z]{3})'
# exam validation code; do not modify
def examcheck():
try:
if all([y[i][0:len(x)]==x1[i] for i,x in enumerate(x1)]+
[y[i][-len(x):]==x2[i] for i,x in enumerate(x2)]):
print("Solution to question 1 may be correct.")
except:
pass
try:
if all([a1<=b1<=c1 and a1*b1*c1 == a1**6 for a1,b1,c1 in b]):
print("Solution to question 2 may be correct.")
except:
pass
try:
from random import randrange as rr
li=[list(range(rr(1,8))) for i in range(3)]
t1=minlen(*li)
if any([len(li[i])==t1 for i in range(3)]) and \
all([len(li[i])>=t1 for i in range(3)]):
print("Solution to question 3 may be correct.")
except:
pass
try:
import re
ts=["ABC123", '123ABC', '12ABC', '111 AAA', 'A1', '']
if [len(re.findall(licre,s)) for s in ts] == [1,1,0,0,0,0]:
print("Solution to question 4 may be correct.")
except:
pass
examcheck()