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 [ ]:
 

Question 2

The following code sets the variable a to a list of strings and the variable b to a list of integers. Create 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 [4]:
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))]

Question 3

Write a function called minel(l) that takes 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 [ ]:
 

Question 4

A set of linear equations expressed in matrix form: y=Ax where y and x are (column) vectors 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 [5]:
import numpy as np
A=np.random.random((3,3))
y=np.random.random((3,1))
In [6]:
# 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()