Midterm exams

This is a "closed book" examination - in particular, you are not to use any resources outside of this notebook (except possibly pen and paper). You may consult help from within the notebook using ? but not any online references. You should turn wireless off or set your laptop in "Airplane" mode prior to taking the exam.

You have 2 hours to complete the exam.


In [1]:
%matplotlib inline

Q1 (10 points).

Given the 2 matrices

A = np.array([[1,2,3],[4,5,6]])
B = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

Perform matrix multiplication of A and B using the following methods:

  1. Using nested for loops without the dot function (4 points)
  2. Using numpy (2 points)
  3. Using R (start the first line of a new cell with %%R). You should pass in the A and B matrices defined in Python for full marks, but partial credit will be given if you redefine them in R (4 points)

In [ ]:

Q2 (10 points)

Read the data/iris.csv data set into a Pandas DataFrame, and answer the following questions:

  • Find the mean, min and max values of all four measurements (sepal.length, sepal.width, petal.length, petal.width) for each species
  • Find the average values of each measurement for rows where the petal.length is less than the sepal.width`

In [ ]:

Q3 (10 points)

Find the longest sequence of repeated letters (e.g. 'AAA') in the string below. Print 1) the length, 2) the index of the starting location, 3) the actual sequence. If there are ties, print the last sequence found. You can assume that only the letters A, C, T and G are found in the string.

TGTAGTCCATGCGGAATTCCACAGGGGCTCTGGGGACAGATTCGGACCTTTCTGTCAACGCCAATCATGGAGGTAGTGTGAGGTATAAATTTGGTCGGCGTAGGTCAAGAAAACCCACCTGCGCTGCTGTACGACACATGGCCGAGGCTTCAAGGGCATTCCACGAAGAGGCTCATGGCAACGCCTCTCGAAAGCTGGCGCTCAGGAAGGTACGATCACCCTCGAAATCAAAGATTTCATCTGAAATAAAAGTTAGTACGCCACTTTAGGGTATCGAGTACTTACCCATTTATAACGGAGGCTGAGCGAACGCTTGGCTGATGAAAAAACAACACTCGGTATAAACGGCGATTTCCACTGATCCAGGTAAAGCATGTTTGTGGATAGCAAGGGCAAGTAGTATGCAGCGAGTTTCGTGACAGTATAGCTCGACATGTATATCTCTGTGGGCGCATTTGGATGCTGTATACTGTAGAAGCAGTATATTCCCTGATGACCGAACTTACTACAAGTTGTTGTCTCGACAGGTAGTACGTGTGATCTGTGTCTGAGACCTGCAACTGGTGCGCATTGAAACTTCGTACATAAACCTACCGACTTCACCGTTTCGGCGTCGGCTTGTAACTGGAGAGTGTTGTTGCGTCATGGTCGATTGAGGATTTGGCCTAAATGTAGCGCGTATACACTGCATTATTAGCGGCTTCGAGGAACATGTAATGGGCGAGGACAGAGAATTGTATGAGATTCAAACTGCCAGGTTTTATGGCGGACCCCTGCTCCCATTGTAATCGACCGGCGGCTGGGGTACGCCCGCACGAGGGTATCGGTAGTATATCTAGCTAAGCTCCGGTGTATGCTGTTGAGACACCATTCATGCGCAAAGCCCCACCGTGCACGCATGCGATGATAAATAAGGATGACTATGGCTTACAGAGATCTTTTTCAGGGGCGTCTTGCAATAATGGTTGATAAATGTGTTTTGCCGAATCAACTGCGCGGC

In [ ]:

Q4 (10 points)

Euclid's algorithm for finding the greatest common divisor of two numbers is

gcd(a, 0) = a
gcd(a, b) = gcd(b, a modulo b)
  1. Write a function to find the greatest common divisor in Python (4 poinst)
  2. What is the greatest common divisor of 17384 and 1928? (1 point)
  3. Write a function to calculate the least common multiple (4 points)
  4. What is the least common multiple of 17384 and 1928? (1 point)

Note:

  • The greatest common divisor of two or more integers is the largest positive integer that is a divisor of both numbers
  • The least common multiple of two numbers is the smallest number (not zero) that is a multiple of both.

In [ ]:

Q5 (10 points)

Write a function to flatten a list of lists using

  1. For loops (2 points)
  2. List comprehensions (4 points)
  3. The reduce higher-order function (4 points)

For example,

flatten([[1,2], [3,4,5],[6,7,8,9]])

should return

[1,2,3,4,5,6,7,8,9]

In [ ]: