ELEX 4653 Sample Final Exam 1

Sample final exam. Version 1.

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 "Solution to question N may be correct." at the bottom of the notebook means that your answer to question N may be correct.

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

Question 1

The following code sets the variable x to a list of random integers. Add Python code that creates a list y containing all the values of x that are even numbers (in the same order). Hint: the expression n % 2 is 1 (True) if n is odd and 0 (False) if n is even.

In [16]:
import random
x=[random.randrange(100) for i in range(20)]

y=[v for v in x if v%2 == 0]
    
y
Out[16]:
[72, 10, 50, 84, 30, 70, 66, 6, 38, 78, 94, 50, 42]

Question 2

The following code sets the variable s to a set of strings. Write Python code that creates a dictionary t containing one entry for each value in s. The key for each entry should be the string and the value for that entry should be the length of the key.

In [20]:
import random
s={("abcdefg")[0:random.randrange(1,6)] for i in range(10)}

t={}
for v in s:
    print(v)
    t[v]=len(v)
    print(t[v])
ab
2
abcd
4
a
1
abc
3

Question 3

Write a function addrev(l) that takes a list argument, l, and returns a list composed of l followed by a reversed copy of l.

In [35]:
def addrev(l):
    newlist=list()
    
    for i in l:
        newlist.append(i)
        
    #print(newlist)
    
    for i in range(len(l)):
        #print(-i-1,newlist[-i-1])
        newlist.append(l[-i-1])
        
    #print(newlist)
    return newlist

addrev(['a',2,8])
Out[35]:
['a', 2, 8, 8, 2, 'a']

Question 4

A UK postcode consists of a sequence one to four alphanumeric characters, a space, one digit and two upper-case letters. For example, "EC1A 1BB". Set the variable pcre to a string containing a regular expression that matches UK postcodes.

In [37]:
pcre='[A-Z0-9]{1,4} \d[A-Z][A-Z]'
In [38]:
# exam validation code; do not modify
def examcheck():
    import random, re
    
    try:
        if not {v for v in x if v&1 == 0}.difference(y):
            print("Solution to question 1 may be correct.")
    except:
        pass
        
    try:
        if not {k for k,v in t.items() if v == len(k)}.difference(set(s)):
            print("Solution to question 2 may be correct.")
    except:
        pass

    try:
        nv=random.randrange(5,10)
        r = [random.randrange(10) for i in range(nv)]
        v = addrev(r)
        if all([v[i] == v[-i-1] and v[i] == r[i] for i in range(nv)]):
            print("Solution to question 3 may be correct.")
    except:
        pass
        
    try:
        if ( re.findall(pcre,"SW1A 1AA SW1A,1AA STHL 1ZZ  NR8 1ZZ  123 123 A 0Aa") ==
                ['SW1A 1AA', 'STHL 1ZZ', 'NR8 1ZZ']):
            print("Solution to question 4 may be correct.")
    except:
        pass
    

examcheck()
Solution to question 1 may be correct.
Solution to question 2 may be correct.
Solution to question 3 may be correct.
Solution to question 4 may be correct.
In [ ]: