ELEX 4653 Lab 1

This lab gives you practice with basic Python syntax, editing a notebook and uploading it to the web site.

Version 2 (April 24, 2018)


Instructions:

  • Modify this notebook by adding the Python code described below.

  • Test your code using the menu item Cell ► Run All

  • When there are no errors submit the notebook (the .ipynb file) to the appropriate dropbox on the course web site.


Make the following two changes to the cell below:

  • add a comment containing your name and the date,
  • add the following if-statement inside the while-loop so that only odd numbers are printed:
    • if (i%2):
In [1]:
i=1
while i < 16:
    if i % 2:
        print(i,end=", ")
    i+=1
1, 3, 5, 7, 9, 11, 13, 15, 

Set the variable myid to a string equal to your BCIT ID (starting with "A0...").

In [2]:
myid="A00123456"

Set the variable mylist to a list containing the 8 digits of your BCIT ID as integers. The list should have 8 values.

In [3]:
mylist=[0,0,1,2,3,4,5,6]

Use a while loop to find the sum of all even digits of your student number (as stored in the list mylist). Use a variable mysum that is initialized to zero and updated, if necessary, each time through the loop.

In [4]:
mysum=0
i=0
while (i<8):
    if (mylist[i]%2 == 0):
        mysum+=mylist[i]
    i+=1

Create a dictionary named roman where the key is one of the lower-case roman numberals (i, v, x, l, c, d, m) and the value is the corresponding value (1, 5, 10, 50, 100, 500 and 1000 respectively).

In [5]:
roman={'i':1,'v':5,'x':10,'l':50, 'c':100, 'd':500, 'm':1000}

Insert code in the function definition given below that computes the numeric value of the string s which is formatted as a roman numeral. Assume the simple "additive" format for roman numerals where the position of a letter does not affect its value. For example, the strings "iv" and "vi" both have the value 6.

In [6]:
def unroman(s):
    # insert code here to compute the value of 'n' from the string 's'
    i=0
    n=0
    while (i<len(s)):
        n+=roman[s[i]]
        i+=1
    return n
In [7]:
# lab validation code; do not modify
def labcheck():
    import re, functools, random
    assert re.match(r'[aA]0\d{5}',myid), "bad format for BCIT ID"
    assert len(mylist) == 8, "mylist is wrong length"
    assert mylist == [int(c) for c in mylist], "mylist has wrong values"
    assert mysum == functools.reduce(lambda x, y: x+(not y%2 and y), mylist), \
        "wrong value for mysum: {}".format(mysum)
    try:
        assert len(roman) == 7
        assert [roman[s] for s in 'ivxlcdm'] == [1,5,10,50,100,500,1000]
    except:
        print("dictionary 'roman' has wrong values")
        return
    try:
        s="ivxlcdm"
        s=str(''.join(random.sample(s,k=len(s))))
        assert unroman(s) == 1666
    except:
        print("failed to convert {} to a number".format(s))
        return
    print("All tests passed -- you may save and submit this lab.")

labcheck()
All tests passed -- you may save and submit this lab.