{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MAS212 Class Test 1 (2017)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **Aim:** \n",
"\n",
" - To help you learn some Python essentials and programming skills 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'. Save regularly! \n",
" \n",
"
- Press Shift-Enter to execute the code in a cell.\n",
"\n",
"
- 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.*\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. Describe two key differences between lists and arrays. (More specifically, between the **`list`

and `ndarray`

types)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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`

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

module)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**5(a) What is \"broadcasting\"? (in the context of the **`numpy`

module).

\n",
" (b) What is the shape of `A*B`

, if the arrays `A`

and `B`

have shapes `(1,3,4)`

and `(2,5,3,1)`

?"
]
},
{
"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 $6 - 2$, $(3^2 + 5^2)^{1/3}$, $\\tan(\\pi/8)$ and $(3 + 5i)^{3}$.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**7. Find the following sum: $$\\sum_{k=1}^{10} \\frac{1}{k^2} = 1 + \\frac{1}{4} + \\frac{1}{9} + \\ldots + \\frac{1}{100}$$**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**8. Print the value of $\\ln(2)$ to 9 significant figures. **"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**9. Using the formula below, calculate Euler's number $e$ to an accuracy of 10 significant figures.**\n",
"$$\n",
"e = \\sum_{n=0}^{\\infty} \\frac{1}{n!}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**10. Use the **`decimal`

package to display $e$ to 50 decimal places."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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).

\n",
"Use the function to compute the sum of the digits in your registration number."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## C. String processing

\n",
"Please answer in \"code\" cells, and **write code to address each question**."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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.\n"
]
}
],
"source": [
"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.\"\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**13. How many times does the word 'it' appear in this paragraph? (N.B. disregard case, so count 'It' as well as 'it')**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**14. How many words are in this paragraph? How many different words?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**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?**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## D. numpy module

\n",
"(Please answer in \"code\" or \"markup\" cells)."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**16. Create a new array A containing the following data:\n",
"$$\n",
"A = \\begin{bmatrix} 1 & 2 & 3 & \\ldots & 10 \\\\ 2 & 3 & 4 & \\ldots & 11 \\\\ \\ldots \\\\ 5 & 6 & 7 & \\ldots & 14 \\end{bmatrix}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"****17. Find the matrix product **`A B`

where \n",
"$$\n",
"A = \\begin{bmatrix} 5 & 6 & 2 \\\\ 1 & -1 & \\frac{3}{4} \\end{bmatrix} , \\quad \\quad\n",
"B = \\begin{bmatrix} 1 & 2 \\\\ 3 & -4 \\\\ 5 & 0 \\end{bmatrix}\n",
"$$\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**18. Find the eigenvalues of the real symmetric matrix\n",
"$$\n",
"A = \\begin{bmatrix} -1 & 2 & 3 \\\\ 2 & 4 & 1 \\\\ 3 & 1 & -3 \\end{bmatrix} .\n",
"$$\n",
"and verify that the sum of the eigenvalues is zero.\n",
"**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"

\n",
"## E. Cryptography

"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Hecoj vferprie, blrk bckh sovh.'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def scramble(s, keyword=\"aardvark\"):\n",
" \"\"\"Scrambles a string.\"\"\"\n",
" n = len(keyword)\n",
" s2 = \"\"\n",
" for k in range(len(s)):\n",
" c = s[k]\n",
" cnew = c\n",
" if c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ': \n",
" k = (ord(c) + ord(keyword.upper()[k % n]) - 2*65) % 26\n",
" cnew = chr(65 + k)\n",
" elif c in 'abcdefghijklmnopqrstuvwxyz':\n",
" k = (ord(c) + ord(keyword.lower()[k % n]) - 2*97) % 26\n",
" cnew = chr(97 + k)\n",
" s2 = s2 + cnew\n",
" return s2\n",
"\n",
"scramble(\"Hello everyone, blah blah blah.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**19. Describe in your own words what the **`scramble`

function does."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**20. Write a function **`unscramble`

to reverse its effect. \n",
"\n",
"Use your function to decipher the following quote by a famous literary detective. From which novel is it taken?"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"txt = \"Hon jfkon ydqe S srly ky yfx tykt nkzn iou kvvv elzpdnrded wce smpfvnisve, zcakovei medkinj, hfgevvu idzrosdwlv, mlvo so tyh tieth?\""
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}