Streamline presentation
This commit is contained in:
parent
4b8aae1f28
commit
8dea642ce9
2 changed files with 388 additions and 382 deletions
File diff suppressed because it is too large
Load diff
|
@ -19,7 +19,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below."
|
||||
"Read [Chapter 4](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/master/04_iteration_00_lecture.ipynb) of the book. Then, work through the exercises below. The `...` indicate where you need to fill in your answers. You should not need to create any additional code cells. Occasionally, the `...` mean that you have to write more than one line of code."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -88,7 +88,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.1**: Explain for the $n = 3$ case why it can be solved as a **recursion**!"
|
||||
"**Q1**: Explain for the $n = 3$ case why it can be solved as a **recursion**!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -102,7 +102,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.2**: How does the number of minimal moves needed to solve a problem with three spots and $n$ disks grow as a function of $n$? How does this relate to the answer to **Q1.1**?"
|
||||
"**Q2**: How does the number of minimal moves needed to solve a problem with three spots and $n$ disks grow as a function of $n$? How does this relate to the answer to **Q1**?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -116,7 +116,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.3**: The **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem is of **exponential growth**. What does that mean? What does that imply for large $n$?"
|
||||
"**Q3**: The **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem is of **exponential growth**. What does that mean? What does that imply for large $n$?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -130,7 +130,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.4**: The video introduces the recursive relationship $Sol(4, 1, 3) = Sol(3, 1, 2) ~ \\bigoplus ~ Sol(1, 1, 3) ~ \\bigoplus ~ Sol(3, 2, 3)$. The $\\bigoplus$ is to be interpreted as some sort of \"plus\" operation. How does this \"plus\" operation work? How does this way of expressing the problem relate to the answer to **Q1.1**?"
|
||||
"**Q4**: The video introduces the recursive relationship $Sol(4, 1, 3) = Sol(3, 1, 2) ~ \\bigoplus ~ Sol(1, 1, 3) ~ \\bigoplus ~ Sol(3, 2, 3)$. The $\\bigoplus$ is to be interpreted as some sort of \"plus\" operation. How does this \"plus\" operation work? How does this way of expressing the problem relate to the answer to **Q1**?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -197,7 +197,7 @@
|
|||
"\n",
|
||||
"Once completed, `sol()` should print out all the moves in the correct order. With **printing a move**, we mean a line like \"1 -> 3\", short for \"Move the top-most disk from spot 1 to spot 3\".\n",
|
||||
"\n",
|
||||
"Write your answers to **Q1.5** to **Q1.7** into the subsequent code cell and finalize `sol()`! No need to write a docstring or validate the input here."
|
||||
"Write your answers to **Q5** to **Q7** into the subsequent code cell and finalize `sol()`! No need to write a docstring or validate the input here."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -208,13 +208,13 @@
|
|||
"source": [
|
||||
"def sol(disks, origin, destination):\n",
|
||||
"\n",
|
||||
" # answer to Q15.5\n",
|
||||
" # answer to Q5\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" # answer to Q15.6\n",
|
||||
" # answer to Q6\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" # answer to Q15.7\n",
|
||||
" # answer to Q7\n",
|
||||
" # ..."
|
||||
]
|
||||
},
|
||||
|
@ -222,14 +222,14 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.5**: What is the `disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!"
|
||||
"**Q5**: What is the `disks` argument when the function reaches its **base case**? Check for the base case with a simple `if` statement and return from the function using the **early exit** pattern!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.6**: If not in the base case, `sol()` needs to determine the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n",
|
||||
"**Q6**: If not in the base case, `sol()` needs to determine the `intermediate` spot given concrete `origin` and `destination` arguments. For example, if called with `origin=1` and `destination=2`, `intermediate` must be `3`.\n",
|
||||
"\n",
|
||||
"Add **one** compound `if` statement to `sol()` that has a branch for **every** possible `origin`-`destination` pair that sets a variable `intermediate` to the correct temporary spot. **How many** branches will there be?"
|
||||
]
|
||||
|
@ -238,7 +238,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.7**: `sol()` needs to call itself **two more times** with the correct 2-pairs chosen from the three available spots `origin`, `intermediate`, and `destination`.\n",
|
||||
"**Q7**: `sol()` needs to call itself **two more times** with the correct 2-pairs chosen from the three available spots `origin`, `intermediate`, and `destination`.\n",
|
||||
"\n",
|
||||
"In between the two recursive function calls, write a `print()` statement that prints out from where to where the \"remaining and largest\" disk has to be moved!"
|
||||
]
|
||||
|
@ -247,7 +247,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.8**: Execute the code cells below and confirm that the printed moves are correct!"
|
||||
"**Q8**: Execute the code cells below and confirm that the printed moves are correct!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -301,7 +301,7 @@
|
|||
"\n",
|
||||
"Let's create a more concise `hanoi()` function that, in addition to a positional `disks` argument, takes three keyword-only arguments `origin`, `intermediate`, and `destination` with default values `\"left\"`, `\"center\"`, and `\"right\"`.\n",
|
||||
"\n",
|
||||
"Write your answers to **Q1.9** and **Q1.10** into the subsequent code cell and finalize `hanoi()`! No need to write a docstring or validate the input here."
|
||||
"Write your answers to **Q9** and **Q10** into the subsequent code cell and finalize `hanoi()`! No need to write a docstring or validate the input here."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -312,10 +312,10 @@
|
|||
"source": [
|
||||
"def hanoi(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n",
|
||||
"\n",
|
||||
" # answer to Q15.9\n",
|
||||
" # answer to Q9\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" # answer to Q15.10\n",
|
||||
" # answer to Q10\n",
|
||||
" # ..."
|
||||
]
|
||||
},
|
||||
|
@ -323,14 +323,14 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.9**: Copy the base case from `sol()`!"
|
||||
"**Q9**: Copy the base case from `sol()`!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.10**: Instead of conditional logic, `hanoi()` calls itself **two times** with the **three** arguments `origin`, `intermediate`, and `destination` passed on in a **different** order.\n",
|
||||
"**Q10**: Instead of conditional logic, `hanoi()` calls itself **two times** with the **three** arguments `origin`, `intermediate`, and `destination` passed on in a **different** order.\n",
|
||||
"\n",
|
||||
"Figure out how the arguments are passed on in the two recursive `hanoi()` calls!\n",
|
||||
"\n",
|
||||
|
@ -341,7 +341,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.11**: Execute the code cells below and confirm that the printed moves are correct!"
|
||||
"**Q11**: Execute the code cells below and confirm that the printed moves are correct!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -415,7 +415,7 @@
|
|||
"\n",
|
||||
"Let's create a `hanoi_moves()` function that follows the same internal logic as `hanoi()`, but, instead of printing out the moves, returns the number of steps done so far in the recursion.\n",
|
||||
"\n",
|
||||
"Write your answers to **Q1.12** to **Q1.14** into the subsequent code cell and finalize `hanoi_moves()`! No need to write a docstring or validate the input here."
|
||||
"Write your answers to **Q12** to **Q14** into the subsequent code cell and finalize `hanoi_moves()`! No need to write a docstring or validate the input here."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -426,12 +426,12 @@
|
|||
"source": [
|
||||
"def hanoi_moves(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\"):\n",
|
||||
"\n",
|
||||
" # answer to Q15.12\n",
|
||||
" # answer to Q12\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" moves = ... # <- answer to Q15.13\n",
|
||||
" moves += hanoi_moves(...) # <- answer to Q15.14 between the ()\n",
|
||||
" moves += hanoi_moves(...) # <- answer to Q15.14 between the ()\n",
|
||||
" moves = ... # <- answer to Q13\n",
|
||||
" moves += hanoi_moves(...) # <- answer to Q14 between the ()\n",
|
||||
" moves += hanoi_moves(...) # <- answer to Q14 between the ()\n",
|
||||
"\n",
|
||||
" return moves"
|
||||
]
|
||||
|
@ -440,21 +440,21 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.12**: Copy the base case from `hanoi()`! What count should be returned when it is reached?"
|
||||
"**Q12**: Copy the base case from `hanoi()`! What count should be returned when it is reached?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.13**: Initialize the variable `moves` with an appropriate count! This is the number of moves that corresponds to **one** recursive function call."
|
||||
"**Q13**: Initialize the variable `moves` with an appropriate count! This is the number of moves that corresponds to **one** recursive function call."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.14**: `moves` is updated with the counts passed up from the two recursive calls.\n",
|
||||
"**Q14**: `moves` is updated with the counts passed up from the two recursive calls.\n",
|
||||
"\n",
|
||||
"Complete the two recursive function calls with the same arguments as in `hanoi()`!"
|
||||
]
|
||||
|
@ -463,7 +463,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.15**: Write a `for`-loop that prints out the **minimum number** of moves needed to solve Towers of Hanoi for any number of `disks` from `1` through `20` to confirm your answer to **Q1.2**."
|
||||
"**Q15**: Write a `for`-loop that prints out the **minimum number** of moves needed to solve Towers of Hanoi for any number of `disks` from `1` through `20` to confirm your answer to **Q2**."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -491,7 +491,7 @@
|
|||
"\n",
|
||||
"With `disks` in the range from `24` through `26`, the computation time roughly doubles for each increase of `disks` by 1.\n",
|
||||
"\n",
|
||||
"**Q1.16**: Execute the code cells below and see for yourself!"
|
||||
"**Q16**: Execute the code cells below and see for yourself!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -537,7 +537,7 @@
|
|||
"source": [
|
||||
"The above `hanoi()` prints the optimal solution's moves in the correct order but fails to label each move with an order number. This can be built in by passing on one more argument `offset` down the recursion tree. As the logic gets a bit \"involved,\" `hanoi_ordered()` below is almost finished.\n",
|
||||
"\n",
|
||||
"Write your answers to **Q1.17** and **Q1.18** into the subsequent code cell and finalize `hanoi_ordered()`! No need to write a docstring or validate the input here."
|
||||
"Write your answers to **Q17** and **Q18** into the subsequent code cell and finalize `hanoi_ordered()`! No need to write a docstring or validate the input here."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -548,7 +548,7 @@
|
|||
"source": [
|
||||
"def hanoi_ordered(disks, *, origin=\"left\", intermediate=\"center\", destination=\"right\", offset=None):\n",
|
||||
"\n",
|
||||
" # answer to Q15.17\n",
|
||||
" # answer to Q17\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" total = (2 ** disks - 1)\n",
|
||||
|
@ -558,23 +558,23 @@
|
|||
" if offset is not None:\n",
|
||||
" count += offset\n",
|
||||
"\n",
|
||||
" hanoi_ordered(..., offset=offset) # <- answer to Q15.18\n",
|
||||
" # ... <- answer to Q15.18\n",
|
||||
" hanoi_ordered(..., offset=count) # <- answer to Q15.18"
|
||||
" hanoi_ordered(..., offset=offset) # <- answer to Q18\n",
|
||||
" # ... <- answer to Q18\n",
|
||||
" hanoi_ordered(..., offset=count) # <- answer to Q18"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.17**: Copy the base case from the original `hanoi()`!"
|
||||
"**Q17**: Copy the base case from the original `hanoi()`!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.18**: Complete the two recursive function calls with the same arguments as in `hanoi()` or `hanoi_moves()`! Do not change the already filled in `offset` arguments!\n",
|
||||
"**Q18**: Complete the two recursive function calls with the same arguments as in `hanoi()` or `hanoi_moves()`! Do not change the already filled in `offset` arguments!\n",
|
||||
"\n",
|
||||
"Then, copy the `print()` statement from `hanoi()` and adjust it to print out `count` as well!"
|
||||
]
|
||||
|
@ -583,7 +583,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.19**: Execute the code cells below and confirm that the order numbers are correct!"
|
||||
"**Q19**: Execute the code cells below and confirm that the order numbers are correct!"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -640,7 +640,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Q1.20**: Conducting your own research on the internet (max. 15 minutes), what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with **more than three** landing spots?"
|
||||
"**Q20**: Conducting your own research on the internet (max. 15 minutes), what can you say about generalizing the **[Towers of Hanoi](https://en.wikipedia.org/wiki/Tower_of_Hanoi)** problem to a setting with **more than three** landing spots?"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue