{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**Note**: Click on \"*Kernel*\" > \"*Restart Kernel and Run All*\" in [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/) *after* finishing the exercises to ensure that your solution runs top to bottom *without* any errors. If you cannot run this file on your machine, you may want to open it [in the cloud ](https://mybinder.org/v2/gh/webartifex/intro-to-data-science/main?urlpath=lab/tree/00_python_in_a_nutshell/04_exercises_fizz_buzz.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 0: Python in a Nutshell (Coding Exercises)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The exercises below assume that you have read the preceeding content sections.\n", "\n", "The `...`'s in the code cells indicate where you need to fill in code snippets. The number of `...`'s within a code cell give you a rough idea of how many lines of code are needed to solve the task. You should not need to create any additional code cells for your final solution. However, you may want to use temporary code cells to try out some ideas." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fizz Buzz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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 is (cf., [source ](https://news.ycombinator.com/item?id=16446774)).\n", "\n", "In its simplest form, a group of people starts 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": [ "**Q1**: Complete the code cell below implementing the Fizz Buzz game with some `if`-`elif`-`else` logic according to the rules above for the numbers from `1` to `33`! The cell should simply print out either the numbers or the words \"Fizz,\" \"Buzz,\" or \"FizzBuzz.\"\n", "\n", "Hints:\n", "- The *order* of the **conditions** is important\n", "- By what single number can all numbers divisible by both `3` and `5` by divided by?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for number in range(...):\n", " ...\n", " ...\n", " ...\n", " ...\n", " ...\n", " ...\n", " ...\n", " ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2.1**: Re-write your solution from **Q1** such that the [print() ](https://docs.python.org/3/library/functions.html#print) function is executed only once!\n", "\n", "Hints:\n", "- You can also store **text data** in variables\n", "- It may be helpful to *overwrite* the `out` variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for out in range(...):\n", " ...\n", " ...\n", " ...\n", " ...\n", " ...\n", " ...\n", "\n", " print(out)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Q2.2**: What may be advantages of writing code like in the solution to **Q2.1**?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " < your answer >" ] } ], "metadata": { "kernelspec": { "display_name": "intro-to-data-science", "language": "python", "name": "intro-to-data-science" }, "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.12.4" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": false, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }