- 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/

**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].

`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`

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

`print()`

your result to 12 decimal places.

**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".**

`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 `20`

th row.

In [5]:

```
s = "A man, a plan, a canal - Panama!"
print(s)
```

**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).

`numpy`

and linear algebraIn [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])
```

`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].

Make a conjecture about the entries in $A^n$.

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

**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.)

**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.**

(

`chr()`

and `ord()`

useful.)In [ ]:

```
```