ELEX 4653 Lab 1¶

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.

Question 1¶

Create a list named mylist containing the following:

  1. a string that is your BCIT ID. For example "A00123456".
  2. an integer equal to the digits of your ID (everything following the A)
  3. a float equal to the integer above divided by 123
  4. a string equal to your BCIT ID with the characters in reverse order
  5. a bool that is True if the integer above is divisible by 3

For example, if your BCIT ID were A00123456 then mylist would equal ["A00123456", 123456, 1003.7073170731708, '65432100A', True]

In [1]:
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
Out[1]:
['A00123456', 123456, 1003.7073170731708, '65432100A', True]

Question 2¶

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 }.

In [2]:
myname="Betty"
mydict = { 'B':0, 'e':1, 't':2, 'y':4 }    
# mydict = {'B':0}
ydict = { 'B':0, 'e':1, 't':2, 'y':4 }
mydict
Out[2]:
{'B': 0, 'e': 1, 't': 2, 'y': 4}

Question 3¶

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 }.

In [3]:
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
Out[3]:
{(1+2j), (3+4j), (5+6j), 0j}
In [4]:
# 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.