ELEX 4653 Final Exam 3, May 2022¶

Instructions:¶

  • 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 Question n OK. at the bottom of the notebook means that no errors were found in your answer to question n.

  • When the exam time is over, save the notebook (the .ipynb file) and upload it to the appropriate Assignment folder on the course web site.

Question 1¶

Write a function named denoise(l) that takes a list l of floats and returns a list containing only the values in l whose absolute value is larger than or equal to 1. For example, if l=[1, 0, -1.01, 0.1, 2, -1e-6, 100, -1e-6] then denoise(l) would return [1, -1.01, 2, 100].

In [ ]:
 

Question 2¶

Write a function swap(l) that takes a list of two-element tuples and returns a list with the elements of each tuple in reverse order. For example, if l = [(1,2), (4,3), (5,6)] then swap(l) would return [(2, 1), (3, 4), (6, 5)].

In [ ]:
 

Question 3¶

Write a function named mixed(s) that takes a string s and returns the string s with the odd-numbered characters converted to lower-case and the even-numbered characters converted to upper case. For example, if s = "Quick Brown Fox." then mixed(s) would return "QuIcK BrOwN FoX.. Hints: you can index the characters of a string. The enumerate iterator.

In [ ]:
 

Question 4¶

Write a string named tube that is a regular expression that matches strings that consist of:

  • one or two digits
  • one or two upper-case letters (A through Z)
  • one digit

This regular expression will match, for example any of: '12AV7', '6V6', '0A2' but not any of: 'X7', '200V7', '127', '12AUG6', '12AV', '12AV30'.

In [ ]:
 
In [1]:
# exam validation code; do not modify
def examcheck():
    import copy, random, re, string, types
    from random import randint
    from random import randint, choices, shuffle, random
    
    def checkre(pat,ok,nok):
        for s in ok:
            assert re.fullmatch(pat,s), \
                f"pattern '{pat}'\n did not match string '{s}'"
        for s in nok:
            assert not re.fullmatch(pat,s), \
                f"pattern '{pat}'\n matched string '{s}'"  
            

    def randwords(n,chars=string.ascii_letters,nl=(2,5)):
        l = set()
        while len(l)<n:
            l |= set((''.join([chars[randint(0,len(chars)-1)] for i in range(randint(*nl))]),))
        return list(l)

    def q1(): # denoise(l1)
        import random
        n=randint(5,7)
        l=[random.gauss(0,2) for i in range(n)]
        ol = copy.deepcopy(l)
        r = denoise(l)
        weak = set(ol) - set(r)
        # print(l,r)
        assert all([abs(x)<1 for x in weak]) and all([abs(x)>1 for x in r]) and set(weak).union(set(r)) == set(ol), \
            f"dotproduct({ol1},{ol2}) returns {r}"
        
    def q2(): #swap
        n = randint(4,5)
        l = [(randint(0,10), randint(0,10)) for i in range(n)]
        ol = copy.deepcopy(l)
        r=swap(l)
        # print(len(r) == len(ol), all([a in ol[i] and b in ol[i] for i,(a,b) in enumerate(r)]) ,all([a<=b for a,b in r]))
        assert len(r) == len(ol) and all([a == ol[i][1] and b == ol[i][0] for i,(a,b) in enumerate(r)]), \
            f"swap({ol}) returns {r}"
        
    def q3(): #mixed
        s = randwords(1,"aeiouy  bcrst .-",(15,25))[0]
        os=copy.deepcopy(s)
        r = mixed(s)
        assert len(r) == len(os) and r.lower() == os.lower() and \
            all([not c.isalpha() or c.isupper() for c in r[0::2]]+[not c.isalpha() or c.islower() for c in r[1::2]]), \
            f"mixed({os}) returns {r}"
 
    def q4(): #tube
        ok = [randwords(1,string.digits,(1,2))[0]+
                randwords(1,string.ascii_uppercase,(2,2))[0]+
                randwords(1,string.digits,(1,1))[0] for i in range(randint(5,7))]
        nok = "X7 200V7 127 12AUG6 12AV 12AV30".split()
        checkre(tube,ok,nok)
        
    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}")    

examcheck()
Failed check for question 1: name 'denoise' is not defined
Failed check for question 2: name 'swap' is not defined
Failed check for question 3: name 'mixed' is not defined
Failed check for question 4: name 'tube' is not defined