{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"# Exercises"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"::: {note}\n",
"You don't need to worry about using Interactive Jupyter Notebooks for these exercises. You can simply ignore the rocket icon ({fa}`rocket`) and go ahead to answer the questions using the knowledge you've gained from the chapter. Have fun!\n",
"::: "
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [],
"source": [
"import json\n",
"from jupyterquiz import display_quiz\n",
"#load all questions json file\n",
"with open(\"01.json\", \"r\") as file:\n",
" questions = json.load(file)\n",
"\n",
"questions = [[q] for q in questions]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "su7ODKnqB7tv",
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"source": [
"## Exercise 2.1.1\n",
"\n",
"In the code cell bellow you can see a function that finds the greatest common divisor of any two numbers using the math
module. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"collapsed": true,
"id": "YjB37kkNGsr9",
"outputId": "36e77aac-9e26-4c63-fbf5-e030e5539ab1"
},
"outputs": [],
"source": [
"import math\n",
"\n",
"def find_greatest_common_divisor(a, b):\n",
" greatest_common_divisor = math.gcds(a, b)\n",
" return greatest_common_divisor\n",
"\n",
"print('The greatest common divisor is:', find_greatest_common_divisor(2, 4))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"
math
has no attribute gcds
\", \"correct\": true, \"feedback\": \"Correct.\"}, {\"answer\": \"There is no error in the code. \", \"correct\": false, \"feedback\": \"I hope not.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: scipy
to perform a 1D linear interpolation between a set of known points, where x_known
and y_known
are arrays with the measured $x$ and $y$ values. Use Google to look up the 1D interpolation function in scipy
.f
\", \"correct\": false, \"feedback\": \"I hope not.\"}, {\"answer\": \" x_predict
\", \"correct\": false, \"feedback\": \"I hope not.\"}, {\"answer\": \" f(x_predict)
\", \"correct\": true, \"feedback\": \"Correct.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: time
module. Use it to measure the working time of the cool_function()
below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"id": "C1aOizjOK7sl",
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"outputs": [],
"source": [
"# you do not need to change anything in this cell\n",
"def cool_function():\n",
" x = 0\n",
" for i in range(100000000):\n",
" x += 1\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Which of the following functions will give us the working time of the cool_function()
?"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"if
statements is filtering the data from errors and checking whether an error is within a certain limit.\n",
"\n",
"For example, checking whether the difference between an estimated value and the actual value are within a certain range.\n",
"\n",
"Mathematically speaking, this can be expressed as $|\\hat{y} - y| < \\epsilon$\n",
"\n",
"where $\\hat{y}$ is your estimated value, $y$ is the actual value and $\\epsilon$ is a certain error threshold.\n",
"\n",
"The function check_error_size()
below must do the same — it should return True
if the error is within the acceptable range eps
and False
if it is not."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"``` python \n",
"def check_error_size(estimated_value, true_value, eps):\n",
" if abs(estimated_value - true_value) <= eps:\n",
" return True\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"else
statement is missing.\", \"correct\": false, \"feedback\": \"Correct.\"}, {\"answer\": \"A else
and a return: True
statements are missing. \", \"correct\": true, \"feedback\": \"I hope not.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: if
statement to a more compact (and logical) statement. Your task is to rewrite the below if
statement by using the 'conditional expression' technique."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"id": "l8fBsW3ea-Ko",
"nbgrader": {
"grade": false,
"locked": true,
"solution": false
}
},
"outputs": [],
"source": [
"y = 0\n",
"x = 5\n",
"\n",
"if x % 2 == 0:\n",
" y = x ** 2\n",
"else:\n",
" y = x % 3"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"data": {
"text/html": [
"if x % 2 == 0 y = x ** 2 else y = x % 3
. \", \"correct\": false, \"feedback\": \"I hope not.\"}, {\"answer\": \"y = x ** 2 if x % 2 == 0 else x % 3
. \", \"correct\": true, \"feedback\": \"Correct.\"}, {\"answer\": \"x ** 2 if x % 2 == 0 else x % 3
.\", \"correct\": false, \"feedback\": \"I hope not.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: temp_fahrenheit
must be a list \", \"correct\": true, \"feedback\": \"Correct.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: sorted(unsorted_list)[0]
. \", \"correct\": false, \"feedback\": \"I hope not.\"}, {\"answer\": \"sorted(unsorted_list)[::-1]
. \", \"correct\": true, \"feedback\": \"Correct.\"}, {\"answer\": \"sorted(unsorted_list)[:-1]
.\", \"correct\": false, \"feedback\": \"I hope not.\"}]}];\n // Make a random ID\nfunction makeid(length) {\n var result = [];\n var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';\n var charactersLength = characters.length;\n for (var i = 0; i < length; i++) {\n result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));\n }\n return result.join('');\n}\n\n// Choose a random subset of an array. Can also be used to shuffle the array\nfunction getRandomSubarray(arr, size) {\n var shuffled = arr.slice(0), i = arr.length, temp, index;\n while (i--) {\n index = Math.floor((i + 1) * Math.random());\n temp = shuffled[index];\n shuffled[index] = shuffled[i];\n shuffled[i] = temp;\n }\n return shuffled.slice(0, size);\n}\n\nfunction printResponses(responsesContainer) {\n var responses=JSON.parse(responsesContainer.dataset.responses);\n var stringResponses='IMPORTANT!To preserve this answer sequence for submission, when you have finalized your answers: