ELEX 4653 Final Exam 2

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

Create a list y1 with 30 elements where the value of the i'th element is the sum of all integers from 0 to i. For example, the first four values are 0, 1, 3, 6, .... Hint: sum() returns the sum of its argument.

In [1]:
y1=[sum(range(i+1)) for i in range(30)]

Question 2

The following code sets the variable a to a list of strings and the variable b to a list of integers. Write Python code that creates a dictionary d where each entry's key is an element of a repeated the number of times given by the corresponding value of b and that entry's value is the corresponding value in b. For example, if a is ['aa', 'b', 'c'] and b is [2, 3, 5] then d should contain { 'aaaa':2, 'bbb':3, 'ccccc':5 }. Hint: using the multiplication operator on a string repeats it that number of times (for example, 'a'3 gives 'aaa')*

In [5]:
from random import randrange as rr
a=[''.join([chr(rr(97,123)) for j in range(rr(1,4))]) for i in range(rr(3,7))]
b=[rr(1,4) for i in range(len(a))]

print(a,b)
d={k*v:v for k,v in zip(a,b)}
print(d,len(d.keys()),len(a))
['ar', 'ei', 'ck', 'rsm', 'pyg'] [2, 1, 2, 1, 2]
{'arar': 2, 'ei': 1, 'ckck': 2, 'rsm': 1, 'pygpyg': 2} 5 5

Question 3

Write a function called minel(l) that takes a an argument l that is a list of 2-element tuples and returns a list consisting of the smallest element in each tuple. For example, if l=[ (1,2), (3,-1), (1,0)] the function would return [1, -1, 0].

In [3]:
def minel(l):
    return [min(t) for t in l]

Question 4

A set of linear equations expressed in matrix form: y=Ax can be solved by computing x=inv(A)*y where inv() represents a matrix inverse and means matrix multiplication. The code below defines the matrix A and the vector y. Compute x. Hint: the function numpy.linalg.inv(a) returns the inverse of a.*

In [4]:
import numpy as np
A=np.random.random((3,3))
y=np.random.random((3,1))

x=np.linalg.inv(A).dot(y)
print(A,y,x)
[[ 0.09670566  0.62388519  0.80565131]
 [ 0.97513913  0.19349528  0.9985903 ]
 [ 0.84216079  0.53801923  0.4831296 ]] [[ 0.1277247 ]
 [ 0.96104934]
 [ 0.52220081]] [[ 0.65551939]
 [-0.41753775]
 [ 0.40318668]]
In [5]:
# exam validation code; do not modify
def examcheck():   
    
    try:
        if all(i==a-b for i,a,b in zip(range(1,31),y1[1:],y1[0:-1])):
            print("Solution to question 1 may be correct.") 
    except:
        pass
        
    try:
        if len(d) == len(a) and all([k[0:(len(k)//v)] in a for k,v in d.items()]):
            print("Solution to question 2 may be correct.")
    except:
        pass
        
    try:
        import random
        l1=random.choices(range(10),k=10)
        l2=random.choices(range(10),k=10)
        if all(v<=l1[i] and v<=l2[i] for i,v in enumerate(minel(zip(l1,l2)))):
            print("Solution to question 3 may be correct.")
    except:
        pass
        
    try:
        import numpy as np
        if np.allclose(A.dot(x),y):
            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.