Added Files, Changed Folder Names, AND MUCH MORE!
++ Week 5 Folder ++ Week 5 Homework +- Week 2 Folder Name
This commit is contained in:
parent
ee3222c4ab
commit
4fc499169b
13 changed files with 538 additions and 0 deletions
73
Week-5/01_basic_names.ipynb
Normal file
73
Week-5/01_basic_names.ipynb
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Names in Python"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** A famous fact in mathematics is that\n",
|
||||
"\n",
|
||||
"$$(1 + \\frac{1}{n})^{n}$$\n",
|
||||
"\n",
|
||||
"gets very close to the number $e$, which is roughly $2.718$, when $n$ is large. Use this fact to compute an approximation to $e$, called `roughly_e`. Your approximation should be correct to at least 5 decimal places."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"roughly_e = ...\n",
|
||||
"roughly_e"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** In the previous question, you might have noticed that you had to type in the value of $n$ twice when you tried different values (once as `1/n` and once as `**n`). You might have even accidentally used 2 different values.\n",
|
||||
"\n",
|
||||
"Redo the code in the cell below, giving your estimate of $e$ the name `also_roughly_e`. Instead of writing the value of $n$ twice, give it the name `n` in your Python code, and then use that name in your expression for `also_roughly_e`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n = ...\n",
|
||||
"also_roughly_e = ...\n",
|
||||
"also_roughly_e"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
75
Week-5/02_confusing_names.ipynb
Normal file
75
Week-5/02_confusing_names.ipynb
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Confusing Names"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** Some physicists and computer scientists were partying late one night, and they decided to figure out roughly how much beer it would take to cover the whole surface of the Sun 3 meters deep. They found the following facts:\n",
|
||||
"\n",
|
||||
"1. The average density (mass per volume) of the Sun is 1410 $\\texttt{kg}/\\texttt{m}^3$.\n",
|
||||
"2. The Sun weighs $1.989 \\times 10^{30} \\texttt{kg}$.\n",
|
||||
"3. The Sun's volume is its weight divided by its density.\n",
|
||||
"4. The Sun is roughly a sphere.\n",
|
||||
"5. The radius of a sphere is $(\\texttt{volume} \\times \\frac{3}{4 \\times \\pi})^{1/3}$\n",
|
||||
"6. $\\pi$ is roughly 3.14159.\n",
|
||||
"7. The surface area of a sphere is $4 \\times \\pi \\times \\texttt{radius}^2$.\n",
|
||||
"\n",
|
||||
"They wrote code in the next cell to compute the answer. But the next morning, they couldn't figure out what their code did, because none of the names made sense. To help clarify their code, replace the names used in the next cell with appropriate names chosen from the following list:\n",
|
||||
"\n",
|
||||
"* `required_beer_volume`\n",
|
||||
"* `sun_radius`\n",
|
||||
"* `sun_volume`\n",
|
||||
"* `sun_surface_area`\n",
|
||||
"* `sun_density`\n",
|
||||
"* `pi`\n",
|
||||
"* `sun_weight`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a = 1410\n",
|
||||
"b = 1.989 * 10**30\n",
|
||||
"c = 3\n",
|
||||
"d = b / a\n",
|
||||
"e = 3.14159\n",
|
||||
"f = (d * 3/(4*e))**(1/3)\n",
|
||||
"g = 4 * e * f**2\n",
|
||||
"h = c * g"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.7.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
79
Week-5/03_naming_errors.ipynb
Normal file
79
Week-5/03_naming_errors.ipynb
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Errors in Naming"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** When you run the following cell, Python will produce an slightly-cryptic error message. Explain in the text cell below, in your own words, what's wrong with the code. (Remember, double-click the cell to edit it, and then click the Run button when you're done.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"4 = 2 + 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Write your answer here, replacing this text.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** When you run the following cell, Python will produce an slightly-cryptic error message. **Fix the error,** and then **explain below** in your own words what was wrong with the code."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"two = 2\n",
|
||||
"four = two plus two"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Write your answer here, replacing this text.*"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.7.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
126
Week-5/04_basic_arithmetic.ipynb
Normal file
126
Week-5/04_basic_arithmetic.ipynb
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Basic Arithmetic in Python\n",
|
||||
"This set of exercises gives you practice writing basic arithmetic expressions in Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** In the next cell, write a Python expression equivalent to this math expression:\n",
|
||||
"\n",
|
||||
"$$1 - \\frac{2^2}{2^{20}}$$\n",
|
||||
"\n",
|
||||
"The value of the expression is around 0.999996. After you have something that works, try to improve your solution by using only as many parentheses as you need. You can add a cell under your initial answer to show your work."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** Try to predict the value of the expression in the next cell. Before you run the cell, write your prediction in the cell below it."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"1 - 2 ** 3 / 4 + 5 * 6"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Your prediction here:\n",
|
||||
"..."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 3.** Try to predict whether the value of the expression below is larger or smaller than the value of the expression in the previous question. You can use the next cell to record your answer. Then run the cell to see what happens. If you were wrong, figure out why, and include that in your written answer."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"1 - 2 ** (3 / 4 + 5 * 6)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*Write your answer here, replacing this text.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 4.** A famous fact in mathematics is that\n",
|
||||
"\n",
|
||||
"$$(1 - \\frac{1}{n})^{-n}$$\n",
|
||||
"\n",
|
||||
"gets very close to the number $e$, which is roughly $2.718$, when $n$ is large. Verify that it gets closer to $e$ as $n$ gets larger by writing this expression in Python and trying different values for $n$.\n",
|
||||
"\n",
|
||||
"*Hint:* If you write several expressions in a cell, only the last will be printed. If you want to see the values of several copies of the expression at once, put `print(...)` around them. Alternatively, you can use separate cells for each expression."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"..."
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
68
Week-5/05_maxmin.ipynb
Normal file
68
Week-5/05_maxmin.ipynb
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## The Middle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Suppose that Yulin, another INF110 student, is tasked with writing code to find the middle of three distinct numbers named `a`, `b`, and `c`. (By “middle” we mean the value that is neither the maximum nor the minimum.) For example, given the values 4, 2, and 7, the code should give back 4.\n",
|
||||
"\n",
|
||||
"Yulin has a moment of inspired thought and rapidly types the following into her Jupyter notebook:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"a + b + c - max(a, b, c) + min(a, b, c)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Is her code correct? Why or why not? If not, make a *simple* change to the cell above to fix it.\n",
|
||||
"\n",
|
||||
"*Hint:* Running the cell will produce an error because we haven't defined `a`, `b`, and `c`, but you're free to make any changes you want to test your ideas. (When you submit, just indicate which cell is your final answer.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.8.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
117
Week-5/06_rounding.ipynb
Normal file
117
Week-5/06_rounding.ipynb
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Rounding with Function Calls"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"These exercises give you practice calling functions in Python."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 1.** Using the function `round`, round the number 1.23456789 to the nearest integer. Give the result the name `rounded`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rounded = ...\n",
|
||||
"rounded"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 2.** Round 1.23456789 to the nearest *thousandth* (.001).\n",
|
||||
"\n",
|
||||
"*Hint:* Running `round?` in a code cell will show you Python's documentation for the `round` function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"less_rounded = ...\n",
|
||||
"less_rounded"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Suppose you're counting all the small donations made to a political candidate. The donations were:\n",
|
||||
"\n",
|
||||
"* 32.25\n",
|
||||
"* 15.00\n",
|
||||
"* 24.45\n",
|
||||
"\n",
|
||||
"You need to report the total rounded to the nearest dollar.\n",
|
||||
"\n",
|
||||
"**Question 3.** First you decide to round *each donation* individually and then add up those 3 rounded amounts. Compute that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sum_of_rounded_amounts = ...\n",
|
||||
"sum_of_rounded_amounts"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Question 4.** You suspect this method doesn't work quite right. To check, you decide to try a different method: Adding up the donations, and then rounding the total. Compute that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"rounded_sum = ...\n",
|
||||
"rounded_sum"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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.8.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 1
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue