diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb index 34021448a..674b220c5 100644 --- a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb +++ b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb @@ -1,169 +1,299 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "snail-and-well.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# The Snail and the Well\n", - "\n", - "A snail falls at the bottom of a 125 cm well. Each day the snail rises 30 cm. But at night, while sleeping, slides 20 cm because the walls are wet. How many days does it take for the snail to escape the well?\n", - "\n", - "**Hint**: The snail gets out of the well when it surpasses the 125cm of height.\n", - "\n", - "## Tools\n", - "\n", - "1. Loop: **while**\n", - "2. Conditional statements: **if-else**\n", - "3. Function: **print()**\n", - "\n", - "## Tasks\n", - "\n", - "#### 1. Assign the challenge data to variables with representative names: `well_height`, `daily_distance`, `nightly_distance` and `snail_position`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Create a variable `days` to keep count of the days that pass until the snail escapes the well. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Find the solution to the challenge using the variables defined above. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Print the solution." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Bonus\n", - "The distance traveled by the snail each day is now defined by a list.\n", - "```\n", - "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", - "```\n", - "On the first day, the snail rises 30cm but during the night it slides 20cm. On the second day, the snail rises 21cm but during the night it slides 20cm, and so on. \n", - "\n", - "#### 1. How many days does it take for the snail to escape the well?\n", - "Follow the same guidelines as in the previous challenge.\n", - "\n", - "**Hint**: Remember that the snail gets out of the well when it surpasses the 125cm of height." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. What is its maximum displacement in one day? And its minimum? Calculate the displacement using only the travel distance of the days used to get out of the well. \n", - "**Hint**: Remember that displacement means the total distance risen taking into account that the snail slides at night. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. What is its average progress? Take into account the snail slides at night." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night." - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "t3IR-fi_cuUY" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aiiwtz5qcuUf" + }, + "source": [ + "# The Snail and the Well\n", + "\n", + "A snail falls at the bottom of a 125 cm well. Each day the snail rises 30 cm. But at night, while sleeping, slides 20 cm because the walls are wet. How many days does it take for the snail to escape the well?\n", + "\n", + "**Hint**: The snail gets out of the well when it surpasses the 125cm of height.\n", + "\n", + "## Tools\n", + "\n", + "1. Loop: **while**\n", + "2. Conditional statements: **if-else**\n", + "3. Function: **print()**\n", + "\n", + "## Tasks\n", + "\n", + "#### 1. Assign the challenge data to variables with representative names: `well_height`, `daily_distance`, `nightly_distance` and `snail_position`." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "lkrJWkYPcuUh" + }, + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = 20\n", + "snail_position = 0" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8TYYAMvkcuUj" + }, + "source": [ + "#### 2. Create a variable `days` to keep count of the days that pass until the snail escapes the well. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pn6aSYtMcuUk" + }, + "source": [ + "days = 0" + ], + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DQPuRPmlcuUk" + }, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "p9dWhmaLcuUl" + }, + "source": [ + "while True:\n", + " days += 1\n", + " snail_position += daily_distance\n", + " if snail_position >= well_height:\n", + " break\n", + " snail_position -= nightly_distance\n" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "x-qxoqr1cuUm" + }, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WMNZokjocuUn", + "outputId": "402462f1-2bb6-48a2-f6b7-b8006537ae70" + }, + "source": [ + "print(days)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "11\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ST5OEgo-cuUn" + }, + "source": [ + "## Bonus\n", + "The distance traveled by the snail each day is now defined by a list.\n", + "```\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "```\n", + "On the first day, the snail rises 30cm but during the night it slides 20cm. On the second day, the snail rises 21cm but during the night it slides 20cm, and so on. \n", + "\n", + "#### 1. How many days does it take for the snail to escape the well?\n", + "Follow the same guidelines as in the previous challenge.\n", + "\n", + "**Hint**: Remember that the snail gets out of the well when it surpasses the 125cm of height." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "phDzTBUjcuUo", + "outputId": "f2ed6fcc-5c19-45ba-9176-c78e1e4a8dd5" + }, + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "print(len(advance_cm))\n" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "11\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9yDTFImycuUp" + }, + "source": [ + "#### 2. What is its maximum displacement in one day? And its minimum? Calculate the displacement using only the travel distance of the days used to get out of the well. \n", + "**Hint**: Remember that displacement means the total distance risen taking into account that the snail slides at night. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "S4WgF__ZcuUp", + "outputId": "24edd603-a818-48c7-d4bc-b23c90288437" + }, + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "print(max(advance_cm))" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "77\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZvcXEZZ_cuUq" + }, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "TyTm03S2cuUq", + "outputId": "04fb2b8a-f725-4b5c-f524-13a087cf766b" + }, + "source": [ + "advance_cm =[30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "num = sum(advance_cm)\n", + "den = len(advance_cm)\n", + "avg = num / den\n", + "print(avg)\n" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "38.09090909090909\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CP--AUP5cuUr" + }, + "source": [ + "#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "JeyUTfgWcuUr", + "outputId": "fa289c0e-3f35-48fe-9958-73de131969a2" + }, + "source": [ + "import numpy as np\n", + "advance_cm =[30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "print(np.std(advance_cm))" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "17.159437082600803\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb index b4a5f6d7e..803bfed02 100644 --- a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb +++ b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb @@ -1,229 +1,399 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "duel-of-sorcerers.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Duel of Sorcerers\n", - "You are witnessing an epic battle between two powerful sorcerers: Gandalf and Saruman. Each sorcerer has 10 spells of variable power in their mind and they are going to throw them one after the other. The winner of the duel will be the one who wins more of those clashes between spells. Spells are represented as a list of 10 integers whose value equals the power of the spell.\n", - "```\n", - "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", - "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", - "```\n", - "For example:\n", - "- The first clash is won by Saruman: 10 against 23.\n", - "- The second clash is won by Saruman: 11 against 66.\n", - "- ...\n", - "\n", - "You will create two variables, one for each sorcerer, where the sum of clashes won will be stored. Depending on which variable is greater at the end of the duel, you will show one of the following three results on the screen:\n", - "* Gandalf wins\n", - "* Saruman wins\n", - "* Tie\n", - "\n", - "" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Tools\n", - "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", - "\n", - "1. Data structures: **lists, dictionaries**\n", - "2. Loop: **for loop**\n", - "3. Conditional statements: **if-elif-else**\n", - "4. Functions: **range(), len(), print()**\n", - "\n", - "## Tasks\n", - "\n", - "#### 1. Create two variables called `gandalf` and `saruman` and assign them the spell power lists. Create a variable called `spells` to store the number of spells that the sorcerers cast. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. \n", - "You will use these variables to count the number of clashes each sorcerer wins. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Using the lists of spells of both sorcerers, update variables `gandalf_wins` and `saruman_wins` to count the number of times each sorcerer wins a clash. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Who won the battle?\n", - "Print `Gandalf wins`, `Saruman wins` or `Tie` depending on the result. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Bonus\n", - "\n", - "In this bonus challenge, you'll need to check the winner of the battle but this time, a sorcerer wins if he succeeds in winning 3 spell clashes in a row.\n", - "\n", - "Also, the spells now have a name and there is a dictionary that associates that name to a power.\n", - "\n", - "```\n", - "POWER = {\n", - " 'Fireball': 50, \n", - " 'Lightning bolt': 40, \n", - " 'Magic arrow': 10, \n", - " 'Black Tentacles': 25, \n", - " 'Contagion': 45\n", - "}\n", - "\n", - "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", - " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", - "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", - " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", - "```\n", - "\n", - "#### 1. Create variables `POWER`, `gandalf` and `saruman` as seen above. Create a variable called `spells` to store the number of spells that the sorcerers cast. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Create two variables called `gandalf_power` and `saruman_power` to store the list of spell powers of each sorcerer." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. The battle starts! Using the variables you've created above, code the execution of spell clashes. Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row. \n", - "If a clash ends up in a tie, the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Find the average spell power of Gandalf and Saruman. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "FvJVj3ZxpeQH" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "i4yCepjipeQJ" + }, + "source": [ + "# Duel of Sorcerers\n", + "You are witnessing an epic battle between two powerful sorcerers: Gandalf and Saruman. Each sorcerer has 10 spells of variable power in their mind and they are going to throw them one after the other. The winner of the duel will be the one who wins more of those clashes between spells. Spells are represented as a list of 10 integers whose value equals the power of the spell.\n", + "```\n", + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "```\n", + "For example:\n", + "- The first clash is won by Saruman: 10 against 23.\n", + "- The second clash is won by Saruman: 11 against 66.\n", + "- ...\n", + "\n", + "You will create two variables, one for each sorcerer, where the sum of clashes won will be stored. Depending on which variable is greater at the end of the duel, you will show one of the following three results on the screen:\n", + "* Gandalf wins\n", + "* Saruman wins\n", + "* Tie\n", + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dQva1x2bpeQK" + }, + "source": [ + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "\n", + "1. Data structures: **lists, dictionaries**\n", + "2. Loop: **for loop**\n", + "3. Conditional statements: **if-elif-else**\n", + "4. Functions: **range(), len(), print()**\n", + "\n", + "## Tasks\n", + "\n", + "#### 1. Create two variables called `gandalf` and `saruman` and assign them the spell power lists. Create a variable called `spells` to store the number of spells that the sorcerers cast. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "z5pDvW-GpeQL" + }, + "source": [ + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "spells = len(gandalf)" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZkjpXzbvpeQL" + }, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. \n", + "You will use these variables to count the number of clashes each sorcerer wins. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Gjq-r4OnpeQL" + }, + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ], + "execution_count": 2, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cxDd5RSSpeQM" + }, + "source": [ + "#### 3. Using the lists of spells of both sorcerers, update variables `gandalf_wins` and `saruman_wins` to count the number of times each sorcerer wins a clash. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SOEIuF0npeQM" + }, + "source": [ + "for i in range(spells):\n", + " if gandalf[i] > saruman[i]:\n", + " gandalf_wins += 1\n", + " elif gandalf[i] < saruman[i]:\n", + " saruman_wins += 1" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_jmaIx-XpeQM" + }, + "source": [ + "#### 4. Who won the battle?\n", + "Print `Gandalf wins`, `Saruman wins` or `Tie` depending on the result. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Ie202WDvpeQN", + "outputId": "a30a2c01-cae4-40c3-c5c3-4dfaf85923df" + }, + "source": [ + "if gandalf_wins > saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "elif gandalf_wins < saruman_wins:\n", + " print(\"Saruman wins\")\n", + "else: \n", + " print(\"Tie\")" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QAPHT4KbpeQN" + }, + "source": [ + "## Bonus\n", + "\n", + "In this bonus challenge, you'll need to check the winner of the battle but this time, a sorcerer wins if he succeeds in winning 3 spell clashes in a row.\n", + "\n", + "Also, the spells now have a name and there is a dictionary that associates that name to a power.\n", + "\n", + "```\n", + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "```\n", + "\n", + "#### 1. Create variables `POWER`, `gandalf` and `saruman` as seen above. Create a variable called `spells` to store the number of spells that the sorcerers cast. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2jc2H9ExpeQN" + }, + "source": [ + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "spells = 0" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GgzmoM74peQO" + }, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WX160BXqpeQO" + }, + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ], + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W_13AxdFpeQO" + }, + "source": [ + "#### 3. Create two variables called `gandalf_power` and `saruman_power` to store the list of spell powers of each sorcerer." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "WT66_UDIpeQO" + }, + "source": [ + "gandalf_power = []\n", + "for i in gandalf:\n", + " gandalf_power.append(POWER[i])\n", + "saruman_power = []\n", + "for i in saruman:\n", + " saruman_power.append(POWER[i])" + ], + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zV-muqY-peQP" + }, + "source": [ + "#### 4. The battle starts! Using the variables you've created above, code the execution of spell clashes. Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row. \n", + "If a clash ends up in a tie, the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "donBaJl9peQP", + "outputId": "7e05c42c-31d5-4399-9561-7ed1427d30cc" + }, + "source": [ + "while gandalf_wins != 3 and saruman_wins != 3: \n", + " if gandalf_power[spells] > saruman_power[spells]:\n", + " gandalf_wins += 1\n", + " while gandalf_wins in [1,2]:\n", + " spells += 1\n", + " if gandalf_power[spells] > saruman_power[spells]:\n", + " gandalf_wins += 1\n", + " else:\n", + " gandalf_wins = 0\n", + " elif gandalf_power[spells] < saruman_power[spells]:\n", + " saruman_wins += 1\n", + " while saruman_wins in [1,2]:\n", + " spells += 1\n", + " if gandalf_power[spells] < saruman_power[spells]:\n", + " saruman_wins += 1\n", + " else:\n", + " saruman_wins = 0 \n", + "\n", + "if gandalf_wins > saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "else:\n", + " print(\"Saruman wins\")" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Af-3InlipeQP" + }, + "source": [ + "#### 5. Find the average spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "GL97Iv2npeQP", + "outputId": "7bef81f5-fa30-49df-900b-c59169663537" + }, + "source": [ + "from statistics import mean\n", + "print(f\"The average power of Gandalf is {round(mean(gandalf_power), 2)}, and Saruman's average power is {round(mean(saruman_power), 2)}\")" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The average power of Gandalf is 39, and Saruman's average power is 30.5\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6FB5Oc1PpeQP" + }, + "source": [ + "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "eEEpswIupeQP", + "outputId": "a92d52a9-f687-4019-ab7e-31894e4a343d" + }, + "source": [ + "from statistics import stdev\n", + "print(f\"The standard deviation of powers of Gandalf's power is {round(stdev(gandalf_power), 2)}, while Saruman's is {round(stdev(saruman_power), 2)}\")\n", + "textest = 'urray'\n" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The standard deviation of powers of Gandalf's power is 15.95, while Saruman's is 16.41\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8fd..10c8f84f9 100644 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -1,125 +1,205 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "bus.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bus\n", - "\n", - "This bus has a passenger entry and exit control system to monitor the number of occupants it carries and thus detect when there are too many.\n", - "\n", - "At each stop, the entry and exit of passengers is represented by a tuple consisting of two integer numbers.\n", - "```\n", - "bus_stop = (in, out)\n", - "```\n", - "The succession of stops is represented by a list of these tuples.\n", - "```\n", - "stops = [(in1, out1), (in2, out2), (in3, out3), (in4, out4)]\n", - "```\n", - "\n", - "## Tools\n", - "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", - "* Data structures: **lists, tuples**\n", - "* Loop: **while/for loops**\n", - "* Functions: **min, max, len**\n", - "\n", - "## Tasks" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Variables\n", - "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 1. Calculate the number of stops." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Assign to a variable a list whose elements are the number of passengers at each stop (in-out).\n", - "Each item depends on the previous item in the list + in - out." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Find the maximum occupation of the bus." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Calculate the average occupation. And the standard deviation." - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "z_YHfQiB1XGF" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q_SSvLI31XGI" + }, + "source": [ + "# Bus\n", + "\n", + "This bus has a passenger entry and exit control system to monitor the number of occupants it carries and thus detect when there are too many.\n", + "\n", + "At each stop, the entry and exit of passengers is represented by a tuple consisting of two integer numbers.\n", + "```\n", + "bus_stop = (in, out)\n", + "```\n", + "The succession of stops is represented by a list of these tuples.\n", + "```\n", + "stops = [(in1, out1), (in2, out2), (in3, out3), (in4, out4)]\n", + "```\n", + "\n", + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "* Data structures: **lists, tuples**\n", + "* Loop: **while/for loops**\n", + "* Functions: **min, max, len**\n", + "\n", + "## Tasks" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "lgNOzt3C1XGI" + }, + "source": [ + "# Variables\n", + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]\n", + "movement = []\n", + "occupation = []" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rLzsRyFa1XGJ" + }, + "source": [ + "#### 1. Calculate the number of stops." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "C1qp08Vs1XGJ", + "outputId": "212ffadb-e1bc-4eb7-f0df-33163db2f20b" + }, + "source": [ + "total_stops = len(stops)\n", + "print(\"The total number of bus stops is\", total_stops)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The total number of bus stops is 9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Kpvvz1w01XGK" + }, + "source": [ + "#### 2. Assign to a variable a list whose elements are the number of passengers at each stop (in-out).\n", + "Each item depends on the previous item in the list + in - out." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "X5VMevZr1XGK" + }, + "source": [ + "for up,down in stops:\n", + " movement.append(up - down)\n", + "\n", + "for i in range(total_stops):\n", + " occupation.append(sum(movement[0:i+1]))" + ], + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DQZrYZPd1XGK" + }, + "source": [ + "#### 3. Find the maximum occupation of the bus." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ueI7jmx01XGL", + "outputId": "0ef02edd-9e47-4de0-a290-a7ace05c6e59" + }, + "source": [ + "print(\"The maximum occupation in the bus is\", max(occupation))" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The maximum occupation in the bus is 14\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q2PgsIxM1XGL" + }, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0aVwHdMS1XGL", + "outputId": "0cc1ac31-d112-4218-bc94-29d7a2e88b32" + }, + "source": [ + "print(\"The average occupation is\", round((sum(occupation)/total_stops), 2))\n", + "import statistics\n", + "print(\"The standard deviation of the occupation is\", round(statistics.stdev(occupation), 2))" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The average occupation is 9.33\n", + "The standard deviation of the occupation is 3.39\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/1.-Python/4.-Robin-Hood/robin-hood.ipynb b/1.-Python/4.-Robin-Hood/robin-hood.ipynb index 01de29d3b..bded8a84e 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -1,133 +1,268 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "robin-hood.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Robin Hood\n", - "Robin Hood has entered a competition to win the archery contest in Sherwood. With his bow and arrows, he needs to shoot on a target and try to hit as close as possible to the center.\n", - "\n", - "![](images/arrows.jpg)\n", - "\n", - "## Context\n", - "In this challenge, the landing position of arrows shot by archers in the competition will be represented using 2-dimensional coordinates. \n", - "\n", - "In the 2-dimensional space, a point can be defined by a pair of values that correspond to the horizontal coordinate (x) and the vertical coordinate (y). For example, in our case, an arrow that hits the center of the archery target will land in position (0, 0) on the coordinate axes. \n", - "\n", - "The space can be divided into 4 zones (quadrants): Q1, Q2, Q3, Q4. If a point is in Q1, both its x coordinate and y coordinate are positive. Any point with a null x or y coordinate is considered to not belong to any quadrant. \n", - "\n", - "If you want to know more about the cartesian coordinate system, you can check this [link](https://en.wikipedia.org/wiki/Cartesian_coordinate_system). \n", - "\n", - "## Tools\n", - "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", - "* Data structures: **lists, sets, tuples**\n", - "* Conditional statements: **if-elif-else**\n", - "* Loop: **while/for**\n", - "* Minimum (optional sorting)\n", - "\n", - "## Tasks\n", - "Robin Hood has hit the following points:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5), (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),\n", - " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 1. Robin Hood is famous for hitting an arrow with another arrow. Find the coordinates of the points where an arrow hits another arrow." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Calculate how many arrows have fallen in each quadrant. \n", - "**Note**: the arrows that fall in the axis (x=0 or y=0) don't belong to any quadrant." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Find the point closest to the center. Calculate its distance to the center. \n", - "Take into account that there might be more than one point at the minimum distance to the center.\n", - "\n", - "**Hint**: Use the Euclidean distance. You can find more information about it [here](https://en.wikipedia.org/wiki/Euclidean_distance). \n", - "**Hint**: Defining a function that calculates the distance to the center can help." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. If the archery target has a radius of 9, calculate the number of arrows that won't hit the target. \n", - "**Hint**: Use the function created in step 3. " - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "5Iylrv5g3OmC" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0OpeHAHt3OmF" + }, + "source": [ + "# Robin Hood\n", + "Robin Hood has entered a competition to win the archery contest in Sherwood. With his bow and arrows, he needs to shoot on a target and try to hit as close as possible to the center.\n", + "\n", + "![](https://github.com/ronlicaj/data-prework/blob/master/images/arrows.jpg?raw=1)\n", + "\n", + "## Context\n", + "In this challenge, the landing position of arrows shot by archers in the competition will be represented using 2-dimensional coordinates. \n", + "\n", + "In the 2-dimensional space, a point can be defined by a pair of values that correspond to the horizontal coordinate (x) and the vertical coordinate (y). For example, in our case, an arrow that hits the center of the archery target will land in position (0, 0) on the coordinate axes. \n", + "\n", + "The space can be divided into 4 zones (quadrants): Q1, Q2, Q3, Q4. If a point is in Q1, both its x coordinate and y coordinate are positive. Any point with a null x or y coordinate is considered to not belong to any quadrant. \n", + "\n", + "If you want to know more about the cartesian coordinate system, you can check this [link](https://en.wikipedia.org/wiki/Cartesian_coordinate_system). \n", + "\n", + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "* Data structures: **lists, sets, tuples**\n", + "* Conditional statements: **if-elif-else**\n", + "* Loop: **while/for**\n", + "* Minimum (optional sorting)\n", + "\n", + "## Tasks\n", + "Robin Hood has hit the following points:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fRvR5qj03OmG" + }, + "source": [ + "points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5), (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),\n", + " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]" + ], + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_YKX2NPm3OmG" + }, + "source": [ + "#### 1. Robin Hood is famous for hitting an arrow with another arrow. Find the coordinates of the points where an arrow hits another arrow." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "D0huQlsc3OmH", + "outputId": "64871447-350d-48cf-c44b-b804fe550e31" + }, + "source": [ + "bullseye = []\n", + "for i in range(len(points)):\n", + " if points[i] in points[i+1:]:\n", + " bullseye.append(points[i])\n", + "print(set(bullseye))" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{(4, 5), (5, 7), (-3, 2), (2, 2)}\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4jjRMDPL3OmH" + }, + "source": [ + "#### 2. Calculate how many arrows have fallen in each quadrant. \n", + "**Note**: the arrows that fall in the axis (x=0 or y=0) don't belong to any quadrant." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "r1BOSax-3OmH", + "outputId": "93d384ab-e771-4049-e126-3249d90e53e9" + }, + "source": [ + "q1 = []\n", + "q2 = []\n", + "q3 = []\n", + "q4 = []\n", + "count = 0\n", + "while count < len(points):\n", + " for hey,you in points[count:count+1]:\n", + " if hey > 0 and you > 0:\n", + " q1.append(points[count])\n", + " elif hey > 0 and you < 0:\n", + " q4.append(points[count])\n", + " elif hey < 0 and you > 0:\n", + " q2.append(points[count])\n", + " elif hey < 0 and you < 0:\n", + " q3.append(points[count])\n", + " count += 1\n", + "solution = \"There is a total of \" + str(len(q1)) + \" arrows in quadrant 1, \" + str(len(q2)) + \" arrows in quadrant 2, \" + str(len(q3)) + \" arrows in quadrant 3 and \" + str(len(q4)) + \" arrows in quadrant 4.\"\n", + "print(solution)\n", + "print(points[7:].index((5,7)))" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "There is a total of 10 arrows in quadrant 1, 6 arrows in quadrant 2, 2 arrows in quadrant 3 and 2 arrows in quadrant 4.\n", + "0\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uhl9zx323OmI" + }, + "source": [ + "#### 3. Find the point closest to the center. Calculate its distance to the center. \n", + "Take into account that there might be more than one point at the minimum distance to the center.\n", + "\n", + "**Hint**: Use the Euclidean distance. You can find more information about it [here](https://en.wikipedia.org/wiki/Euclidean_distance). \n", + "**Hint**: Defining a function that calculates the distance to the center can help." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "7xcnOUB03OmI", + "outputId": "93aae5d1-7f8b-429b-a661-3548a2675861" + }, + "source": [ + "##defining distance from center\n", + "\n", + "import math\n", + "def ramayana(shot):\n", + " hey = shot[0]\n", + " you = shot[1]\n", + " distance = math.sqrt(hey**2 + you**2)\n", + " return distance\n", + "\n", + "##creating list of distances\n", + "\n", + "distances = []\n", + "for i in points:\n", + " distances.append(round(ramayana(i), 2))\n", + "print(distances)\n", + "##creating list of closest points\n", + "\n", + "counter = 0\n", + "marksman = []\n", + "for i in distances:\n", + " if i == min(distances):\n", + " marksman.append(points[counter])\n", + " counter += 1 \n", + "\n", + "##printing solution\n", + "\n", + "print(\"The closest point(s) to the center is(are):\", marksman)\n", + "print(\"The distance of the closest point to the center is:\", min(distances))" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[6.4, 2.0, 8.06, 3.16, 3.61, 6.4, 3.61, 8.6, 8.6, 2.83, 6.4, 2.0, 8.06, 3.16, 3.61, 6.4, 3.61, 8.6, 8.6, 2.83, 12.73, 12.04]\n", + "The closest point(s) to the center is(are): [(0, 2), (0, -2)]\n", + "The distance of the closest point to the center is: 2.0\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "hXzKW4h93OmI" + }, + "source": [ + "#### 4. If the archery target has a radius of 9, calculate the number of arrows that won't hit the target. \n", + "**Hint**: Use the function created in step 3. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EPcKF9j63OmJ", + "outputId": "c2aea258-674c-49ba-f5ff-f48a9ffcfee0" + }, + "source": [ + "radius = 9\n", + "print(sum(1 for i in distances if i > radius), \"arrows didn't meet the target.\")" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2 arrows didn't meet the target.\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa20..474f0b1d7 100644 --- a/1.-Python/5.-Temperature-Processor/temperature.ipynb +++ b/1.-Python/5.-Temperature-Processor/temperature.ipynb @@ -1,262 +1,473 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "temperature.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Temperature Sensor\n", - "\n", - "There is a temperature sensor in the processor of your company's server. The company wants to analyze the data provided by the sensor to decide if they should change the cooling system for a better one. As changing the cooling system is expensive and you are an excellent data analyst, you can't make a decision without basis.\n", - "\n", - "## Tools\n", - "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", - "1. Data structures: **lists**\n", - "2. Loops: **list comprehension**\n", - "3. Functions: **min, max, print, len**\n", - "4. Conditional statements: **if-elif-else**\n", - "\n", - "## Tasks\n", - "The temperatures measured throughout the 24 hours of a day are:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "temperatures_C = [33, 66, 65, 0, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The first element of the list is the temperature at 12am, the second element is the temperature at 1am, and so on. \n", - "\n", - "The company has decided that if one of the following events occurs, then the cooling system needs to be replaced for a new one to avoid damaging the processor.\n", - "* More than 4 temperatures are greater than or equal to 70ºC.\n", - "* Any temperature is above 80ºC.\n", - "* The average temperature exceeds 65ºC.\n", - "\n", - "Follow the steps so that you can make the decision.\n", - "\n", - "#### 1. Find the minimum temperature of the day and store it in a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Find the maximum temperature of the day and store it in a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Create a list with the temperatures that are greater than or equal to 70ºC. Store it in a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Find the average temperature of the day and store it in a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Imagine that there was a sensor failure at 3am and the data for that specific hour was not recorded. How would you estimate the missing value? Replace the current value of the list at 3am for an estimation. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Bonus: the maintenance staff is from the United States and does not understand the international metric system. Help them by converting the temperatures from Celsius to Fahrenheit.\n", - "To know more about temperature conversion check this [link](https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature).\n", - "\n", - "**Formula**: \n", - "\n", - "$F = 1.8 * C + 32$" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 7. Make a decision!\n", - "Now it's time to make a decision taking into account what you have seen until now. \n", - "\n", - "Remember that if one of the following events occurs, then the cooling system needs to be replaced for a new one to avoid damaging the processor.\n", - "* More than 4 temperatures are greater than or equal to 70ºC.\n", - "* Any temperature is above 80ºC.\n", - "* The average temperature exceeds 65ºC.\n", - "\n", - "#### To make your decision, check if any of the three conditions above is met. You might need to use some of the variables you created in steps 1 to 6. Print a message to show if the cooling system needs to be changed or not." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Bonus\n", - "\n", - "The company has decided that the decision you made is not valid. They want you to analyze the data again but this time, the conditions that need to be met in order to change the cooling system are different.\n", - "\n", - "This time, if one of the following events occurs, then the cooling system needs to be replaced:\n", - "* The temperature is greater than 70ºC during more than 4 consecutive hours.\n", - "* Any temperature is above 80ºC.\n", - "* The average temperature exceeds 65ºC.\n", - "\n", - "Follow the steps so that you can make the decision.\n", - "\n", - "#### 1. Create a list with the hours where the temperature is greater than 70ºC. Store it in a variable." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Check if the list you created in step 1 has more than 4 consecutive hours. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Make the decision!\n", - "To make your decision, check if any of the three conditions is met. Print a message to show if the cooling system needs to be changed or not." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Find the average value of the temperature lists (ºC and ºF). What is the relation between both average values?" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Find the standard deviation of the temperature lists (ºC and ºF). What is the relation between both standard deviations?" - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "k7vlVpG35PL-" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f0xX-O8k5PMD" + }, + "source": [ + "# Temperature Sensor\n", + "\n", + "There is a temperature sensor in the processor of your company's server. The company wants to analyze the data provided by the sensor to decide if they should change the cooling system for a better one. As changing the cooling system is expensive and you are an excellent data analyst, you can't make a decision without basis.\n", + "\n", + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "1. Data structures: **lists**\n", + "2. Loops: **list comprehension**\n", + "3. Functions: **min, max, print, len**\n", + "4. Conditional statements: **if-elif-else**\n", + "\n", + "## Tasks\n", + "The temperatures measured throughout the 24 hours of a day are:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "48pARpD75PME" + }, + "source": [ + "temperatures_C = [33, 66, 65, 0, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "HE8xWI0_5PMF" + }, + "source": [ + "The first element of the list is the temperature at 12am, the second element is the temperature at 1am, and so on. \n", + "\n", + "The company has decided that if one of the following events occurs, then the cooling system needs to be replaced for a new one to avoid damaging the processor.\n", + "* More than 4 temperatures are greater than or equal to 70ºC.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "Follow the steps so that you can make the decision.\n", + "\n", + "#### 1. Find the minimum temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jRH4OMSH5PMF" + }, + "source": [ + "coldest = min(temperatures_C)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "p9lwyJID5PMG" + }, + "source": [ + "#### 2. Find the maximum temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "R9pMUpsi5PMG" + }, + "source": [ + "hottest = max(temperatures_C)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QTHGCW535PMG" + }, + "source": [ + "#### 3. Create a list with the temperatures that are greater than or equal to 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jJKXSKST5PMH" + }, + "source": [ + "hot_temps = [t for t in temperatures_C if t >= 70]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z1JICZsP5PMH" + }, + "source": [ + "#### 4. Find the average temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "sbniE6pv5PMI" + }, + "source": [ + "from statistics import mean\n", + "average = round(mean(temperatures_C), 2)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "R7RKovAj5PMI" + }, + "source": [ + "#### 5. Imagine that there was a sensor failure at 3am and the data for that specific hour was not recorded. How would you estimate the missing value? Replace the current value of the list at 3am for an estimation. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "n3RprhAu5PMJ", + "outputId": "66f2f579-4cf3-4bc4-ed56-3710b390d87c" + }, + "source": [ + "temperatures_C[3] = (temperatures_C[2] + temperatures_C[4])/2\n", + "print(temperatures_C)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[33, 66, 65, 62.0, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "pehKVIBg5PMJ" + }, + "source": [ + "#### 6. Bonus: the maintenance staff is from the United States and does not understand the international metric system. Help them by converting the temperatures from Celsius to Fahrenheit.\n", + "To know more about temperature conversion check this [link](https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature).\n", + "\n", + "**Formula**: \n", + "\n", + "$F = 1.8 * C + 32$" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mqIDXKSX5PMK", + "outputId": "70fe17fe-379e-4171-a3d5-5c84222909f8" + }, + "source": [ + "temperatures_F = [round(x*1.8 + 32) for x in temperatures_C]\n", + "print(temperatures_F)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[91, 151, 149, 144, 138, 140, 144, 147, 158, 169, 176, 178, 176, 181, 194, 174, 142, 127, 122, 120, 127, 118, 113, 102]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7Qy15Nfl5PMK" + }, + "source": [ + "#### 7. Make a decision!\n", + "Now it's time to make a decision taking into account what you have seen until now. \n", + "\n", + "Remember that if one of the following events occurs, then the cooling system needs to be replaced for a new one to avoid damaging the processor.\n", + "* More than 4 temperatures are greater than or equal to 70ºC.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "#### To make your decision, check if any of the three conditions above is met. You might need to use some of the variables you created in steps 1 to 6. Print a message to show if the cooling system needs to be changed or not." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ZlkCvFa15PMK", + "outputId": "fec76cfb-547d-4231-98da-cc2d20229b45" + }, + "source": [ + "if len(hot_ones) >= 4 or m >= 80 in temperatures_C or average >= 65:\n", + " print(\"The Server is overheating. Cooling system need to be changed.\")\n", + "else:\n", + " print(\"Your server is cool. The cooling system needs not to be cahnged.\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The Server is overheating. Cooling system need to be changed.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jqHc7gQN5PMK" + }, + "source": [ + "## Bonus\n", + "\n", + "The company has decided that the decision you made is not valid. They want you to analyze the data again but this time, the conditions that need to be met in order to change the cooling system are different.\n", + "\n", + "This time, if one of the following events occurs, then the cooling system needs to be replaced:\n", + "* The temperature is greater than 70ºC during more than 4 consecutive hours.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "Follow the steps so that you can make the decision.\n", + "\n", + "#### 1. Create a list with the hours where the temperature is greater than 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RrKf9x8O5PMK", + "outputId": "62db9cc7-ed15-4ea4-b70f-7b03da8d6f1c" + }, + "source": [ + "beyond70 = []\n", + "for i in range(len(temperatures_C)):\n", + " if temperatures_C[i] > 70:\n", + " if i == 0:\n", + " beyond70.append('12 A.M.')\n", + " elif i > 12:\n", + " beyond70.append(str(i - 12) + ' P.M.')\n", + " elif i == 12:\n", + " beyond70.append(str(i) + ' P.M.')\n", + " else:\n", + " beyond70.append(str(i) + ' A.M.')\n", + "print(beyond70)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['9 A.M.', '10 A.M.', '11 A.M.', '12 P.M.', '1 P.M.', '2 P.M.', '3 P.M.']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7eqRa8GM5PML" + }, + "source": [ + "#### 2. Check if the list you created in step 1 has more than 4 consecutive hours. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "UMPFeTkY5PML", + "outputId": "faf7a61a-6818-4ce9-abea-e7870b0c1efa" + }, + "source": [ + "print(f'There were {len(beyond70)} hours with temperatures above 70 C.')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "There were 7 hours with temperatures above 70 C.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fRZwk6Kc5PML" + }, + "source": [ + "#### 3. Make the decision!\n", + "To make your decision, check if any of the three conditions is met. Print a message to show if the cooling system needs to be changed or not." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Iq8tx2JW5PML", + "outputId": "c64deac4-104c-4f9b-ac0e-ffbddabddf01" + }, + "source": [ + "consexecution = 0\n", + "counter = 0\n", + "while consexecution < 4 and len(temperatures_C) != (counter + 1):\n", + " if temperatures_C[counter] >= 70:\n", + " consexecution += 1\n", + " else:\n", + " consexecution = 0\n", + " counter += 1\n", + "\n", + "if consexecution >= 4 or m >= 80 in temperatures_C or average >= 65:\n", + " print(\"Server under overheating hazard. Please change the cooling system.\")\n", + "else:\n", + " print(\"Cooling system effective. Your server is cool.\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Server under overheating hazard. Please change the cooling system.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Il0EmrFe5PML" + }, + "source": [ + "#### 4. Find the average value of the temperature lists (ºC and ºF). What is the relation between both average values?" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "WiYAUa9d5PML", + "outputId": "b1c42bd9-5bc5-4060-b196-46b75426596f" + }, + "source": [ + "from statistics import mean\n", + "print(f'The average temperature in C is {round(mean(temperatures_C), 2)}, and the average temperature in F is {round(mean(temperatures_F), 2)}, \\nwhich means that, in average, the measured temperatures in F are {round(mean(temperatures_F)/mean(temperatures_C), 2)} greater than the temperatures in C.')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The average temperature in C is 62.83, and the average temperature in F is 145.04, \n", + "which means that, in average, the measured temperatures in F are 2.31 greater than the temperatures in C.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "owlqj1Pq5PML" + }, + "source": [ + "#### 5. Find the standard deviation of the temperature lists (ºC and ºF). What is the relation between both standard deviations?" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "lku9116m5PMM", + "outputId": "78064f9a-6940-4b81-c1a3-fa371027acfc" + }, + "source": [ + "from statistics import stdev\n", + "print(f'The standard deviation of the measured temperatures in C is {round(stdev(temperatures_C), 2)}, and the standard deviation in F is {round(stdev(temperatures_F), 2)}, \\nwhich means that the standard deviation in F is {round(stdev(temperatures_F)/stdev(temperatures_C), 2)} greater than with the temperatures in C.')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The standard deviation of the measured temperatures in C is 14.95, and the standard deviation in F is 26.99, \n", + "which means that the standard deviation in F is 1.81 greater than with the temperatures in C.\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" index 9e551dfd4..382b3416e 100644 --- "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" @@ -1,234 +1,452 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "" - ] + "nbformat": 4, + "nbformat_minor": 0, + "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.2" + }, + "colab": { + "name": "rock-paper-scissors.ipynb", + "provenance": [] + } }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Rock, Paper & Scissors\n", - "\n", - "Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n", - "\n", - "## Task\n", - "Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n", - "\n", - "## Tools\n", - "1. Loop: **for/while**\n", - "2. Functions: **input(), print()...**\n", - "3. Conditional statements: **if, elif, else**\n", - "4. Definition of functions. Modular programming\n", - "5. Import modules\n", - "\n", - "**To solve this challenge, the use of functions is recommended.**\n", - "\n", - "#### 1. Import the choice function of the random module." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n", - "Remember that the number of rounds must be odd: 1, 3, 5, ..." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n", - "**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 6. Define a function that randomly returns one of the 3 gesture options.\n", - "You will use this function to simulate the gesture choice of the computer. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n", - "The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 8. Define a function that checks who won a round. \n", - "The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n", - "You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", - "\n", - "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", - "\n", - "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### 11. Print the winner of the game based on who won more rounds.\n", - "Remember that the game might be tied. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Bonus: Rock, Paper, Scissors, Lizard & Spock\n", - "![](images/rpsls.jpg)\n", - "\n", - "In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n", - "\n", - "In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n", - "\n", - "**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game." - ] - }, - { - "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.7.2" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "KvIpODej_d-6" + }, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gqebzgki_d--" + }, + "source": [ + "# Rock, Paper & Scissors\n", + "\n", + "Let's play the famous game against our computer. You can check the rules [here](https://en.wikipedia.org/wiki/Rock%E2%80%93paper%E2%80%93scissors). \n", + "\n", + "## Task\n", + "Create a program that imitates the playability of the well known game of rock, paper, scissors. Follow the guidelines provided.\n", + "\n", + "## Tools\n", + "1. Loop: **for/while**\n", + "2. Functions: **input(), print()...**\n", + "3. Conditional statements: **if, elif, else**\n", + "4. Definition of functions. Modular programming\n", + "5. Import modules\n", + "\n", + "**To solve this challenge, the use of functions is recommended.**\n", + "\n", + "#### 1. Import the choice function of the random module." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZiEU3fHV_d_A" + }, + "source": [ + "from random import choice" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VDC0N5a0_d_B" + }, + "source": [ + "#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pBx3E3MU_d_B" + }, + "source": [ + "gestures = ['rock','paper','scissors']" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "smmbuxKf_d_B" + }, + "source": [ + "#### 3. Create a variable called `n_rounds` to store the maximum number of rounds to play in a game. \n", + "Remember that the number of rounds must be odd: 1, 3, 5, ..." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ToHgKbpa_d_C", + "outputId": "26324ed9-1fad-48aa-b969-a13ae7ea6c1b" + }, + "source": [ + "n_rounds = input(\"How many rounds you want to play? (Hint: should be an odd number) >>> \")\n", + "try:\n", + " n_rounds = int(n_rounds)\n", + " if n_rounds % 2 == 0:\n", + " print(\"That is so NOT an odd number...\")\n", + " n_rounds = 0\n", + " elif n_rounds > 10:\n", + " print(\"I don't have time. Let's try less than 10.\")\n", + " n_rounds = 0\n", + " else:\n", + " print(\"Alright! Let's play \", int(n_rounds), \" rounds.\")\n", + "except ValueError:\n", + " print(\"To play \", n_rounds, \" rounds I'd have to slice your hand in pieces, honey.\")\n", + " n_rounds = 0" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "How many rounds you want to play? (Hint: should be an odd number) >>> 3\n", + "Alright! Let's play 3 rounds.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_Njh5SyN_d_C" + }, + "source": [ + "#### 4. Create a variable called `rounds_to_win` to store the number of rounds that a player must win to win the game.\n", + "**Hint**: the value stored in `rounds_to_win` depends on the value of `n_rounds`. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z-bzVok2_d_C" + }, + "source": [ + "import math\n", + "rounds_to_win = math.ceil(n_rounds/2)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-6zf6RU5_d_D" + }, + "source": [ + "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BRxqrJW2_d_D" + }, + "source": [ + "cpu_score = 0\n", + "player_score = 0" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cvuFoT65_d_D" + }, + "source": [ + "#### 6. Define a function that randomly returns one of the 3 gesture options.\n", + "You will use this function to simulate the gesture choice of the computer. " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-1NFUQFg_d_E" + }, + "source": [ + "### w should be the list of posible gestures\n", + "def T800(w):\n", + " return choice(w);" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "eiNNv_2c_d_E" + }, + "source": [ + "#### 7. Define a function that asks the player which is the gesture he or she wants to show: 'rock', 'paper' or 'scissors'.\n", + "The player should only be allowed to choose one of the 3 gesture options. If the player's choice is not rock, paper or scissors, keep asking until it is." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "wg62Ko7l_d_E" + }, + "source": [ + "### x should also be the gesture options\n", + "def shand(x):\n", + " player = 0\n", + " while player == 0:\n", + " player = input(\"Please choose wisely: \" + str(x) + \" >>> \")\n", + " if player in x:\n", + " return player\n", + " else:\n", + " print(\"You fool. Play by the rules.\")\n", + " player = 0" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Vvs4x5z9_d_E" + }, + "source": [ + "#### 8. Define a function that checks who won a round. \n", + "The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yikw2Vvh_d_E" + }, + "source": [ + "joy = {'rock':['scissors'], 'paper':['rock'], 'scissors':['paper']}\n", + "### y should be the winning dictionary\n", + "def winner(y):\n", + " outcome = 2\n", + " if player == quick_thumb:\n", + " outcome = 0\n", + " elif player in y[quick_thumb]:\n", + " outcome = 1\n", + " return outcome" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8RKDPGXr_d_F" + }, + "source": [ + "#### 9. Define a function that prints the choice of the computer, the choice of the player and a message that announces who won the current round. \n", + "You should also use this function to update the variables that count the number of rounds that the computer and the player have won. The score of the winner increases by one point. If there is a tie, the score does not increase." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SLhPrL_J_d_F" + }, + "source": [ + "### z should be the outcome of the 'winner' function\n", + "def roundin(z):\n", + " result = {0:'Tie!',1:'I win.',2:'You win.'}\n", + " print('You chose ' + player)\n", + " print('I chose ' + quick_thumb)\n", + " print(result[z])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZwuFjPhC_d_F" + }, + "source": [ + "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", + "\n", + "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", + "\n", + "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EtvtiYq9_d_F", + "outputId": "82cfb7a4-142b-45bb-eb24-ff640853dd71" + }, + "source": [ + "rounds_done = 0\n", + "while rounds_done < n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " player = shand(gestures)\n", + " quick_thumb = T800(gestures)\n", + " outcome = winner(joy)\n", + " roundin(outcome)\n", + " rounds_done += 1\n", + " if outcome == 1:\n", + " cpu_score += 1\n", + " elif outcome == 2:\n", + " player_score += 1\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Please choose wisely: ['rock', 'paper', 'scissors'] >>> paper\n", + "You chose paper\n", + "I chose paper\n", + "Tie!\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9J-rI54X_d_F" + }, + "source": [ + "#### 11. Print the winner of the game based on who won more rounds.\n", + "Remember that the game might be tied. " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5ak5tH58_d_F", + "outputId": "dd91d7d9-3b72-45fc-ad99-6ad8516897e0" + }, + "source": [ + "if player_score >= cpu_score:\n", + " print(\"You win. \\nThor: 'Loki, my mind is going.'\")\n", + "elif cpu_score >= player_score:\n", + " print(\"CPU wins! \\nThe Instructor: 'So did the man went down with his choice.'\")\n", + "else:\n", + " print(\"Tie.\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "CPU wins! \n", + "The Instructor: 'So did the man went down with his choice.'\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vYLAdCkq_d_F" + }, + "source": [ + "# Bonus: Rock, Paper, Scissors, Lizard & Spock\n", + "![](https://github.com/ronlicaj/data-prework/blob/master/images/rpsls.jpg?raw=1)\n", + "\n", + "In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n", + "\n", + "In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n", + "\n", + "**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game." + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6Fv-Y4kS_d_G", + "outputId": "90f6b59c-bef5-4ad5-a4d9-fef8f5c8f421" + }, + "source": [ + "gestures2 = ['rock','paper','scissors','lizard','spock']\n", + "joy2 = {'rock':['scissors','lizard'], 'paper':['rock','spock'], 'scissors':['paper','lizard'], 'lizard':['spock','paper'],'spock':['rock','scissors']}\n", + "cpu_score = 0\n", + "player_score = 0\n", + "rounds_done = 0\n", + "while rounds_done < n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " player = shand(gestures2)\n", + " quick_thumb = T800(gestures2)\n", + " outcome = winner(joy2)\n", + " roundin(outcome)\n", + " rounds_done += 1\n", + " if outcome == 1:\n", + " cpu_score += 1\n", + " elif outcome == 2:\n", + " player_score += 1\n", + "if player_score >= cpu_score:\n", + " print(\"You won... \\nThor: 'Loki, my mind is going crazy.'\")\n", + "elif cpu_score >= player_score:\n", + " print(\"CPU wins! \\nThe Instructor: 'So did the man went down with his choice.'\")\n", + "else:\n", + " print(\"Tie.\")" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Please choose wisely: ['rock', 'paper', 'scissors', 'lizard', 'spock'] >>> rock\n", + "You chose rock\n", + "I chose lizard\n", + "You win.\n", + "Please choose wisely: ['rock', 'paper', 'scissors', 'lizard', 'spock'] >>> scissors\n", + "You chose scissors\n", + "I chose rock\n", + "I win.\n", + "Please choose wisely: ['rock', 'paper', 'scissors', 'lizard', 'spock'] >>> lizard\n", + "You chose lizard\n", + "I chose spock\n", + "You win.\n", + "You won... \n", + "Thor: 'Loki, my mind is going crazy.'\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/2.-Statistics/1..png b/2.-Statistics/1..png new file mode 100644 index 000000000..acbe4ccba Binary files /dev/null and b/2.-Statistics/1..png differ diff --git a/2.-Statistics/10..png b/2.-Statistics/10..png new file mode 100644 index 000000000..6477467ba Binary files /dev/null and b/2.-Statistics/10..png differ diff --git a/2.-Statistics/11..png b/2.-Statistics/11..png new file mode 100644 index 000000000..0d7fa21d2 Binary files /dev/null and b/2.-Statistics/11..png differ diff --git a/2.-Statistics/12..png b/2.-Statistics/12..png new file mode 100644 index 000000000..8a0b21fc6 Binary files /dev/null and b/2.-Statistics/12..png differ diff --git a/2.-Statistics/13..png b/2.-Statistics/13..png new file mode 100644 index 000000000..3f7a86591 Binary files /dev/null and b/2.-Statistics/13..png differ diff --git a/2.-Statistics/14..png b/2.-Statistics/14..png new file mode 100644 index 000000000..e1c5d7d80 Binary files /dev/null and b/2.-Statistics/14..png differ diff --git a/2.-Statistics/15..png b/2.-Statistics/15..png new file mode 100644 index 000000000..27e452059 Binary files /dev/null and b/2.-Statistics/15..png differ diff --git a/2.-Statistics/2..png b/2.-Statistics/2..png new file mode 100644 index 000000000..2c7542cc1 Binary files /dev/null and b/2.-Statistics/2..png differ diff --git a/2.-Statistics/3..png b/2.-Statistics/3..png new file mode 100644 index 000000000..1f5dab3ee Binary files /dev/null and b/2.-Statistics/3..png differ diff --git a/2.-Statistics/4..png b/2.-Statistics/4..png new file mode 100644 index 000000000..58993e8ae Binary files /dev/null and b/2.-Statistics/4..png differ diff --git a/2.-Statistics/5..png b/2.-Statistics/5..png new file mode 100644 index 000000000..0463e0ef1 Binary files /dev/null and b/2.-Statistics/5..png differ diff --git a/2.-Statistics/6..png b/2.-Statistics/6..png new file mode 100644 index 000000000..2873f0789 Binary files /dev/null and b/2.-Statistics/6..png differ diff --git a/2.-Statistics/7..png b/2.-Statistics/7..png new file mode 100644 index 000000000..a1f0df7ed Binary files /dev/null and b/2.-Statistics/7..png differ diff --git a/2.-Statistics/8..png b/2.-Statistics/8..png new file mode 100644 index 000000000..b054e9337 Binary files /dev/null and b/2.-Statistics/8..png differ diff --git a/2.-Statistics/9..png b/2.-Statistics/9..png new file mode 100644 index 000000000..445a19a33 Binary files /dev/null and b/2.-Statistics/9..png differ