diff --git a/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb new file mode 100644 index 000000000..5ef0273ba --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb @@ -0,0 +1,169 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create a variable `days` to keep count of the days that pass until the snail escapes the well. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bonus\n", + "The distance traveled by the snail each day is now defined by a list.\n", + "```\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "```\n", + "On the first day, the snail rises 30cm but during the night it slides 20cm. On the second day, the snail rises 21cm but during the night it slides 20cm, and so on. \n", + "\n", + "#### 1. How many days does it take for the snail to escape the well?\n", + "Follow the same guidelines as in the previous challenge.\n", + "\n", + "**Hint**: Remember that the snail gets out of the well when it surpasses the 125cm of height." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. What is its maximum displacement in one day? And its minimum? Calculate the displacement using only the travel distance of the days used to get out of the well. \n", + "**Hint**: Remember that displacement means the total distance risen taking into account that the snail slides at night. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb index 34021448a..c41ea73f4 100644 --- a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb +++ b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb @@ -30,10 +30,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "well_height = 125\n", + "daily_distance = 30 \n", + "nightly_distance = 20\n", + "snail_position = 0\n", + "\n" + ] }, { "cell_type": "markdown", @@ -44,10 +50,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 45, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 0" + ] }, { "cell_type": "markdown", @@ -58,10 +66,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 46, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while (snail_position < well_height) :\n", + " snail_position = (daily_distance - nightly_distance) + snail_position\n", + " days += 1 \n", + "\n", + " " + ] }, { "cell_type": "markdown", @@ -72,10 +86,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n" + ] + } + ], + "source": [ + "print(days)" + ] }, { "cell_type": "markdown", @@ -96,10 +120,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 48, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "11\n", + "24\n", + "81\n", + "105\n", + "130\n", + "days: 6\n" + ] + } + ], + "source": [ + "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", + " daily_distance = advance_cm[days]\n", + " snail_position = (daily_distance - nightly_distance) + snail_position\n", + " print (snail_position)\n", + " days += 1 \n", + " \n", + "print(\"days: \", days)" + ] }, { "cell_type": "markdown", @@ -111,10 +161,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "max_distance: 57\n", + "min_distance: 1\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "snail_position = 0\n", + "days = 0\n", + "max_distance = 0\n", + "min_distance = 125\n", + "snail_progress_list = []\n", + "while (snail_position < well_height) :\n", + " daily_distance = advance_cm[days]\n", + " total_day_distance = daily_distance - nightly_distance\n", + " #recalculating max_distance in every iteration\n", + " if total_day_distance > max_distance :\n", + " max_distance = total_day_distance\n", + " #recalculating min_distance in every iteration \n", + " if total_day_distance < min_distance :\n", + " min_distance = total_day_distance\n", + " \n", + " snail_position = total_day_distance + snail_position\n", + " snail_progress_list.append(total_day_distance)\n", + " days += 1 \n", + " \n", + "print(\"max_distance: \",max_distance)\n", + "print(\"min_distance: \",min_distance)" + ] }, { "cell_type": "markdown", @@ -125,10 +207,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 1, 13, 57, 24, 25]\n", + "average progress 21.666666666666668\n" + ] + } + ], + "source": [ + "average = sum(snail_progress_list)/len(snail_progress_list)\n", + "print(snail_progress_list)\n", + "print(\"average progress\",average)\n" + ] }, { "cell_type": "markdown", @@ -139,10 +234,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard Deviation of displacement is 19.510680835549195 \n" + ] + } + ], + "source": [ + "import statistics\n", + "\n", + "print(\"Standard Deviation of displacement is % s \"\n", + " % (statistics.stdev(snail_progress_list)))" + ] } ], "metadata": { @@ -161,7 +269,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "nbformat": 4, 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..8dd9cb258 --- /dev/null +++ b/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb @@ -0,0 +1,229 @@ +{ + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. \n", + "You will use these variables to count the number of clashes each sorcerer wins. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Using the lists of spells of both sorcerers, update variables `gandalf_wins` and `saruman_wins` to count the number of times each sorcerer wins a clash. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Who won the battle?\n", + "Print `Gandalf wins`, `Saruman wins` or `Tie` depending on the result. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bonus\n", + "\n", + "In this bonus challenge, you'll need to check the winner of the battle but this time, a sorcerer wins if he succeeds in winning 3 spell clashes in a row.\n", + "\n", + "Also, the spells now have a name and there is a dictionary that associates that name to a power.\n", + "\n", + "```\n", + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "```\n", + "\n", + "#### 1. Create variables `POWER`, `gandalf` and `saruman` as seen above. Create a variable called `spells` to store the number of spells that the sorcerers cast. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Create two variables called `gandalf_power` and `saruman_power` to store the list of spell powers of each sorcerer." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. The battle starts! Using the variables you've created above, code the execution of spell clashes. Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row. \n", + "If a clash ends up in a tie, the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Find the average spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "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..bf0e5700f 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,13 @@ }, { "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]" + ] }, { "cell_type": "markdown", @@ -64,10 +67,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 +84,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for i in range(len(saruman)):\n", + " if (gandalf[i]>saruman[i]):\n", + " gandalf_wins +=1\n", + " elif(gandalf[i] saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "elif gandalf_wins < saruman_wins:\n", + " print(\"Saruman wins\")\n", + "else:\n", + " print(\"Tie\")" + ] }, { "cell_type": "markdown", @@ -128,10 +156,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "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", + "\n", + "spells = 0" + ] }, { "cell_type": "markdown", @@ -142,10 +185,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -156,10 +202,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "gandalf_power : [50, 40, 40, 10, 50, 10, 40, 50, 50, 50]\n", + "saruman_power : [45, 45, 25, 50, 25, 40, 10, 45, 10, 10]\n" + ] + } + ], + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "spells = len(gandalf)\n", + "\n", + "for i in range(spells):\n", + " gandalf_power.append(POWER[gandalf[i]])\n", + " saruman_power.append(POWER[saruman[i]])\n", + "\n", + "print(\"gandalf_power :\",gandalf_power)\n", + "print(\"saruman_power :\",saruman_power)" + ] }, { "cell_type": "markdown", @@ -171,10 +238,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], + "source": [ + "for i in range(len(saruman)):\n", + " if (gandalf[i]>saruman[i]):\n", + " gandalf_wins +=1\n", + " saruman_wins = 0\n", + " elif(gandalf[i]=3 :\n", + " print(\"Gandalf wins\")\n", + "elif saruman_wins>=3:\n", + " print(\"Saruman wins\")\n", + "else:\n", + " print(\"Tie\")" + ] }, { "cell_type": "markdown", @@ -185,10 +277,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "average spell power of Gandalf : 39.0\n", + "average spell power of Saruman : 30.5\n" + ] + } + ], + "source": [ + "print(\"average spell power of Gandalf :\" ,sum(gandalf_power)/spells)\n", + "print(\"average spell power of Saruman :\" ,sum(saruman_power)/spells)\n", + " " + ] }, { "cell_type": "markdown", @@ -199,10 +304,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard Deviation of Gandaf power spells is 15.951314818673865 \n", + "Standard Deviation of saruman power spells is 16.40629960309962 \n" + ] + } + ], + "source": [ + "import statistics\n", + "\n", + "print(\"Standard Deviation of Gandaf power spells is % s \"\n", + " % (statistics.stdev(gandalf_power)))\n", + "\n", + "print(\"Standard Deviation of saruman power spells is % s \"\n", + " % (statistics.stdev(saruman_power)))" + ] } ], "metadata": { @@ -221,7 +343,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "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..2bd6fe7a2 --- /dev/null +++ b/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb @@ -0,0 +1,139 @@ +{ + "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": 2, + "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": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "number_stops = len(stops)\n", + "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": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the maximum occupation of the bus." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8fd..c2f7900f1 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": 2, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "number_stops = len(stops)\n", + "number_stops" + ] }, { "cell_type": "markdown", @@ -67,10 +81,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 13, 11, 10, 14, 10, 7, 5, 4]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "passengers_stops = []\n", + "passengers_stops.append(stops[0][0] - stops[0][1])\n", + "\n", + "for i in range(number_stops):\n", + " if i>0:\n", + " passengers_stops.append ((stops[i][0] - stops[i][1]) + passengers_stops[i-1])\n", + " \n", + "passengers_stops" + ] }, { "cell_type": "markdown", @@ -81,10 +115,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "maximum occupation of the bus: 14\n" + ] + } + ], + "source": [ + "print(\"maximum occupation of the bus: \", max (passengers_stops))" + ] }, { "cell_type": "markdown", @@ -95,10 +139,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average occupation: 9.333333333333334\n", + "Standard Deviation of bus occupation is 3.391164991562634 \n" + ] + } + ], + "source": [ + "import statistics\n", + "\n", + "print (\"Average occupation: \", sum(passengers_stops)/number_stops)\n", + "print(\"Standard Deviation of bus occupation is % s \"\n", + " % (statistics.stdev(passengers_stops)))\n" + ] } ], "metadata": { @@ -117,7 +176,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "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..6ead084e5 --- /dev/null +++ b/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb @@ -0,0 +1,241 @@ +{ + "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": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{(-3, 2), (4, 5), (5, 7), (2, 2)}\n" + ] + } + ], + "source": [ + "dictionary_points = {}\n", + "sets_points = set()\n", + "\n", + "for point in points: \n", + " value = dictionary_points.get(point, \"False\")\n", + " \n", + " if value == \"False\":\n", + " dictionary_points[point] =1\n", + " else:\n", + " dictionary_points[point] =dictionary_points[point]+1\n", + " sets_points.add(point)\n", + " \n", + "\n", + " \n", + "print(sets_points)\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": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1: 10\n", + "Q2: 6\n", + "Q3: 2\n", + "Q4: 2\n" + ] + } + ], + "source": [ + "#the space can be divided into 4 zones (quadrants): Q1, Q2, Q3, Q4. \n", + "#If a point is in Q1, both its x coordinate and y coordinate are positive. \n", + "#Any point with a null x or y coordinate is considered to not belong to any quadrant.\n", + "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(\"Q1: \",Q1)\n", + "print(\"Q2: \",Q2)\n", + "print(\"Q3: \",Q3)\n", + "print(\"Q4: \",Q4)\n", + "\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": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "min distance 2.0\n", + "list_points [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "point0 = (0,0)\n", + "def euc_distance(point1, point2):\n", + " return math.sqrt(((point1[0]-point2[0])**2) + (point1[1]-point2[1])**2)\n", + "\n", + "\n", + "#calculating the min distance\n", + "min_distance = 9999\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance<=min_distance:\n", + " min_distance = distance\n", + "\n", + "#getting all the points with the min distnce to the center \n", + "list_points = []\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance==min_distance:\n", + " list_points.append(point)\n", + " \n", + "print(\"min distance\", min_distance) \n", + "print(\"list_points\", list_points)\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": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of arrows that don't hit the target 2\n" + ] + } + ], + "source": [ + "counter = 0\n", + "radius = 9\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance>radius :\n", + " counter+=1\n", + "print(\"Number of arrows that don't hit the target\", counter) " + ] + } + ], + "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.8.10" + } + }, + "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..6ead084e5 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,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{(-3, 2), (4, 5), (5, 7), (2, 2)}\n" + ] + } + ], + "source": [ + "dictionary_points = {}\n", + "sets_points = set()\n", + "\n", + "for point in points: \n", + " value = dictionary_points.get(point, \"False\")\n", + " \n", + " if value == \"False\":\n", + " dictionary_points[point] =1\n", + " else:\n", + " dictionary_points[point] =dictionary_points[point]+1\n", + " sets_points.add(point)\n", + " \n", + "\n", + " \n", + "print(sets_points)\n" + ] }, { "cell_type": "markdown", @@ -70,10 +94,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1: 10\n", + "Q2: 6\n", + "Q3: 2\n", + "Q4: 2\n" + ] + } + ], + "source": [ + "#the space can be divided into 4 zones (quadrants): Q1, Q2, Q3, Q4. \n", + "#If a point is in Q1, both its x coordinate and y coordinate are positive. \n", + "#Any point with a null x or y coordinate is considered to not belong to any quadrant.\n", + "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(\"Q1: \",Q1)\n", + "print(\"Q2: \",Q2)\n", + "print(\"Q3: \",Q3)\n", + "print(\"Q4: \",Q4)\n", + "\n", + " \n" + ] }, { "cell_type": "markdown", @@ -88,10 +147,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "min distance 2.0\n", + "list_points [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "point0 = (0,0)\n", + "def euc_distance(point1, point2):\n", + " return math.sqrt(((point1[0]-point2[0])**2) + (point1[1]-point2[1])**2)\n", + "\n", + "\n", + "#calculating the min distance\n", + "min_distance = 9999\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance<=min_distance:\n", + " min_distance = distance\n", + "\n", + "#getting all the points with the min distnce to the center \n", + "list_points = []\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance==min_distance:\n", + " list_points.append(point)\n", + " \n", + "print(\"min distance\", min_distance) \n", + "print(\"list_points\", list_points)\n", + " " + ] }, { "cell_type": "markdown", @@ -103,10 +195,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of arrows that don't hit the target 2\n" + ] + } + ], + "source": [ + "counter = 0\n", + "radius = 9\n", + "for point in points:\n", + " distance = euc_distance(point0, point)\n", + " if distance>radius :\n", + " counter+=1\n", + "print(\"Number of arrows that don't hit the target\", counter) " + ] } ], "metadata": { @@ -125,7 +233,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "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..30b1354ca --- /dev/null +++ b/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb @@ -0,0 +1,272 @@ +{ + "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": 4, + "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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "min_temp_day: 0\n" + ] + } + ], + "source": [ + "print(\"min_temp_day: \", min(temperatures_C))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Find the maximum temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Create a list with the temperatures that are greater than or equal to 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Find the average temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Imagine that there was a sensor failure at 3am and the data for that specific hour was not recorded. How would you estimate the missing value? Replace the current value of the list at 3am for an estimation. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Bonus: the maintenance staff is from the United States and does not understand the international metric system. Help them by converting the temperatures from Celsius to Fahrenheit.\n", + "To know more about temperature conversion check this [link](https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature).\n", + "\n", + "**Formula**: \n", + "\n", + "$F = 1.8 * C + 32$" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 7. Make a decision!\n", + "Now it's time to make a decision taking into account what you have seen until now. \n", + "\n", + "Remember that if one of the following events occurs, then the cooling system needs to be replaced for a new one to avoid damaging the processor.\n", + "* More than 4 temperatures are greater than or equal to 70ºC.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "#### To make your decision, check if any of the three conditions above is met. You might need to use some of the variables you created in steps 1 to 6. Print a message to show if the cooling system needs to be changed or not." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bonus\n", + "\n", + "The company has decided that the decision you made is not valid. They want you to analyze the data again but this time, the conditions that need to be met in order to change the cooling system are different.\n", + "\n", + "This time, if one of the following events occurs, then the cooling system needs to be replaced:\n", + "* The temperature is greater than 70ºC during more than 4 consecutive hours.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "Follow the steps so that you can make the decision.\n", + "\n", + "#### 1. Create a list with the hours where the temperature is greater than 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Check if the list you created in step 1 has more than 4 consecutive hours. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Make the decision!\n", + "To make your decision, check if any of the three conditions is met. Print a message to show if the cooling system needs to be changed or not." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Find the average value of the temperature lists (ºC and ºF). What is the relation between both average values?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Find the standard deviation of the temperature lists (ºC and ºF). What is the relation between both standard deviations?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "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..864d8b080 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": 4, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "min_temp_day = min(temperatures_C)\n" + ] }, { "cell_type": "markdown", @@ -67,10 +69,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "max_temp_day = max(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -81,10 +85,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "temp_over_70 = []\n", + "for temp in temperatures_C:\n", + " if temp>70 :\n", + " temp_over_70.append(temp)\n" + ] }, { "cell_type": "markdown", @@ -95,10 +112,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "mean_temp_day = sum(temperatures_C)/len(temperatures_C)\n" + ] }, { "cell_type": "markdown", @@ -109,10 +128,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#We could use different technics, I'm going to use the mean value although (as other alternative) \n", + "#we could use an #avarage between the previous value and the next value\n", + "\n", + "temperatures_C[3] = mean_temp_day\n" + ] }, { "cell_type": "markdown", @@ -128,10 +152,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_F = []\n", + "for temp in temperatures_C:\n", + " temperatures_F.append((temp*1.8)+32)\n", + " " + ] }, { "cell_type": "markdown", @@ -150,10 +179,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "the cooling system needs to be replaced- temperature measures over 70: 7\n", + "the cooling system needs to be replaced- there is at least one temperature measures over 80\n" + ] + } + ], + "source": [ + "if len(temp_over_70)>4:\n", + " print(\"the cooling system needs to be replaced- temperature measures over 70: \", len(temp_over_70))\n", + "if max_temp_day>80:\n", + " print(\"the cooling system needs to be replaced- there is at least one temperature measures over 80\")\n", + "if mean_temp_day>65: \n", + " print(\"the cooling system needs to be replaced- temperature avarege over 65\")" + ] }, { "cell_type": "markdown", @@ -175,10 +220,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[9, 10, 11, 12, 13, 14, 15]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "temp_over_70_hours = []\n", + "for i in range (len(temperatures_C)):\n", + " if temperatures_C[i]>70 :\n", + " if i==0:\n", + " temp_over_70_hours.append(12)\n", + " else:\n", + " temp_over_70_hours.append(i)\n", + "temp_over_70_hours" + ] }, { "cell_type": "markdown", @@ -189,10 +254,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "counter_cons= 1\n", + "for i in range(len(temp_over_70_hours)):\n", + " if i == 0:\n", + " previous_value = temp_over_70_hours[0]\n", + " else:\n", + " if (previous_value + 1 == temp_over_70_hours[i]):\n", + " previous_value = temp_over_70_hours[i]\n", + " counter_cons +=1\n", + " else:\n", + " counter_cons==1\n", + " \n", + "counter_cons" + ] }, { "cell_type": "markdown", @@ -204,10 +293,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The cooling system needs to be replaced- more than 4 consecutives temperature measures over 70\n", + "The cooling system needs to be replaced- there is at least one temperature measures over 80\n" + ] + } + ], + "source": [ + "if counter_cons>4:\n", + " print(\"The cooling system needs to be replaced- more than 4 consecutives temperature measures over 70\")\n", + "if max_temp_day>80:\n", + " print(\"The cooling system needs to be replaced- there is at least one temperature measures over 80\")\n", + "if mean_temp_day>65: \n", + " print(\"The cooling system needs to be replaced- temperature avarege over 65\")" + ] }, { "cell_type": "markdown", @@ -218,10 +323,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean C: 62.760416666666664\n", + "Mean F: 144.96875\n" + ] + } + ], + "source": [ + "#The relation between both avarage values is the formula below :\n", + "#𝐹=1.8∗𝐶+32\n", + "\n", + "print(\"Mean C: \", sum(temperatures_C)/len(temperatures_C))\n", + "print(\"Mean F: \",sum(temperatures_F)/len(temperatures_F))\n", + "\n" + ] }, { "cell_type": "markdown", @@ -232,10 +353,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard Deviation of temperatures_C is 14.956727286515202 \n", + "Standard Deviation of bus temperatures_F is 26.922109115727363 \n" + ] + } + ], + "source": [ + "#The relation between both avarage values is the formula below :\n", + "#𝐹=1.8∗𝐶+32\n", + "\n", + "import statistics\n", + "\n", + "print(\"Standard Deviation of temperatures_C is % s \"\n", + " % (statistics.stdev(temperatures_C)))\n", + "print(\"Standard Deviation of bus temperatures_F is % s \"\n", + " % (statistics.stdev(temperatures_F)))" + ] } ], "metadata": { @@ -254,7 +394,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "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..8627f902b --- /dev/null +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" @@ -0,0 +1,513 @@ +{ + "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": 108, + "metadata": {}, + "outputs": [], + "source": [ + "from random import choice as ch\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": 109, + "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": 110, + "metadata": {}, + "outputs": [], + "source": [ + "n_rounds = 3" + ] + }, + { + "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": 111, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rounds_to_win = int(n_rounds/2) +1\n", + "rounds_to_win" + ] + }, + { + "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": 112, + "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": 113, + "metadata": {}, + "outputs": [], + "source": [ + "def cpu_input():\n", + " return ch(gestures)\n", + "\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": 114, + "metadata": {}, + "outputs": [], + "source": [ + "def user_input ():\n", + " input_gesture = input( \"which is the gesture would you like to use :'rock', 'paper' or 'scissors' \")\n", + " correct = False\n", + " while (correct == False):\n", + " if input_gesture== 'exit':\n", + " break\n", + " if (input_gesture=='rock') | (input_gesture=='paper') | (input_gesture=='scissors'):\n", + " correct = True\n", + " else:\n", + " input_gesture = input( \"which is the gesture would you like to use :'rock', 'paper' or 'scissors' \")\n", + " return input_gesture " + ] + }, + { + "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": 115, + "metadata": {}, + "outputs": [], + "source": [ + "def check_round (cpu_choice, user_choice):\n", + " if cpu_choice == user_choice :\n", + " return 0\n", + " #user wins\n", + " elif ( ((user_choice == \"scissors\") & (cpu_choice == \"paper\")) | ((user_choice == \"paper\") & (cpu_choice == \"rock\")) | ((user_choice == \"rock\") & (cpu_choice == \"scissors\"))):\n", + " return 2\n", + " else:\n", + " #cpu wins\n", + " return 1\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": 116, + "metadata": {}, + "outputs": [], + "source": [ + "def current_round(cpu_choice, user_choice):\n", + " \n", + " global cpu_score\n", + " global player_score\n", + " global n_rounds\n", + "\n", + " \n", + " winner = check_round(cpu_choice, user_choice )\n", + " if winner == 1:\n", + " cpu_score += 1\n", + " print(\"Winner of the round : CPU\")\n", + " elif winner ==2:\n", + " player_score += 1\n", + " print(\"Winner of the round : USER\")\n", + " else:\n", + " print (\"TIE!!!\")\n", + " \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", + "\n", + "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", + "\n", + "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "which is the gesture would you like to use :'rock', 'paper' or 'scissors' rock\n", + "user_choice: rock\n", + "cpu_choice: scissors\n", + "Winner of the round : USER\n", + "which is the gesture would you like to use :'rock', 'paper' or 'scissors' rock\n", + "user_choice: rock\n", + "cpu_choice: paper\n", + "Winner of the round : CPU\n", + "which is the gesture would you like to use :'rock', 'paper' or 'scissors' rock\n", + "user_choice: rock\n", + "cpu_choice: scissors\n", + "Winner of the round : USER\n", + "---------------------\n", + "---GAME WINNEEEER:---\n", + "--------USER---------\n" + ] + } + ], + "source": [ + "playing = True\n", + "cpu_score = 0\n", + "player_score = 0\n", + "\n", + "user_choice = user_input()\n", + "print(\"user_choice: \", user_choice)\n", + "cpu_choice = cpu_input()\n", + "print (\"cpu_choice: \", cpu_choice) \n", + "\n", + "while (playing):\n", + " if ( cpu_score cpu_score:\n", + " print(\"---------------------\")\n", + " print(\"---ROUNDS WINNEEEER:-\")\n", + " print(\"--------USER---------\")\n", + "elif cpu_score > player_score: \n", + " print(\"---------------------\")\n", + " print(\"---ROUNDS WINNEEEER:-\")\n", + " print(\"--------CPU----------\")\n", + "else:\n", + " print(\"---------------------\")\n", + " print(\"---------TIE---------\")\n", + " print(\"---------------------\")\n", + " \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": 159, + "metadata": {}, + "outputs": [], + "source": [ + "from random import choice as ch\n", + "gestures_bonus = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "\n", + "cpu_score_bonus = 0\n", + "player_score_bonus = 0\n", + "\n", + "def cpu_input_bonus():\n", + " return ch(gestures_bonus)\n", + "\n", + "def user_input_rounds ():\n", + " n_rounds_str =input( \"Please, Insert number of rounds (odd number)\")\n", + " correct = False\n", + " while (not correct):\n", + " if (n_rounds_str.isnumeric()):\n", + " n_rounds_int = int(n_rounds_str)\n", + " if (n_rounds_int%2 == 1): \n", + " correct = True\n", + " return n_rounds_int\n", + " else:\n", + " n_rounds_str = input( \"Please, Insert number an odd number\")\n", + " n_rounds_str =input( \"Please, Insert number of rounds (odd number)\") \n", + " \n", + "\n", + "def user_input_bonus ():\n", + " input_gesture_bonus = input( \"which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'\")\n", + " correct = False\n", + " while (not correct):\n", + " if (input_gesture_bonus=='rock') | (input_gesture_bonus=='paper') | (input_gesture_bonus=='scissors')| (input_gesture_bonus=='lizard')| (input_gesture_bonus=='spock'):\n", + " correct = True\n", + " else:\n", + " input_gesture_bonus = input( \"which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'\")\n", + " return input_gesture_bonus \n", + "\n", + "\n", + "\n", + "def check_round_bonus (cpu_choice, user_choice):\n", + " if cpu_choice == user_choice :\n", + " return 0\n", + " #user wins\n", + " elif ( ((user_choice == \"scissors\") & (cpu_choice == \"paper\")) | \n", + " ((user_choice == \"scissors\") & (cpu_choice == \"lizard\")) | \n", + " ((user_choice == \"paper\") & (cpu_choice == \"rock\")) |\n", + " ((user_choice == \"paper\") & (cpu_choice == \"spock\")) | \n", + " ((user_choice == \"rock\") & (cpu_choice == \"scissors\"))| \n", + " ((user_choice == \"rock\") & (cpu_choice == \"lizard\")) |\n", + " ((user_choice == \"lizard\") & (cpu_choice == \"spock\")) |\n", + " ((user_choice == \"lizard\") & (cpu_choice == \"paper\")) |\n", + " ((user_choice == \"spock\") & (cpu_choice == \"scissors\")) |\n", + " ((user_choice == \"spock\") & (cpu_choice == \"rock\"))):\n", + " return 2\n", + " else:\n", + " #cpu wins\n", + " return 1\n", + " \n", + "def current_round_bonus(cpu_choice, user_choice):\n", + " \n", + " global cpu_score_bonus\n", + " global player_score_bonus\n", + " \n", + " winner = check_round_bonus(cpu_choice, user_choice )\n", + " if winner == 1:\n", + " cpu_score_bonus += 1\n", + " print(\"Winner of the round : CPU\")\n", + " elif winner ==2:\n", + " player_score_bonus += 1\n", + " print(\"Winner of the round : USER\")\n", + " else:\n", + " print (\"TIE!!!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please, Insert number of rounds (odd number)adf\n", + "Please, Insert number of rounds (odd number)adf\n", + "Please, Insert number of rounds (odd number)4\n", + "Please, Insert number an odd number4\n", + "Please, Insert number of rounds (odd number)3\n", + "which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'lizard\n", + "user_choice: lizard\n", + "cpu_choice: rock\n", + "Winner of the round : CPU\n", + "which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'lizard\n", + "user_choice: lizard\n", + "cpu_choice: rock\n", + "Winner of the round : CPU\n", + "---------------------\n", + "---GAME WINNEEEER:---\n", + "--------CPU----------\n" + ] + } + ], + "source": [ + "playing_bonus = True\n", + "cpu_score_bonus = 0\n", + "player_score_bonus = 0\n", + "\n", + "n_rounds_bonus = user_input_rounds()\n", + "rounds_to_win_bonus = int(n_rounds_bonus/2) +1\n", + "\n", + "user_choice = user_input_bonus()\n", + "print(\"user_choice: \", user_choice)\n", + "cpu_choice = cpu_input_bonus()\n", + "print (\"cpu_choice: \", cpu_choice) \n", + "\n", + "while (playing_bonus):\n", + " if ( playing_bonus cpu_score:\n", + " print(\"---------------------\")\n", + " print(\"---ROUNDS WINNEEEER:-\")\n", + " print(\"--------USER---------\")\n", + "elif cpu_score > player_score: \n", + " print(\"---------------------\")\n", + " print(\"---ROUNDS WINNEEEER:-\")\n", + " print(\"--------CPU----------\")\n", + "else:\n", + " print(\"---------------------\")\n", + " print(\"---------TIE---------\")\n", + " print(\"---------------------\")\n", + " \n", + " " + ] }, { "cell_type": "markdown", @@ -204,10 +351,142 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 159, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from random import choice as ch\n", + "gestures_bonus = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "\n", + "cpu_score_bonus = 0\n", + "player_score_bonus = 0\n", + "\n", + "def cpu_input_bonus():\n", + " return ch(gestures_bonus)\n", + "\n", + "def user_input_rounds ():\n", + " n_rounds_str =input( \"Please, Insert number of rounds (odd number)\")\n", + " correct = False\n", + " while (not correct):\n", + " if (n_rounds_str.isnumeric()):\n", + " n_rounds_int = int(n_rounds_str)\n", + " if (n_rounds_int%2 == 1): \n", + " correct = True\n", + " return n_rounds_int\n", + " else:\n", + " n_rounds_str = input( \"Please, Insert number an odd number\")\n", + " n_rounds_str =input( \"Please, Insert number of rounds (odd number)\") \n", + " \n", + "\n", + "def user_input_bonus ():\n", + " input_gesture_bonus = input( \"which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'\")\n", + " correct = False\n", + " while (not correct):\n", + " if (input_gesture_bonus=='rock') | (input_gesture_bonus=='paper') | (input_gesture_bonus=='scissors')| (input_gesture_bonus=='lizard')| (input_gesture_bonus=='spock'):\n", + " correct = True\n", + " else:\n", + " input_gesture_bonus = input( \"which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'\")\n", + " return input_gesture_bonus \n", + "\n", + "\n", + "\n", + "def check_round_bonus (cpu_choice, user_choice):\n", + " if cpu_choice == user_choice :\n", + " return 0\n", + " #user wins\n", + " elif ( ((user_choice == \"scissors\") & (cpu_choice == \"paper\")) | \n", + " ((user_choice == \"scissors\") & (cpu_choice == \"lizard\")) | \n", + " ((user_choice == \"paper\") & (cpu_choice == \"rock\")) |\n", + " ((user_choice == \"paper\") & (cpu_choice == \"spock\")) | \n", + " ((user_choice == \"rock\") & (cpu_choice == \"scissors\"))| \n", + " ((user_choice == \"rock\") & (cpu_choice == \"lizard\")) |\n", + " ((user_choice == \"lizard\") & (cpu_choice == \"spock\")) |\n", + " ((user_choice == \"lizard\") & (cpu_choice == \"paper\")) |\n", + " ((user_choice == \"spock\") & (cpu_choice == \"scissors\")) |\n", + " ((user_choice == \"spock\") & (cpu_choice == \"rock\"))):\n", + " return 2\n", + " else:\n", + " #cpu wins\n", + " return 1\n", + " \n", + "def current_round_bonus(cpu_choice, user_choice):\n", + " \n", + " global cpu_score_bonus\n", + " global player_score_bonus\n", + " \n", + " winner = check_round_bonus(cpu_choice, user_choice )\n", + " if winner == 1:\n", + " cpu_score_bonus += 1\n", + " print(\"Winner of the round : CPU\")\n", + " elif winner ==2:\n", + " player_score_bonus += 1\n", + " print(\"Winner of the round : USER\")\n", + " else:\n", + " print (\"TIE!!!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 160, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Please, Insert number of rounds (odd number)adf\n", + "Please, Insert number of rounds (odd number)adf\n", + "Please, Insert number of rounds (odd number)4\n", + "Please, Insert number an odd number4\n", + "Please, Insert number of rounds (odd number)3\n", + "which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'lizard\n", + "user_choice: lizard\n", + "cpu_choice: rock\n", + "Winner of the round : CPU\n", + "which is the gesture would you like to use :'rock', 'paper', 'scissors', 'lizard' or 'spock'lizard\n", + "user_choice: lizard\n", + "cpu_choice: rock\n", + "Winner of the round : CPU\n", + "---------------------\n", + "---GAME WINNEEEER:---\n", + "--------CPU----------\n" + ] + } + ], + "source": [ + "playing_bonus = True\n", + "cpu_score_bonus = 0\n", + "player_score_bonus = 0\n", + "\n", + "n_rounds_bonus = user_input_rounds()\n", + "rounds_to_win_bonus = int(n_rounds_bonus/2) +1\n", + "\n", + "user_choice = user_input_bonus()\n", + "print(\"user_choice: \", user_choice)\n", + "cpu_choice = cpu_input_bonus()\n", + "print (\"cpu_choice: \", cpu_choice) \n", + "\n", + "while (playing_bonus):\n", + " if ( playing_bonus