MAS212 Class Test 1 (2018)

Aim:

  • To help you learn some Python essentials and programming skills to enjoy the remainder of this course.

How it works:

  • Complete the notebook below, as follows. Click on a question, then from the menu select Insert -> Insert Cell Below. From the dropdown box (next to the "stop" symbol) choose whether you wish to insert 'Code' or 'Markdown'. Save regularly!
  • Press Shift-Enter to execute the code in a cell.
  • Submit the .ipynb file via http://somas-uploads.shef.ac.uk/mas212 by 23.59pm on Sun 7th Oct 2018. If you are off-campus, you will need to use a VPN to submit.
  • Your lecturer will mark each question as either 2 (good), 1 (needs revision), 0 (not attempted/wrong).
  • This is an open-book test, which means you may consult books, notes and internet resources. Do not discuss or share your test with anyone. Copy-and-pasting is not permitted. Please give answers in your own words.
  • Some of these questions are relatively straightforward, and some are harder (e.g. Q10, 11, 13, 19, 20). Please don't spend any longer than a maximum of 3 hours on this test, as it is for a relatively small amount of credit.

Key Resources:

  • Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/


A. The Python language

(Please answer the following questions in "raw text" or "markdown" cells)

1. True or False? (i) Python is case-sensitive. (ii) Strings are mutable.

Statement (i) is True/False and statement (ii) is True/False [delete as applicable].

2. In your own words, describe the list and set containers, highlighting at least one similarity and one difference between them.

3. What is "vectorization"? What is a Universal Function, also known as a ufunc?

4. Broadcasting. Let A, B and C be arrays with shapes (3,3,4), (1,4) and (4,4), respectively. Which of the following products exist, under the rules of broadcasting?
(i) A\*A ; (ii) A\*B ; (iii) A\*C.


B. Python code

(Please answer the following questions in "code" cells).

5. Python as a calculator: Calculate $6 \times 2$, $(3^2 + 5^2)^{1/3}$, $(3 + 5i)^{3}$ and $\exp(-i\pi/4)$.

6. Find the following sum: $$\sum_{k=1}^{10} k^{-k} = 1^{-1} + 2^{-2} + 3^{-3} + \ldots + 10^{-10} .$$ Now print() your result to 12 decimal places.

7. Calculate and display $\ln(2)$ to 5 significant figures, by using a truncated version of the series $$ \ln(2) = 1 - 1/2 + 1/3 - 1/4 + 1/5 - \ldots $$

8. Write code to generate a list of the first 15 Triangular Numbers: [1,3,6,10,15, $\ldots$, 120]

9. Write code to save your name and registration number, on separate lines, into a file "me.txt".

10. Write a function pascal(n) to calculate the n-th row of Pascal's triangle as a list. For example, pascal(3) should return the list [1,3,3,1].
print() the 20th row.

11. Write code to implement the secant method to compute the largest root of $f(x) = x^3 - 5x - 1$ to 5 decimal places, starting with the initial values $x_0 = 2.1$ and $x_1 = 2.5$.


C. String processing

Please answer in "code" cells, and write code to address each question.

In [5]:
s = "A man, a plan, a canal - Panama!"
print(s)
A man, a plan, a canal - Panama!

12. Write code to count how many times the first letter of the alphabet appears in the string above (case insensitive: count both 'a' and 'A').

13. Write a function checkpalindrome(s) which returns True if the phrase in s is a palindrome, and False otherwise. Check your function works by using the string above. (Task: remove all punctuation and spaces from the string; change it to lowercase; then check whether the string is equal to its reverse).


D. numpy and linear algebra

(Please answer in "code" or "markup" cells).

In [22]:
import numpy as np
xs = np.array([0.868, 0.836, 0.410, 0.657, 0.071, 0.602, 0.806, 0.325, 0.872, 0.475])

14. Write code to find the mean average of data set xs (above) by using the formula $$\langle x\rangle> = \frac{1}{n} \sum_i x_i$$

15. Write code to find the variance of the data set xs by using the formula $$\text{var} = \langle x^2 \rangle - \langle x \rangle^2$$ [N.B. Do not use the functions xs.mean() or xs.var() in Q14 and Q15].

16. Let $A$ be the $2 \times 2$ matrix $$A = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}. $$ Use Python to calculate $A^{10}$ (i.e. $A$ multiplied by itself 10 times) using the rules of matrix multiplication.
Make a conjecture about the entries in $A^n$.

17. Find the eigenvalues of the real symmetric matrix $$ A = \begin{bmatrix} -1 & 2 & 3 \\ 2 & 4 & 1 \\ 3 & 1 & -3 \end{bmatrix} . $$ and verify that the sum of the eigenvalues is zero.


E. Polynomials

In [1]:
from numpy.polynomial import Polynomial
p = Polynomial([3,2,1])  # This is a quadratic polynomial: x^2 + 2x + 3.  
p**2 # p^2 is a quartic polynomial: (x^2 + 2x + 3)^2 = x^4 + 4x^3 + 10x^2 + 12x + 9
Out[1]:
Polynomial([  9.,  12.,  10.,   4.,   1.], [-1.,  1.], [-1.,  1.])

18. Use the Polynomial class (imported above) to find the roots of the quartic polynomial $$ p(x) = 2 x^4 + 5x^3 - 18x^2 - 27x + 54 . $$ Hint: p.roots()

19. Check that the polynomial $y(x) = 35 x^4 - 30 x^2 + 3$ is a solution of Legendre's differential equation for $n=4$, by substituting it into the left-hand side of the differential equation below, and showing that the LHS is zero. $$ \frac{d}{dx} \left[ (1 - x^2) \frac{d y}{d x} \right] + n(n+1) y = 0 . $$ (Hint: Use p.deriv() to take the derivative of a polynomial p.)


F. Cryptography

20. The name of a Fields Medallist has been scrambled using a One-Time Pad with a key made from the digits of $\pi$. Write code to unscramble the ciphertext to find a person's name.
The ciphertext is: PBVZFVOOWCFSQHWL.

(Hint: the first letter of the person's name is M: this is found by shifting the first letter of the ciphertext P back by 3 letters (3 is the first digit of $\pi$). The next letter should be shifted back by 1, the next by 4, and so on. You may find the functions chr() and ord() useful.)

In [ ]: