Move FizzBuzz exercise from chapter 01 to 03

This commit is contained in:
Alexander Hess 2019-09-25 14:27:03 +02:00
commit 84e08d06f4
2 changed files with 61 additions and 99 deletions

View file

@ -211,11 +211,6 @@
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
@ -235,28 +230,36 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "markdown",
@ -270,14 +273,18 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"discounted_price(...)"
]
},
{
"cell_type": "markdown",
@ -304,14 +311,23 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Fizz Buzz revisited"
"### Fizz Buzz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you worked on the Fizz Buzz exercise in Chapter 1, you actually did not know about the `elif` and `else` keywords yet. Well, now you do."
"The kids game [Fizz Buzz](https://en.wikipedia.org/wiki/Fizz_buzz) is said to be often used in job interviews for entry level positions. However, opinions vary as to how good of a test it actually is ([source](https://news.ycombinator.com/item?id=16446774)).\n",
"\n",
"In its simplest form, a group of people start counting upwards in an alternating fashion. Whenever a number is divisible by $3$, the person must say \"Fizz\" instead of the number. The same holds for numbers divisible by $5$ when the person must say \"Buzz\". If a number is divisible by both numbers, one must say \"FizzBuzz\". Probably, this game would also make a good drinking game with the \"right\" beverages."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9.1**: First, create a list `numbers` with the numbers from 1 through 100. You could type all numbers manually but there is of course a smarter way. The built-in [range()](https://docs.python.org/3/library/functions.html#func-range) may be useful here. Read how it works in the documentation. To make the output of [range()](https://docs.python.org/3/library/functions.html#func-range) a `list` object, you have to wrap it with the [list()](https://docs.python.org/3/library/functions.html#func-list) built-in (i.e., `list(range(...))`)."
]
},
{
@ -320,16 +336,20 @@
"metadata": {},
"outputs": [],
"source": [
"numbers = list(range(1, 101))"
"numbers = ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9**: Copy and paste your answer to **Q11.2** in Chapter 1 here and instead of three consecutive `if` statements re-write it with *one* compound `if` statement.\n",
"**Q9.2**: Loop over the `numbers` list and replace numbers for which one of the two (or both) conditions apply with text strings `\"Fizz\"`, `\"Buzz\"`, or `\"FizzBuzz\"` using the indexing operator `[]` and the assignment statement `=`.\n",
"\n",
"This code will then be a lot more robust as the order of the three `if` statements cannot be screwed up."
"In Chapter 1 we saw that Python starts indexing with `0` as the first element. Keep that in mind.\n",
"\n",
"So in each iteration of the `for`-loop you have to determine an `index` variable as well as checking the actual `number` for its divisors.\n",
"\n",
"Hint: the order of the conditions is important!"
]
},
{
@ -338,17 +358,31 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
"for number in numbers:\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ...\n",
" ..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Q9.3**: Create a loop that prints out either the number or any of the Fizz Buzz substitutes. Do it in such a way that we do not end up with 100 lines of output here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for number in numbers:\n",
" print(...)"
]
}
],