ELEX 4653 Sample Final Exam 2

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

Create a list y with 20 elements where the value of the i'th element is equal to 20*i + 3. The values of i should range from 0 to 19.

In [6]:
y=[20*i+3 for i in range(20)]

Question 2

The following code sets the variable s to a set of numbers. Create a set t consisting of all elements of s that are between 100 and 200 (inclusive).

In [7]:
import random
s={n for n in [random.randrange(300) for i in range(30)]}

t={i for i in s if i>=100 and i <=200}

Question 3

Write a function called minvowel(s) that takes one string argument(s) and returns the smallest vowel in the string.
The ''smallness'' of a character should be determined by Python's comparison operators. Hint: use in.

In [8]:
def minvowel(s):
    return sorted([c for c in s if c in 'aeiou'])[0]

Question 4

Import the numpy package using the alias np. Write a function matpow(m,n) that returns the results of multiplying a numpy array m by itself n times. Do not use the ** operator.

In [9]:
import numpy as np

def matpow(m,n):
    p = m
    for i in range(n):
        p = p.dot(m)
    return p
In [10]:
# exam validation code; do not modify
def examcheck():
    import random, numpy as np
    
    try:
        if [(i-3)//20 for i in y] == list(range(20)):
            print("Solution to question 1 may be correct.")  
    except:
        pass
        
    try:
        if t == (s - {i for i in s if i<100} - {i for i in s if i>200}):
            print("Solution to question 2 may be correct.")
    except:
        pass
        
    try:
        if minvowel("biefg") == 'e':
            print("Solution to question 3 may be correct.")
    except:
        pass
        
    try:
        if (matpow(2*np.eye(3),3) == 16*np.eye(3)).all():
            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.