-
-
Save drvinceknight/bb4c676f76e3631afbf6732a17f8f6dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Solutions to mock class test\n", | |
"\n", | |
"# Question 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6050" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"nbrs = [] \n", | |
"i = 0\n", | |
"while len(nbrs) < 10: \n", | |
"# Using a while loop to keep track of how many numbers are in the list\n", | |
" i += 1 # Increment the number I am testing\n", | |
" if i % 10 == 0 and i % 11 == 0: # Test for divisibility\n", | |
" nbrs.append(i) # Put the number in the list\n", | |
"sum(nbrs) # Sum the number" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Question 2" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import math # Typo\n", | |
"def hyp(a, b): # Need two arguments\n", | |
" return math.sqrt(a ** 2 + b ** 2) # Fix the brack and missing exponent on a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"95.0" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"hyp(76, 57) # Test the required triangle" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Question 3" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Part a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def p(k):\n", | |
" \"\"\"Return the kth term of the summation\"\"\"\n", | |
" return ((2 ** k) * (math.factorial(k)) ** 2) / (math.factorial(2 * k + 1))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Part b" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def sum_n(n):\n", | |
" \"\"\"Return the sum of the first nth terms of the summation\"\"\"\n", | |
" return sum(p(k) for k in range(n + 1))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Part c" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 2.0\n", | |
"1 2.6666666666666665\n", | |
"2 2.933333333333333\n", | |
"3 3.0476190476190474\n", | |
"4 3.098412698412698\n" | |
] | |
} | |
], | |
"source": [ | |
"def approx_pi(n):\n", | |
" \"\"\"Return 2 times the first n terms of the summation\"\"\"\n", | |
" return 2 * sum_n(n)\n", | |
"\n", | |
"# Using a for loop get the required numbers\n", | |
"for n in range(5):\n", | |
" print(n, approx_pi(n))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Part d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"with open(\"pi.csv\", \"w\") as outfile:\n", | |
" for n in range(50):\n", | |
" outfile.write(str(approx_pi(n)) + \"\\n\")" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [default]", | |
"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.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment