{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MAS212 Class Test 1 (2016)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **Aim:** \n",
"\n",
" - To help you learn some essentials of Python to enjoy the remainder of this course.\n",
"

\n",
"\n",
"**How it works:**\n",
"\n",
" - 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' (i.e. text). Save regularly! \n",
" \n",
"
- Press Shift-Enter to execute the code in a cell.\n",
"\n",
"
- Submit via http://somas-uploads.shef.ac.uk/mas212 by 5pm on Mon 10th Oct.
*If you are off-campus, you will need to use a VPN to submit.*\n",
"\n",
" - Your lecturer will mark each question as either 2 (good), 1 (needs revision), 0 (not attempted/wrong).\n",
"\n",
"
- 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.*\n",
"

\n",
"\n",
"**Key Resources:**\n",
"\n",
" - Lecture materials and links on course web page: http://sam-dolan.staff.shef.ac.uk/mas212/\n",
"

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## A. The Python language

\n",
"(Please answer the following questions in \"raw text\" or \"markdown\" cells)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**1. Describe the three numeric data types in Python 3. **"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**2. Give three examples of data types which are ***mutable*. (N.B. Common data types include `int, list, tuple, dict, set, string, float`

)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**3. Describe two key differences between lists and arrays. (More specifically, between the **`list`

and `ndarray`

types)."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"**4. What is a Universal Function? (in the context of the **`numpy`

module)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5. What is \"vectorization\"?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## B. Python code

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Please answer the following questions in \"code\" cells)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**6. Python as a calculator: Calculate $3 \\times 5$, $\\ln(3)$, $\\exp(i \\pi / 4)$ and $\\frac{1+\\sqrt{3}i}{1-\\sqrt{3}i}$, where $i$ is the unit imaginary number.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**7. Use numpy to find the following sum: $$\\sum_{k=1}^{1000} \\frac{1}{k^4} = 1 + \\frac{1}{16} + \\frac{1}{81} + \\ldots + \\frac{1}{10^{12}}$$**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**8. Use the **`decimal`

package to find the value of $\\ln(2)$ to 30 decimal places. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**9. The Euler-Mascheroni constant $\\gamma$ is defined by**\n",
"$$\n",
"\\gamma = \\lim_{n \\rightarrow \\infty} \\left(-\\ln(n) + \\sum_{k=1}^n \\frac{1}{k} \\right)\n",
"$$\n",
"**Calculate $\\gamma$ to an accuracy of 5 decimal places.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**10. Write a function to find the product set $A \\times B$, that is, the Cartesian product of two sets $A$ and $B$. Test your function by finding the product of the sets $A = \\{1,2,3\\}$ and $B = \\{4, 5\\}$**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## C. Continued fractions

\n",
"Any positive real number $r$ can be written in the form of a continued fraction:\n",
"$$ r = a_0+\\cfrac{1}{a_1+\\cfrac{1}{a_2+\\cfrac{1}{a_3+\\cdots}}} $$\n",
"where $a_k \\in \\mathbb{N}$. \n",
"\n",
"If $r$ is rational then the sequence of coefficients $[a_0 ; a_1, a_2, \\ldots]$ is finite. Conversely, if $r$ is irrational then the sequence is infinite. \n",
"\n",
"To calculate the continued fraction representation $[a_0 ; a_1, a_2, \\ldots]$ of $r$, use the following approach. First, take the 'floor' (the integer part) of $r$; this is $a_0$. Now subtract the integer part from $r$ to get a real number in the interval $[0, 1)$. If it is zero, stop. Otherwise, take its reciprocal to get a number in the range $(1, \\infty)$. Take its floor; this is $a_1$. Now subtract the floor. If the remainder is zero, stop. Otherwise, take its reciprocal and continue in this fashion. More detail here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**11. Write a function which takes a real number $r$ and returns a list of the first few coefficients of the continued fraction $[a_0, a_1, \\ldots a_n]$ up to $n=6$, say. Test your function by confirming that the representation of $\\pi$ is $[3; 7, 15, 1, 292, 1, \\ldots ]$\n",
"**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**12. Compute the real number with the continued fraction sequence $[2;4,4,4,4,4,4\\ldots]$ to an accuracy of at least 8 decimal places**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## D. Strings

\n",
"(Please answer in \"code\" cells)."
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s = \"All happy families are alike; each unhappy family is unhappy in its own way\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**13. Open a new file \"xxx.txt\", where xxx is your registration number. Write this famous opening line (the string **`s`

) into the file, putting just one word on each line, like this:

\n",
"All

\n",
"happy

\n",
"families

\n",
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**14. How many words are in this opening sentence? How many distinct words?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**15. Write code to invert the meaning of the sentence, by swapping \"happy\" and \"unhappy\". Print out the new sentence.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## E. `numpy`

and linear algebra

\n",
"(Please answer in \"code\" cells)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**16. Make a 2D array of dimension $3 \\times 4$ containing the integers 1, 2, ... 12**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**17. Make an array of 1000 random numbers drawn from the standard uniform distribution on the interval [0,1) (see lecture 2).**

Calculate the standard deviation $\\sigma$ of the data, and verify that it is approximately equal to $\\frac{1}{2 \\sqrt{3}}$."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"**18.** A square matrix $N$ is said to be **nilpotent** if $N^k = 0$ for some $k > 1$. **Demonstrate that the following matrix is nilpotent:**\n",
"$$\n",
"A = \\begin{bmatrix} 2 & 2 & -2 \\\\ 5 & 1 & -3 \\\\ 1 & 5 & -3 \\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"A = np.matrix(\"2,2,-2 ; 5,1,-3 ; 1,5,-3\") # This is a quick-and-easy way of defining a matrix"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**19. Using **`np.linalg.eig(A)`

, find the eigenvalues of the real symmetric matrix\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 & 3 \\\\ 2 & 4 & 1 \\\\ 3 & 1 & -5 \\end{bmatrix} .\n",
"$$\n",
"and verify that the sum of the eigenvalues is zero.\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## F. Cryptography

"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**20. A musician has encrypted the notes of a famous melody using a Caesar shift. Write code to decrypt **`ciphertext`

using `keytext`

, to recover the melody as a string of letters, A to G.

From what piece of music is this melody taken? (*this part not assessed*)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ciphertext = \"SYFOMNHALYDMKVKJPUYUOBZDIJKSSGSVYRQHWNNERLPGPPWVYWDMHNJETVVZIM\"\n",
"keytext = \"MGASUSXDREASUITVPLIMRDEZUUULKWLIGLNXJRPZNUPXNOKJGJDUYRUYJIJEUQ\""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}