diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..818880493 Binary files /dev/null and b/.DS_Store differ diff --git a/1.-Python/.DS_Store b/1.-Python/.DS_Store new file mode 100644 index 000000000..f921d0b0f Binary files /dev/null and b/1.-Python/.DS_Store differ diff --git a/1.-Python/1.-Snail-and-Well/.DS_Store b/1.-Python/1.-Snail-and-Well/.DS_Store new file mode 100644 index 000000000..db86b0893 Binary files /dev/null and b/1.-Python/1.-Snail-and-Well/.DS_Store differ diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb similarity index 54% rename from 1.-Python/1.-Snail-and-Well/snail-and-well.ipynb rename to 1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb index 34021448a..6c0866714 100644 --- a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb +++ b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb @@ -30,10 +30,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 " + ] }, { "cell_type": "markdown", @@ -44,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 0" + ] }, { "cell_type": "markdown", @@ -58,10 +65,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail has escaped\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail has escaped\")" + ] }, { "cell_type": "markdown", @@ -72,10 +99,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 13 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")\n" + ] }, { "cell_type": "markdown", @@ -96,10 +145,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 11 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " for i in advance_cm:\n", + " snail_position += i\n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")" + ] }, { "cell_type": "markdown", @@ -111,10 +181,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the maximum displacement is 57\n", + "the minimum displacement is -8\n" + ] + } + ], + "source": [ + "print(\"the maximum displacement is\", max(advance_cm) + nightly_distance)\n", + "print(\"the minimum displacement is\", min(advance_cm) + nightly_distance)" + ] }, { "cell_type": "markdown", @@ -125,10 +207,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the average displacement is 18.09090909090909\n" + ] + } + ], + "source": [ + "avg = (sum(advance_cm) + nightly_distance * days)/len(advance_cm)\n", + "print(\"the average displacement is\", avg)" + ] }, { "cell_type": "markdown", @@ -139,10 +232,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the standard deviation is 26.35234868074828\n" + ] + } + ], + "source": [ + "var = sum((i-avg)**2 for i in advance_cm) / len(advance_cm)\n", + "std = var**0.5\n", + "print(\"the standard deviation is\",std)" + ] } ], "metadata": { @@ -161,7 +266,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well1-checkpoint.ipynb b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well1-checkpoint.ipynb new file mode 100644 index 000000000..20e5bcd43 --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well1-checkpoint.ipynb @@ -0,0 +1,279 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "source": [ + "" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 " + ] + }, + { + "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": 2, + "metadata": {}, + "outputs": [], + "source": [ + "days = 0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail has escaped\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail has escaped\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 13 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")\n" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 11 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " for i in advance_cm:\n", + " snail_position += i\n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")" + ] + }, + { + "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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the maximum displacement is 57\n", + "the minimum displacement is -8\n" + ] + } + ], + "source": [ + "print(\"the maximum displacement is\", max(advance_cm) + nightly_distance)\n", + "print(\"the minimum displacement is\", min(advance_cm) + nightly_distance)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the average displacement is 18.09090909090909\n" + ] + } + ], + "source": [ + "avg = (sum(advance_cm) + nightly_distance * days)/len(advance_cm)\n", + "print(\"the average displacement is\", avg)" + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the standard deviation is 26.35234868074828\n" + ] + } + ], + "source": [ + "var = sum((i-avg)**2 for i in advance_cm) / len(advance_cm)\n", + "std = var**0.5\n", + "print(\"the standard deviation is\",std)" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well1.ipynb b/1.-Python/1.-Snail-and-Well/snail-and-well1.ipynb new file mode 100644 index 000000000..20e5bcd43 --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/snail-and-well1.ipynb @@ -0,0 +1,279 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "-" + } + }, + "source": [ + "" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 " + ] + }, + { + "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": 2, + "metadata": {}, + "outputs": [], + "source": [ + "days = 0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail has escaped\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail has escaped\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 13 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " snail_position += daily_distance \n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")\n" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the snail needs 11 days to escape\n" + ] + } + ], + "source": [ + "well_height = 125\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "snail_position = 0 \n", + "days = 0\n", + "\n", + "while snail_position < well_height:\n", + " for i in advance_cm:\n", + " snail_position += i\n", + " snail_position += nightly_distance\n", + " days += 1\n", + "\n", + "print(\"the snail needs\", days, \"days to escape\")" + ] + }, + { + "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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the maximum displacement is 57\n", + "the minimum displacement is -8\n" + ] + } + ], + "source": [ + "print(\"the maximum displacement is\", max(advance_cm) + nightly_distance)\n", + "print(\"the minimum displacement is\", min(advance_cm) + nightly_distance)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the average displacement is 18.09090909090909\n" + ] + } + ], + "source": [ + "avg = (sum(advance_cm) + nightly_distance * days)/len(advance_cm)\n", + "print(\"the average displacement is\", avg)" + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the standard deviation is 26.35234868074828\n" + ] + } + ], + "source": [ + "var = sum((i-avg)**2 for i in advance_cm) / len(advance_cm)\n", + "std = var**0.5\n", + "print(\"the standard deviation is\",std)" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb b/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb new file mode 100644 index 000000000..71cba6a52 --- /dev/null +++ b/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb @@ -0,0 +1,343 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [], + "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 = 10" + ] + }, + { + "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": 2, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] + }, + { + "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": 3, + "metadata": {}, + "outputs": [], + "source": [ + "for spells in zip(saruman, gandalf):\n", + " gandalf_spell_power = spells[0]\n", + " saruman_spell_power = spells[1]\n", + "\n", + " if gandalf_spell_power>saruman_spell_power:\n", + " gandalf_wins += 1\n", + " elif saruman_spell_power>gandalf_spell_power:\n", + " saruman_wins += 1\n" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Saruman wins\n" + ] + } + ], + "source": [ + "if gandalf_wins > saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "elif gandalf_wins < saruman_wins:\n", + " print(\"Saruman wins\")\n", + "else:\n", + " print(\"It's a tie\")" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [], + "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 = 10\n" + ] + }, + { + "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": 6, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] + }, + { + "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": 7, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_power = []\n", + "saruman_power = []" + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The winner is Gandalf!\n" + ] + } + ], + "source": [ + "for spell in gandalf:\n", + " gandalf_power.append(POWER[spell])\n", + "\n", + "for spell in saruman:\n", + " saruman_power.append(POWER[spell])\n", + "\n", + "winner = \"\"\n", + "\n", + "for g, s in zip(gandalf_power, saruman_power): \n", + " if g > s: \n", + " gandalf_wins+=1\n", + " saruman_wins=0\n", + " elif s > g: \n", + " saruman_wins+=1\n", + " gandalf_wins=0\n", + "\n", + " if gandalf_wins==3:\n", + " winner=\"Gandalf\"\n", + " \n", + " elif saruman_wins==3:\n", + " winner=\"Saruman\"\n", + " \n", + "\n", + "if gandalf_wins < 3 and saruman_wins < 3:\n", + " print(\" noone, it is a Tie.\")\n", + "else:\n", + " print(f\"The winner is {winner}!\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Find the average spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average spell power of Gandalf is 39.0\n", + "The average spell power of Saruman is 30.5\n" + ] + } + ], + "source": [ + "average_gandalf = sum(gandalf_power)/len(gandalf)\n", + "average_saruman = sum(saruman_power)/len(saruman)\n", + "print(\"The average spell power of Gandalf is\", average_gandalf)\n", + "print(\"The average spell power of Saruman is\", average_saruman)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The standard deviation of the spell power of Gandalf is 16.40629960309962\n", + "The standard deviation of the spell power of Saruman is 15.951314818673865\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"The standard deviation of the spell power of Gandalf is\", statistics.stdev(saruman_power))\n", + "print(\"The standard deviation of the spell power of Saruman is\", statistics.stdev(gandalf_power))" + ] + } + ], + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} 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..71cba6a52 100644 --- a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb +++ b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb @@ -49,10 +49,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "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 = 10" + ] }, { "cell_type": "markdown", @@ -64,10 +68,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -78,10 +85,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for spells in zip(saruman, gandalf):\n", + " gandalf_spell_power = spells[0]\n", + " saruman_spell_power = spells[1]\n", + "\n", + " if gandalf_spell_power>saruman_spell_power:\n", + " gandalf_wins += 1\n", + " elif saruman_spell_power>gandalf_spell_power:\n", + " saruman_wins += 1\n" + ] }, { "cell_type": "markdown", @@ -93,10 +109,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Saruman wins\n" + ] + } + ], + "source": [ + "if gandalf_wins > saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "elif gandalf_wins < saruman_wins:\n", + " print(\"Saruman wins\")\n", + "else:\n", + " print(\"It's a tie\")" + ] }, { "cell_type": "markdown", @@ -128,10 +159,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "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 = 10\n" + ] }, { "cell_type": "markdown", @@ -142,10 +187,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -156,10 +204,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = []\n", + "saruman_power = []" + ] }, { "cell_type": "markdown", @@ -171,10 +222,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The winner is Gandalf!\n" + ] + } + ], + "source": [ + "for spell in gandalf:\n", + " gandalf_power.append(POWER[spell])\n", + "\n", + "for spell in saruman:\n", + " saruman_power.append(POWER[spell])\n", + "\n", + "winner = \"\"\n", + "\n", + "for g, s in zip(gandalf_power, saruman_power): \n", + " if g > s: \n", + " gandalf_wins+=1\n", + " saruman_wins=0\n", + " elif s > g: \n", + " saruman_wins+=1\n", + " gandalf_wins=0\n", + "\n", + " if gandalf_wins==3:\n", + " winner=\"Gandalf\"\n", + " \n", + " elif saruman_wins==3:\n", + " winner=\"Saruman\"\n", + " \n", + "\n", + "if gandalf_wins < 3 and saruman_wins < 3:\n", + " print(\" noone, it is a Tie.\")\n", + "else:\n", + " print(f\"The winner is {winner}!\")\n" + ] }, { "cell_type": "markdown", @@ -185,10 +272,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average spell power of Gandalf is 39.0\n", + "The average spell power of Saruman is 30.5\n" + ] + } + ], + "source": [ + "average_gandalf = sum(gandalf_power)/len(gandalf)\n", + "average_saruman = sum(saruman_power)/len(saruman)\n", + "print(\"The average spell power of Gandalf is\", average_gandalf)\n", + "print(\"The average spell power of Saruman is\", average_saruman)\n" + ] }, { "cell_type": "markdown", @@ -199,10 +300,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The standard deviation of the spell power of Gandalf is 16.40629960309962\n", + "The standard deviation of the spell power of Saruman is 15.951314818673865\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"The standard deviation of the spell power of Gandalf is\", statistics.stdev(saruman_power))\n", + "print(\"The standard deviation of the spell power of Saruman is\", statistics.stdev(gandalf_power))" + ] } ], "metadata": { @@ -221,7 +335,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb b/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb new file mode 100644 index 000000000..d2e02ac10 --- /dev/null +++ b/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb @@ -0,0 +1,179 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": 1, + "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": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of stops are 9\n" + ] + } + ], + "source": [ + "number_stops= len(stops)\n", + "print(\"The number of stops are\",number_stops)" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\n" + ] + } + ], + "source": [ + "passengers_count = []\n", + " \n", + "for stop in stops:\n", + " passengers_diff = stop[0] - stop[1]\n", + " if (len(passengers_count) == 0):\n", + " passengers_count.append(passengers_diff)\n", + " else:\n", + " passengers_count.append(passengers_count[-1] + passengers_diff)\n", + "print(passengers_count)\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the maximum occupation of the bus." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The maximum occupation of the bus is 14\n" + ] + } + ], + "source": [ + "print(\"The maximum occupation of the bus is\",max(passengers_count))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average occupation of the bus is 9.333333333333334\n", + "The standard deviation is 3.391164991562634\n" + ] + } + ], + "source": [ + "average_occupation = sum(passengers_count) / len(passengers_count)\n", + "print(\"The average occupation of the bus is\",average_occupation)\n", + "import statistics\n", + "print(\"The standard deviation is\",statistics.stdev(passengers_count))" + ] + } + ], + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8fd..d2e02ac10 100644 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The number of stops are 9\n" + ] + } + ], + "source": [ + "number_stops= len(stops)\n", + "print(\"The number of stops are\",number_stops)" + ] }, { "cell_type": "markdown", @@ -67,10 +78,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\n" + ] + } + ], + "source": [ + "passengers_count = []\n", + " \n", + "for stop in stops:\n", + " passengers_diff = stop[0] - stop[1]\n", + " if (len(passengers_count) == 0):\n", + " passengers_count.append(passengers_diff)\n", + " else:\n", + " passengers_count.append(passengers_count[-1] + passengers_diff)\n", + "print(passengers_count)\n", + " " + ] }, { "cell_type": "markdown", @@ -81,10 +111,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The maximum occupation of the bus is 14\n" + ] + } + ], + "source": [ + "print(\"The maximum occupation of the bus is\",max(passengers_count))" + ] }, { "cell_type": "markdown", @@ -95,10 +135,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average occupation of the bus is 9.333333333333334\n", + "The standard deviation is 3.391164991562634\n" + ] + } + ], + "source": [ + "average_occupation = sum(passengers_count) / len(passengers_count)\n", + "print(\"The average occupation of the bus is\",average_occupation)\n", + "import statistics\n", + "print(\"The standard deviation is\",statistics.stdev(passengers_count))" + ] } ], "metadata": { @@ -117,7 +171,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb b/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb new file mode 100644 index 000000000..2e3a8215e --- /dev/null +++ b/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb @@ -0,0 +1,235 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": 2, + "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robin Hood hit the arrow with another arrow at 2 2\n", + "Robin Hood hit the arrow with another arrow at 2 2\n", + "Robin Hood hit the arrow with another arrow at 9 9\n" + ] + } + ], + "source": [ + "for point in points:\n", + " if point[0] == point[1]:\n", + " print(\"Robin Hood hit the arrow with another arrow at\", point[0], point[1])\n", + " " + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In Q1 there are 10 arrows. In Q2 there are 6 arrows. In Q3 there are 2 arrows. In Q4 there are 2 arrows.\n" + ] + } + ], + "source": [ + "q1= 0\n", + "q2= 0\n", + "q3= 0\n", + "q4= 0\n", + "\n", + "for point in points:\n", + " if point[0] > 0 and point [1] > 0:\n", + " q1 += 1\n", + " elif point[0] < 0 and point [1] > 0:\n", + " q2 += 1\n", + " elif point[0] < 0 and point [1] < 0:\n", + " q3 += 1\n", + " elif point[0] > 0 and point [1] < 0:\n", + " q4 += 1\n", + "print(\"In Q1 there are\",q1,\"arrows.\",\"In Q2 there are\",q2,\"arrows.\",\"In Q3 there are\",q3,\"arrows.\",\"In Q4 there are\",q4,\"arrows.\")\n", + " \n", + " " + ] + }, + { + "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": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "distances = []\n", + "\n", + "i = 0\n", + "import math\n", + "while i < len(points):\n", + "\n", + " a = math.sqrt(math.pow(points[i][0],2) + math.pow(points[i][1],2))\n", + " distances.append(a)\n", + " i += 1\n", + "get_min_distance = min(distances)\n", + "print(get_min_distance)\n", + "\n" + ] + }, + { + "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": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 2 arrows on the ground\n" + ] + } + ], + "source": [ + "\n", + "count_min = 0\n", + "count_max = 0\n", + "store_min_pos = []\n", + "count_position = 0\n", + "\n", + "for i in distances:\n", + "\n", + " if get_min_distance == i:\n", + " store_min_pos.append(count_position)\n", + " count_min += 1\n", + " elif i > 9:\n", + " count_max += 1\n", + " count_position += 1\n", + "\n", + "arrow = []\n", + "\n", + "\n", + "if count_max > 1:\n", + " print(\"There are %d arrows on the ground\" % count_max)\n", + "elif count_max == 0:\n", + " print(\"There are no arrows on the ground\")\n", + "else:\n", + " print(\"There is just one arrow on the ground\")" + ] + }, + { + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/4.-Robin-Hood/robin-hood.ipynb b/1.-Python/4.-Robin-Hood/robin-hood.ipynb index 01de29d3b..2e3a8215e 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -55,10 +55,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robin Hood hit the arrow with another arrow at 2 2\n", + "Robin Hood hit the arrow with another arrow at 2 2\n", + "Robin Hood hit the arrow with another arrow at 9 9\n" + ] + } + ], + "source": [ + "for point in points:\n", + " if point[0] == point[1]:\n", + " print(\"Robin Hood hit the arrow with another arrow at\", point[0], point[1])\n", + " " + ] }, { "cell_type": "markdown", @@ -70,10 +85,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "In Q1 there are 10 arrows. In Q2 there are 6 arrows. In Q3 there are 2 arrows. In Q4 there are 2 arrows.\n" + ] + } + ], + "source": [ + "q1= 0\n", + "q2= 0\n", + "q3= 0\n", + "q4= 0\n", + "\n", + "for point in points:\n", + " if point[0] > 0 and point [1] > 0:\n", + " q1 += 1\n", + " elif point[0] < 0 and point [1] > 0:\n", + " q2 += 1\n", + " elif point[0] < 0 and point [1] < 0:\n", + " q3 += 1\n", + " elif point[0] > 0 and point [1] < 0:\n", + " q4 += 1\n", + "print(\"In Q1 there are\",q1,\"arrows.\",\"In Q2 there are\",q2,\"arrows.\",\"In Q3 there are\",q3,\"arrows.\",\"In Q4 there are\",q4,\"arrows.\")\n", + " \n", + " " + ] }, { "cell_type": "markdown", @@ -88,10 +129,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "distances = []\n", + "\n", + "i = 0\n", + "import math\n", + "while i < len(points):\n", + "\n", + " a = math.sqrt(math.pow(points[i][0],2) + math.pow(points[i][1],2))\n", + " distances.append(a)\n", + " i += 1\n", + "get_min_distance = min(distances)\n", + "print(get_min_distance)\n", + "\n" + ] }, { "cell_type": "markdown", @@ -101,6 +163,46 @@ "**Hint**: Use the function created in step 3. " ] }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 2 arrows on the ground\n" + ] + } + ], + "source": [ + "\n", + "count_min = 0\n", + "count_max = 0\n", + "store_min_pos = []\n", + "count_position = 0\n", + "\n", + "for i in distances:\n", + "\n", + " if get_min_distance == i:\n", + " store_min_pos.append(count_position)\n", + " count_min += 1\n", + " elif i > 9:\n", + " count_max += 1\n", + " count_position += 1\n", + "\n", + "arrow = []\n", + "\n", + "\n", + "if count_max > 1:\n", + " print(\"There are %d arrows on the ground\" % count_max)\n", + "elif count_max == 0:\n", + " print(\"There are no arrows on the ground\")\n", + "else:\n", + " print(\"There is just one arrow on the ground\")" + ] + }, { "cell_type": "code", "execution_count": null, @@ -125,7 +227,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb b/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb new file mode 100644 index 000000000..f24d2c5c3 --- /dev/null +++ b/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb @@ -0,0 +1,467 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": 2, + "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "minimum_temp = min(temperatures_C)\n", + "print(minimum_temp)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Find the maximum temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n" + ] + } + ], + "source": [ + "maximum_temp = max(temperatures_C)\n", + "print(maximum_temp)" + ] + }, + { + "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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "over_equal_70 = []\n", + "for i in temperatures_C:\n", + " if i >= 70:\n", + " over_equal_70.append(i)\n", + "\n", + "print(over_equal_70)\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Find the average temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "average_temp = sum(temperatures_C)/ len(temperatures_C)\n", + "print(average_temp)" + ] + }, + { + "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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[33, 66, 65, 54.666666666666664, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n" + ] + } + ], + "source": [ + "replace_bad_temp = []\n", + "\n", + " # get average of prior results if 0\n", + "\n", + "for i in temperatures_C:\n", + " if i == 0:\n", + " replace_bad_temp.append(sum(replace_bad_temp)/len(replace_bad_temp))\n", + " else:\n", + " replace_bad_temp.append(i)\n", + "\n", + "temperatures_C = replace_bad_temp\n", + "\n", + "print(temperatures_C)\n", + "\n", + " " + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[91.4, 150.8, 149.0, 130.39999999999998, 138.2, 140.0, 143.60000000000002, 147.2, 158.0, 168.8, 176.0, 177.8, 176.0, 181.4, 194.0, 174.20000000000002, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n" + ] + } + ], + "source": [ + "fahrenheit_temps = []\n", + "for i in temperatures_C:\n", + " fahrenheit_temps.append((i*1.8)+32)\n", + "print(fahrenheit_temps)" + ] + }, + { + "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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Change system\n" + ] + } + ], + "source": [ + "def make_decision(temperatures_C):\n", + "\n", + "\n", + " plus_70_warning = 0\n", + "\n", + " for i in temperatures_C:\n", + " if i >= 70:\n", + " plus_70_warning+=1\n", + "\n", + " if plus_70_warning > 4 or any(i > 80 for i in temperatures_C) or sum(temperatures_C)/len(temperatures_C) > 65:\n", + " print('Change system')\n", + " else:\n", + " print('System ok')\n", + "\n", + "make_decision(temperatures_C) " + ] + }, + { + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "over_70 = []\n", + "for i in temperatures_C:\n", + " if i > 70:\n", + " over_70.append(i)\n", + "\n", + "print(over_70)" + ] + }, + { + "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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature is greater than 70ºC during more than 4 consecutive hours.\n" + ] + } + ], + "source": [ + "if len(over_70) > 4:\n", + " print(\"The temperature is greater than 70ºC during more than 4 consecutive hours.\")\n", + "else:\n", + " print(\"No changes required\")" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No changes required\n" + ] + } + ], + "source": [ + "over_80 =[]\n", + "for i in temperatures_C:\n", + " if i > 80:\n", + " over_80.append(i)\n", + " \n", + "if len(over_70) > 4 and len(over_80) > 0 and average_temp > 65:\n", + " print(\"System needs to be changed\")\n", + "else:\n", + " print(\"No changes required\")" + ] + }, + { + "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": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average of C 60.25\n", + "average of F 144.54999999999998\n", + "The average of F is greater than C\n" + ] + } + ], + "source": [ + "print(\"average of C\",average_temp) \n", + "average_f = sum(fahrenheit_temps)/len(fahrenheit_temps)\n", + " \n", + "print(\"average of F\",average_f)\n", + "\n", + "if average_temp > average_f:\n", + " print(\"The average of C is greater than F\")\n", + "else:\n", + " print(\"The average of F is greater than C\")\n" + ] + }, + { + "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": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "standard deviation of C 15.040658797713721\n", + "standard deviation of F 27.073185835884697\n", + "The standard deviation of F is greater than C\n" + ] + } + ], + "source": [ + "import statistics\n", + "\n", + "standard_dev_C =(statistics.stdev(temperatures_C))\n", + "print(\"standard deviation of C\",standard_dev_C)\n", + "standard_dev_F =(statistics.stdev(fahrenheit_temps))\n", + "print(\"standard deviation of F\", standard_dev_F)\n", + "\n", + "if standard_dev_C > standard_dev_F:\n", + " print(\"The standard deviation of C is greater than F\")\n", + "else:\n", + " print(\"The standard deviation of F is greater than C\")" + ] + }, + { + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa20..974d79248 100644 --- a/1.-Python/5.-Temperature-Processor/temperature.ipynb +++ b/1.-Python/5.-Temperature-Processor/temperature.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "minimum_temp = min(temperatures_C)\n", + "print(minimum_temp)" + ] }, { "cell_type": "markdown", @@ -67,10 +78,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n" + ] + } + ], + "source": [ + "maximum_temp = max(temperatures_C)\n", + "print(maximum_temp)" + ] }, { "cell_type": "markdown", @@ -81,10 +103,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "over_equal_70 = []\n", + "for i in temperatures_C:\n", + " if i >= 70:\n", + " over_equal_70.append(i)\n", + "\n", + "print(over_equal_70)\n", + " \n" + ] }, { "cell_type": "markdown", @@ -95,10 +133,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "average_temp = sum(temperatures_C)/ len(temperatures_C)\n", + "print(average_temp)" + ] }, { "cell_type": "markdown", @@ -109,10 +158,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[33, 66, 65, 54.666666666666664, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n" + ] + } + ], + "source": [ + "replace_bad_temp = []\n", + "\n", + " # get average of prior results if 0\n", + "\n", + "for i in temperatures_C:\n", + " if i == 0:\n", + " replace_bad_temp.append(sum(replace_bad_temp)/len(replace_bad_temp))\n", + " else:\n", + " replace_bad_temp.append(i)\n", + "\n", + "temperatures_C = replace_bad_temp\n", + "\n", + "print(temperatures_C)\n", + "\n", + " " + ] }, { "cell_type": "markdown", @@ -128,10 +201,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[91.4, 150.8, 149.0, 130.39999999999998, 138.2, 140.0, 143.60000000000002, 147.2, 158.0, 168.8, 176.0, 177.8, 176.0, 181.4, 194.0, 174.20000000000002, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n" + ] + } + ], + "source": [ + "fahrenheit_temps = []\n", + "for i in temperatures_C:\n", + " fahrenheit_temps.append((i*1.8)+32)\n", + "print(fahrenheit_temps)" + ] }, { "cell_type": "markdown", @@ -150,10 +236,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Change system\n" + ] + } + ], + "source": [ + "def make_decision(temperatures_C):\n", + "\n", + "\n", + " plus_70_warning = 0\n", + "\n", + " for i in temperatures_C:\n", + " if i >= 70:\n", + " plus_70_warning+=1\n", + "\n", + " if plus_70_warning > 4 or any(i > 80 for i in temperatures_C) or sum(temperatures_C)/len(temperatures_C) > 65:\n", + " print('Change system')\n", + " else:\n", + " print('System ok')\n", + "\n", + "make_decision(temperatures_C) " + ] }, { "cell_type": "markdown", @@ -175,10 +285,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "over_70 = []\n", + "for i in temperatures_C:\n", + " if i > 70:\n", + " over_70.append(i)\n", + "\n", + "print(over_70)" + ] }, { "cell_type": "markdown", @@ -189,10 +314,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature is greater than 70ºC during more than 4 consecutive hours.\n" + ] + } + ], + "source": [ + "if len(over_70) > 4:\n", + " print(\"The temperature is greater than 70ºC during more than 4 consecutive hours.\")\n", + "else:\n", + " print(\"No changes required\")" + ] }, { "cell_type": "markdown", @@ -204,10 +342,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No changes required\n" + ] + } + ], + "source": [ + "over_80 =[]\n", + "for i in temperatures_C:\n", + " if i > 80:\n", + " over_80.append(i)\n", + " \n", + "if len(over_70) > 4 and len(over_80) > 0 and average_temp > 65:\n", + " print(\"System needs to be changed\")\n", + "else:\n", + " print(\"No changes required\")" + ] }, { "cell_type": "markdown", @@ -218,10 +374,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average of C 60.25\n", + "average of F 144.54999999999998\n", + "The average of F is greater than C\n" + ] + } + ], + "source": [ + "print(\"average of C\",average_temp) \n", + "average_f = sum(fahrenheit_temps)/len(fahrenheit_temps)\n", + " \n", + "print(\"average of F\",average_f)\n", + "\n", + "if average_temp > average_f:\n", + " print(\"The average of C is greater than F\")\n", + "else:\n", + " print(\"The average of F is greater than C\")\n" + ] }, { "cell_type": "markdown", @@ -230,6 +406,33 @@ "#### 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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "standard deviation of C 15.040658797713721\n", + "standard deviation of F 27.073185835884697\n", + "the relation between of standard deviation of the temperature lists (ºC and ºF) is 1.8\n" + ] + } + ], + "source": [ + "import statistics\n", + "\n", + "standard_dev_C =(statistics.stdev(temperatures_C))\n", + "print(\"standard deviation of C\",standard_dev_C)\n", + "standard_dev_F =(statistics.stdev(fahrenheit_temps))\n", + "print(\"standard deviation of F\", standard_dev_F)\n", + "\n", + "\n", + "print(\"the relation between of standard deviation of the temperature lists (ºC and ºF) is \", round(standard_dev_F/standard_dev_C,2))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -254,7 +457,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" new file mode 100644 index 000000000..22b8a10eb --- /dev/null +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" @@ -0,0 +1,567 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import random\n" + ] + }, + { + "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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "gestures = [\"rock\", \"paper\", \"scissors\"]" + ] + }, + { + "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": 14, + "metadata": {}, + "outputs": [], + "source": [ + "n_rounds = 5" + ] + }, + { + "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": 15, + "metadata": {}, + "outputs": [], + "source": [ + "rounds_to_win = 3" + ] + }, + { + "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": 16, + "metadata": {}, + "outputs": [], + "source": [ + "cpu_score = 0\n", + "player_score = 0" + ] + }, + { + "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": 17, + "metadata": {}, + "outputs": [], + "source": [ + "def cpu():\n", + " cpu_choice = random.choice(gestures)\n", + "\n", + " return cpu_choice\n" + ] + }, + { + "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": 18, + "metadata": {}, + "outputs": [], + "source": [ + "def pick():\n", + " choose_gesture = input(\"choose your gesture \")\n", + " while choose_gesture != gestures:\n", + " if choose_gesture == \"rock\":\n", + " print(\"rock\")\n", + " break\n", + " elif choose_gesture == \"paper\":\n", + " print(\"paper\")\n", + " break\n", + " elif choose_gesture == \"scissors\":\n", + " print(\"scissors\")\n", + " break\n", + " else:\n", + " print(\"wrong input\")\n", + " choose_gesture = input(\"choose your gesture \")\n", + " return choose_gesture\n", + "\n", + "\n", + " \n" + ] + }, + { + "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": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture Player rock\n", + "Computer chooses paper\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def Rules():\n", + " print(\"rock,paper or scissors?\")\n", + " choose_gesture = input(\"choose your gesture Player \")\n", + " player = choose_gesture\n", + " computer = print(\"Computer chooses\", cpu())\n", + " \n", + "\n", + " if player == cpu:\n", + " #tie\n", + " return 0\n", + " elif player == \"rock\":\n", + " if cpu == \"paper\":\n", + " #cpu wins\n", + " return 2\n", + " else:\n", + " #player wins\n", + " return 1\n", + " elif player == \"paper\":\n", + " if cpu == \"rock\":\n", + " return 1\n", + " else:\n", + " return 2\n", + " else:\n", + " if cpu == \"rock\":\n", + " return 2\n", + " else:\n", + " return 1 \n", + "Rules()\n" + ] + }, + { + "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": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n" + ] + }, + { + "data": { + "text/plain": [ + "(0, 1)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def choices(player_score, cpu_score):\n", + " print(\"rock,paper or scissors?\")\n", + " choose_gesture = pick()\n", + " player_choice = choose_gesture\n", + "\n", + " cpu_choice = cpu()\n", + " print(\"Computer chooses\", cpu_choice)\n", + "\n", + " if player_choice == cpu_choice:\n", + " print(\"Tie\")\n", + " elif player_choice == 'rock':\n", + " if cpu_choice == \"paper\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'paper':\n", + " if cpu_choice == \"scissors\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " else:\n", + " if cpu_choice == \"rock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + "\n", + " return player_score, cpu_score\n", + "\n", + "choices(player_score, cpu_score)" + ] + }, + { + "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": "raw", + "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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses scissors\n", + "player wins\n", + "Player score is 1\n", + "Computer score is 0\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 1\n", + "Computer score is 1\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses scissors\n", + "player wins\n", + "Player score is 2\n", + "Computer score is 1\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 2\n", + "Computer score is 2\n", + "rock,paper or scissors?\n" + ] + } + ], + "source": [ + "def main():\n", + " cpu_score = 0\n", + " player_score = 0\n", + " n_rounds = 5\n", + " while not n_rounds == 0:\n", + " player_score,cpu_score = choices(player_score, cpu_score)\n", + " print(\"Player score is\",player_score)\n", + " print(\"Computer score is\", cpu_score)\n", + " n_rounds -= 1\n", + " \n", + " if cpu_score > player_score:\n", + " print(\"\\nUnfortunately, the computer is the winner.\")\n", + " if cpu_score < player_score: \n", + " print(\"\\nYeah, the player is the winner!\")\n", + "\n", + "main()\n", + " " + ] + }, + { + "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": 146, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter an odd number 3\n", + "This is an odd number.\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture paper\n", + "paper\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 1\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture spock\n", + "spock\n", + "Computer chooses paper\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 2\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture lizard\n", + "lizard\n", + "Computer chooses paper\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 3\n", + "\n", + "Unfortunately, the computer is the winner.\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "\n", + "def rounds():\n", + " n_rounds = int(input(\"Enter an odd number \"))\n", + " mod = n_rounds % 2\n", + " if mod > 0:\n", + " print(\"This is an odd number.\")\n", + " else:\n", + " print(\"This is an even number. Please insert an odd number \")\n", + " rounds()\n", + " return n_rounds \n", + " \n", + "\n", + "\n", + "def cpu():\n", + " cpu_choice = random.choice(gestures)\n", + "\n", + " return cpu_choice\n", + "\n", + "def pick():\n", + " choose_gesture = input(\"choose your gesture \")\n", + " while choose_gesture != gestures:\n", + " if choose_gesture == \"rock\":\n", + " print(\"rock\")\n", + " break\n", + " elif choose_gesture == \"paper\":\n", + " print(\"paper\")\n", + " break\n", + " elif choose_gesture == \"scissors\":\n", + " print(\"scissors\")\n", + " break\n", + " elif choose_gesture == \"lizard\":\n", + " print(\"lizard\")\n", + " break\n", + " elif choose_gesture == \"spock\":\n", + " print(\"spock\")\n", + " break\n", + " else:\n", + " print(\"wrong input\")\n", + " choose_gesture = input(\"choose your gesture \")\n", + " return choose_gesture\n", + "\n", + "def choices(player_score, cpu_score):\n", + " print(\"rock,paper,lizard, spock or scissors?\")\n", + " choose_gesture = pick()\n", + " player_choice = choose_gesture\n", + "\n", + " cpu_choice = cpu()\n", + " print(\"Computer chooses\", cpu_choice)\n", + "\n", + " if player_choice == cpu_choice:\n", + " #tie\n", + " print(\"Tie\")\n", + " elif player_choice == 'rock':\n", + " if cpu_choice == \"paper\" or \"spock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'paper':\n", + " if cpu_choice == \"scissors\" or \"lizard\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'scissors':\n", + " if cpu_choice == \"rock\" or \"spock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'lizard':\n", + " if cpu_choice == \"rock\" or \"scissors\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " else:\n", + " if cpu_choice == \"lizard\" or \"paper\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + "\n", + " return player_score, cpu_score\n", + "\n", + "\n", + "def main():\n", + " cpu_score = 0\n", + " player_score = 0\n", + " n_rounds = rounds()\n", + " while not n_rounds == 0:\n", + " player_score, cpu_score = choices(player_score, cpu_score)\n", + " print(\"Player score is\",player_score)\n", + " print(\"Computer score is\", cpu_score)\n", + " n_rounds -= 1\n", + " if cpu_score > player_score:\n", + " print(\"\\nUnfortunately, the computer is the winner.\")\n", + " else:\n", + " print(\"\\nYeah, the player is the winner!\")\n", + "\n", + "main()\n" + ] + } + ], + "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.9.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} 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..16e4065a7 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" @@ -32,10 +32,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "\n", + "import random\n" + ] }, { "cell_type": "markdown", @@ -46,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gestures = [\"rock\", \"paper\", \"scissors\"]" + ] }, { "cell_type": "markdown", @@ -61,10 +66,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n_rounds = 5" + ] }, { "cell_type": "markdown", @@ -76,10 +83,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "rounds_to_win = 3" + ] }, { "cell_type": "markdown", @@ -90,10 +99,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "cpu_score = 0\n", + "player_score = 0" + ] }, { "cell_type": "markdown", @@ -105,10 +117,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def cpu():\n", + " cpu_choice = random.choice(gestures)\n", + "\n", + " return cpu_choice\n" + ] }, { "cell_type": "markdown", @@ -120,10 +137,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def pick():\n", + " choose_gesture = input(\"choose your gesture \")\n", + " while choose_gesture != gestures:\n", + " if choose_gesture == \"rock\":\n", + " print(\"rock\")\n", + " break\n", + " elif choose_gesture == \"paper\":\n", + " print(\"paper\")\n", + " break\n", + " elif choose_gesture == \"scissors\":\n", + " print(\"scissors\")\n", + " break\n", + " else:\n", + " print(\"wrong input\")\n", + " choose_gesture = input(\"choose your gesture \")\n", + " return choose_gesture\n", + "\n", + "\n", + " \n" + ] }, { "cell_type": "markdown", @@ -135,10 +172,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture Player rock\n", + "Computer chooses paper\n" + ] + }, + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def Rules():\n", + " print(\"rock,paper or scissors?\")\n", + " choose_gesture = input(\"choose your gesture Player \")\n", + " player = choose_gesture\n", + " computer = print(\"Computer chooses\", cpu())\n", + " \n", + "\n", + " if player == cpu:\n", + " #tie\n", + " return 0\n", + " elif player == \"rock\":\n", + " if cpu == \"paper\":\n", + " #cpu wins\n", + " return 2\n", + " else:\n", + " #player wins\n", + " return 1\n", + " elif player == \"paper\":\n", + " if cpu == \"rock\":\n", + " return 1\n", + " else:\n", + " return 2\n", + " else:\n", + " if cpu == \"rock\":\n", + " return 2\n", + " else:\n", + " return 1 \n", + "Rules()\n" + ] }, { "cell_type": "markdown", @@ -150,10 +236,68 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n" + ] + }, + { + "data": { + "text/plain": [ + "(0, 1)" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def choices(player_score, cpu_score):\n", + " print(\"rock,paper or scissors?\")\n", + " choose_gesture = pick()\n", + " player_choice = choose_gesture\n", + "\n", + " cpu_choice = cpu()\n", + " print(\"Computer chooses\", cpu_choice)\n", + "\n", + " if player_choice == cpu_choice:\n", + " print(\"Tie\")\n", + " elif player_choice == 'rock':\n", + " if cpu_choice == \"paper\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'paper':\n", + " if cpu_choice == \"scissors\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " else:\n", + " if cpu_choice == \"rock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + "\n", + " return player_score, cpu_score\n", + "\n", + "choices(player_score, cpu_score)" + ] }, { "cell_type": "markdown", @@ -167,14 +311,7 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", + "cell_type": "raw", "metadata": {}, "source": [ "#### 11. Print the winner of the game based on who won more rounds.\n", @@ -183,10 +320,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses scissors\n", + "player wins\n", + "Player score is 1\n", + "Computer score is 0\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 1\n", + "Computer score is 1\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses scissors\n", + "player wins\n", + "Player score is 2\n", + "Computer score is 1\n", + "rock,paper or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 2\n", + "Computer score is 2\n", + "rock,paper or scissors?\n" + ] + }, + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"\\nYeah, the player is the winner!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mn_rounds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mn_rounds\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mplayer_score\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcpu_score\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mchoices\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplayer_score\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcpu_score\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Player score is\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mplayer_score\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Computer score is\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcpu_score\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mchoices\u001b[0;34m(player_score, cpu_score)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mchoices\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mplayer_score\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcpu_score\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"rock,paper or scissors?\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mchoose_gesture\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpick\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mplayer_choice\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mchoose_gesture\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m\u001b[0m in \u001b[0;36mpick\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mpick\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mchoose_gesture\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"choose your gesture \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mchoose_gesture\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mgestures\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mchoose_gesture\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"rock\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"rock\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/anaconda3/envs/DA_Environment/lib/python3.9/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 859\u001b[0m )\n\u001b[0;32m--> 860\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/anaconda3/envs/DA_Environment/lib/python3.9/site-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 902\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 904\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 905\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user" + ] + } + ], + "source": [ + "def main():\n", + " cpu_score = 0\n", + " player_score = 0\n", + " n_rounds = 5\n", + " while not n_rounds == 0:\n", + " player_score,cpu_score = choices(player_score, cpu_score)\n", + " print(\"Player score is\",player_score)\n", + " print(\"Computer score is\", cpu_score)\n", + " n_rounds -= 1\n", + " \n", + " if cpu_score > player_score:\n", + " print(\"\\nUnfortunately, the computer is the winner.\")\n", + " if cpu_score < player_score: \n", + " print(\"\\nYeah, the player is the winner!\")\n", + "\n", + "main()\n", + " " + ] }, { "cell_type": "markdown", @@ -202,6 +409,165 @@ "**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": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter an odd number 4\n", + "This is an even number. Please insert an odd number \n", + "Enter an odd number 3\n", + "This is an odd number.\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture rock\n", + "rock\n", + "Computer chooses scissors\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 1\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture lizard\n", + "lizard\n", + "Computer chooses rock\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 2\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture spock\n", + "spock\n", + "Computer chooses spock\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 3\n", + "rock,paper,lizard, spock or scissors?\n", + "choose your gesture paper\n", + "paper\n", + "Computer chooses spock\n", + "computer wins\n", + "Player score is 0\n", + "Computer score is 4\n", + "\n", + "Unfortunately, the computer is the winner.\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "\n", + "def rounds():\n", + " n_rounds = int(input(\"Enter an odd number \"))\n", + " mod = n_rounds % 2\n", + " if mod > 0:\n", + " print(\"This is an odd number.\")\n", + " else:\n", + " print(\"This is an even number. Please insert an odd number \")\n", + " rounds()\n", + " return n_rounds \n", + " \n", + "\n", + "\n", + "def cpu():\n", + " cpu_choice = random.choice(gestures)\n", + "\n", + " return cpu_choice\n", + "\n", + "def pick():\n", + " choose_gesture = input(\"choose your gesture \")\n", + " while choose_gesture != gestures:\n", + " if choose_gesture == \"rock\":\n", + " print(\"rock\")\n", + " break\n", + " elif choose_gesture == \"paper\":\n", + " print(\"paper\")\n", + " break\n", + " elif choose_gesture == \"scissors\":\n", + " print(\"scissors\")\n", + " break\n", + " elif choose_gesture == \"lizard\":\n", + " print(\"lizard\")\n", + " break\n", + " elif choose_gesture == \"spock\":\n", + " print(\"spock\")\n", + " break\n", + " else:\n", + " print(\"wrong input\")\n", + " choose_gesture = input(\"choose your gesture \")\n", + " return choose_gesture\n", + "\n", + "def choices(player_score, cpu_score):\n", + " print(\"rock,paper,lizard, spock or scissors?\")\n", + " choose_gesture = pick()\n", + " player_choice = choose_gesture\n", + "\n", + " cpu_choice = cpu()\n", + " print(\"Computer chooses\", cpu_choice)\n", + "\n", + " if player_choice == cpu_choice:\n", + " #tie\n", + " print(\"Tie\")\n", + " elif player_choice == 'rock':\n", + " if cpu_choice == \"paper\" or \"spock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'paper':\n", + " if cpu_choice == \"scissors\" or \"lizard\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'scissors':\n", + " if cpu_choice == \"rock\" or \"spock\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " elif player_choice == 'lizard':\n", + " if cpu_choice == \"rock\" or \"scissors\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + " else:\n", + " if cpu_choice == \"lizard\" or \"paper\":\n", + " print(\"computer wins\")\n", + " cpu_score+=1\n", + " else:\n", + " print(\"player wins\")\n", + " player_score += 1\n", + "\n", + " return player_score, cpu_score\n", + "\n", + "\n", + "def main():\n", + " cpu_score = 0\n", + " player_score = 0\n", + " n_rounds = rounds()\n", + " while not n_rounds == 0:\n", + " player_score, cpu_score = choices(player_score, cpu_score)\n", + " print(\"Player score is\",player_score)\n", + " print(\"Computer score is\", cpu_score)\n", + " n_rounds -= 1\n", + " \n", + " if cpu_score > player_score:\n", + " print(\"\\nUnfortunately, the computer is the winner.\")\n", + " else:\n", + " print(\"\\nYeah, the player is the winner!\")\n", + "\n", + "main()\n" + ] + }, { "cell_type": "code", "execution_count": null, @@ -226,7 +592,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/2.-Statistics/Jule Twelkemeier Coursera course completed.png b/2.-Statistics/Jule Twelkemeier Coursera course completed.png new file mode 100644 index 000000000..e0544e2da Binary files /dev/null and b/2.-Statistics/Jule Twelkemeier Coursera course completed.png differ diff --git a/2.-Statistics/JuleTwelkemeier Coursera course grades.png b/2.-Statistics/JuleTwelkemeier Coursera course grades.png new file mode 100644 index 000000000..619fac901 Binary files /dev/null and b/2.-Statistics/JuleTwelkemeier Coursera course grades.png differ diff --git a/robot.md b/robot.md new file mode 100644 index 000000000..e69de29bb