# MAS212 Class Test 1 (2017)¶

## 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 8th Oct 2017. 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.

Key Resources:

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

## A. The Python language

1. Describe the three numeric data types in Python 3.

2. Describe two key differences between lists and arrays. (More specifically, between the list and ndarray types).

3. What does 'mutable' mean? Give three examples of data types that are mutable. (N.B. Common data types include int, list, tuple, dict, set, string, float).</b>

4. What is a Universal Function? (in the context of the numpy module)

5(a) What is "broadcasting"? (in the context of the numpy module).
(b) What is the shape of A*B, if the arrays A and B have shapes (1,3,4) and (2,5,3,1)?

## B. Python code

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

7. Find the following sum: $$\sum_{k=1}^{10} \frac{1}{k^2} = 1 + \frac{1}{4} + \frac{1}{9} + \ldots + \frac{1}{100}$$

8. Print the value of $\ln(2)$ to 9 significant figures.

9. Using the formula below, calculate Euler's number $e$ to an accuracy of 10 significant figures. $$e = \sum_{n=0}^{\infty} \frac{1}{n!}$$

10. Use the decimal package to display $e$ to 50 decimal places.

11. Write a function sumdigits(N) to compute the sum of the digits of a number. For example, sumdigits(1234) would return 10 (as 1+2+3+4=10).
Use the function to compute the sum of the digits in your registration number.

12. Apply the Newton-Raphson method $$x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}$$ to compute the largest root of $f(x) = x^3 - 5x - 1$ to 10 decimal places.

## C. String processing

In :
s = "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way. In short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only."
print(s)

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way. In short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.


13. How many times does the word 'it' appear in this paragraph? (N.B. disregard case, so count 'It' as well as 'it')

14. How many words are in this paragraph? How many different words?

15. The letter 'e'/'E' appears 69 times in this paragraph. Which letter is the next most common, and how many times does it appear?

## D. numpy module

In :
import numpy as np


16. Create a new array A containing the following data: $$A = \begin{bmatrix} 1 & 2 & 3 & \ldots & 10 \\ 2 & 3 & 4 & \ldots & 11 \\ \ldots \\ 5 & 6 & 7 & \ldots & 14 \end{bmatrix}$$

17. Find the matrix product A B where $$A = \begin{bmatrix} 5 & 6 & 2 \\ 1 & -1 & \frac{3}{4} \end{bmatrix} , \quad \quad B = \begin{bmatrix} 1 & 2 \\ 3 & -4 \\ 5 & 0 \end{bmatrix}$$

18. 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. Cryptography

In :
def scramble(s, keyword="aardvark"):
"""Scrambles a string."""
n = len(keyword)
s2 = ""
for k in range(len(s)):
c = s[k]
cnew = c
if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
k = (ord(c) + ord(keyword.upper()[k % n]) - 2*65) % 26
cnew = chr(65 + k)
elif c in 'abcdefghijklmnopqrstuvwxyz':
k = (ord(c) + ord(keyword.lower()[k % n]) - 2*97) % 26
cnew = chr(97 + k)
s2 = s2 + cnew
return s2

scramble("Hello everyone, blah blah blah.")

Out:
'Hecoj vferprie, blrk bckh sovh.'

19. Describe in your own words what the scramble function does.

20. Write a function unscramble to reverse its effect.

Use your function to decipher the following quote by a famous literary detective. From which novel is it taken?</b>

In :
txt = "Hon jfkon ydqe S srly ky yfx tykt nkzn iou kvvv elzpdnrded wce smpfvnisve, zcakovei medkinj, hfgevvu idzrosdwlv, mlvo so tyh tieth?"