The lab provides practice creating built-in types.
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 a list named mylist
containing the following:
For example, if your BCIT ID were A00123456
then mylist would equal ["A00123456", 123456, 1003.7073170731708, '65432100A', True]
s='A00123456'
i=int(s[1:])
f=i/123
rs=s[::-1]
b=not bool(i%3)
# mylist=[s, i, f, rs, b]
mylist=[s, i, f, rs, b]
# mylist=[s, i, f, rs, not b]
mylist = ["A00123456", 123456, 1003.7073170731708, '65432100A', True]
mylist
['A00123456', 123456, 1003.7073170731708, '65432100A', True]
Create a string named myname
containing your first name. Use a variant that is three or more letters long.
Create a dictionary named mydict
that contains one entry for each unique letter of your name with the position in your name where that letter first occurs. For example if you name is 'Betty' then you would set mydict = { 'B':0, 'e':1, 't':2, 'y':4 }
.
myname="Betty"
mydict = { 'B':0, 'e':1, 't':2, 'y':4 }
# mydict = {'B':0}
ydict = { 'B':0, 'e':1, 't':2, 'y':4 }
mydict
{'B': 0, 'e': 1, 't': 2, 'y': 4}
Create a set of complex numbers, myset
where each element is equal to a+jb where a and b are successive digits from your BCIT ID.
For example if you ID is A00123456 then myset = { 0+0j, 1+2j, 3+4j, 5+6j }
.
myset = set([complex(float(s[i]),float(s[i+1])) for i in range(1,9,2)])
# myset = {}
myset = { 0+0j, 1+2j, 3+4j, 5+6j }
myset
{(1+2j), (3+4j), (5+6j), 0j}
# lab validation code; do not modify
def labcheck():
import re
def q1():
assert len(mylist) == 5, \
f"wrong number of values in mylist={mylist}"
s,i,f,rs,b = mylist
t=[type(x) for x in mylist]
assert(t == [str, int, float, str, bool]), \
f"mylist has types {t}"
assert(all(['A'+str("%08d"%i) == s,
abs(f*123 - i) < 0.1,
all([s[i] == rs[8-i] for i in range(8)]),
b == ((i//3)*3 - i == 0)])), \
f"mylist = {mylist}"
def q2():
assert len(myname) >= 3 and \
len(mydict) == len(set(myname)) and \
all([myname[mydict[c]] == c for c in set(myname)]), \
f"for myname='{myname}', mydict=f{mydict}"
def q3():
s = mylist[0]
assert not set([("%d"%(c.real),"%d"%(c.imag)) for c in myset]) ^ \
set(tuple(zip(s[1:-1:2],s[2::2]))), \
f"myset={myset} for mylist[0] = {s}"
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.