Instructions:
Modify this notebook by adding the Python code described below.
Test your code using the menu item Cell ► Run All
Save the notebook (the .ipynb file) and upload it to the appropriate Assignment folder on the course web site.
Create the following variables:
myid
that is your BCIT ID. For example "A00123456".myid
For example, if your BCIT ID were A00123456 then myid
would equal "A00123456"
and mylist
would equal [6, 0.8333.., 'A00', True, 5+6j]
If the last digit of your student number is 0, replace it with a 1. For example, if you student number is A00123450, set myid
to "A00123451"
.
myid="A00123458"
mylist=[5, 5/8, "A00", True, float(myid[7])+float(myid[8])*1j]
myid,mylist
('A00123458', [5, 0.625, 'A00', True, (5+8j)])
# this code prints some variables that you will need to use in your solution
# do not modify this code!
import random
import math
if 'myid' not in locals():
print("myid not defined.")
else:
random.seed(int(myid[1:]))
labels=[]
k=random.randrange(4,5)
while len(labels)!=k:
digits=random.choices(list(range(10)),k=k)
labels=list(set([''.join(random.choices('ABCDXYZLMNP'+'0123456789',k=random.randrange(3,5)))
for i in range(k)]))
print(f"labels={labels}\ndigits={digits}")
labels=['P3M', 'CLB8', 'AMZB', '67NY'] digits=[1, 9, 7, 1]
Create a dict named mydict
where the keys are the strings in labels
and the values are the digits from digits
. For example, if:
labels=['6P05', '780C', 'BA8', 'ADBP']
and
digits=[0, 1, 0, 6]
then
mydict
would be { '6P05':0, '780C': 1, 'BA8': 0, 'ADBP':6 }
.
mydict=dict(zip(labels,digits))
print(mydict)
{'P3M': 1, 'CLB8': 9, 'AMZB': 7, '67NY': 1}
Create a set called odds
containing all of the odd values in digits
. For example if digits
is [0,1,3,3,4,5]
then odds
would be set to {1,3,5}
.
odds=set([i for i in digits if i%2==1])
digits,odds
([1, 9, 7, 1], {1, 7, 9})
# lab validation code; do not modify
def labcheck():
import copy, re
def q1():
b,a = [int(v) for v in myid[-2:]]
assert type(myid) == str and len(myid) == 9 and \
[int(myid[7]), b/a, myid[:3], not a%2, b+a*1j] == mylist, \
f"mylist = {mylist} for myid = {myid}"
def q2():
assert (type(mydict) == dict and
sorted(mydict.keys()) == sorted(labels) and
all([mydict[a] == b for a,b in zip(mydict.keys(), mydict.values())])), \
f"mydict = {mydict}"
def q3():
assert ( type(odds) == set and
all((i in odds for i in digits if i%2)) and not
any((i not in digits for i in odds))), \
f"odds = {odds} for digits = {digits}"
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}")
labcheck()
Question 1 OK. Question 2 OK. Question 3 OK.