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..30d068c32 --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb @@ -0,0 +1,278 @@ +{ + "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": 63, + "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": 64, + "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": 61, + "metadata": {}, + "outputs": [], + "source": [ + "while snail_position <= well_height:\n", + " days += 1\n", + " snail_position += daily_distance\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 11 days and cleared the well by 5 cm\n" + ] + } + ], + "source": [ + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")" + ] + }, + { + "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": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 6 days and cleared the well by 25 cm\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "\n", + "while snail_position <= well_height:\n", + " snail_position += advance_cm[days]\n", + " days += 1\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " \n", + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")" + ] + }, + { + "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": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 6 days and cleared the well by 25 cm\n", + "Its maximum displacement during it's escape was: 57 cm, achieved on day 4\n", + "Its minumum displacement during it's escape was: 1 cm, achieved on day 2\n" + ] + } + ], + "source": [ + "displacement = []\n", + "\n", + "while snail_position <= well_height:\n", + " snail_position += advance_cm[days]\n", + " displacement.append(advance_cm[days])\n", + " days += 1\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " displacement[days-1] += nightly_distance\n", + " \n", + " \n", + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")\n", + "print(\"Its maximum displacement during it's escape was: \",max(displacement), \" cm, achieved on day \", displacement.index(max(displacement))+1)\n", + "print(\"Its minumum displacement during it's escape was: \",min(displacement), \" cm, achieved on day \", displacement.index(min(displacement))+1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average progress is: 25.0 cm\n" + ] + } + ], + "source": [ + "# takes in to account on the day of escape it will not slide back down.\n", + "print(\"Average progress is: \", sum(displacement)/len(displacement), \" cm\")" + ] + }, + { + "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": 102, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation of displacement: 21.77154105707724\n", + "Population standard deviation of displacement: 19.87460691435179\n", + "Standard deviation of displacement (using statistics import): 21.77154105707724\n", + "Population standard deviation of displacement (using statistics import): 19.87460691435179\n" + ] + } + ], + "source": [ + "import math\n", + "avg_displacement = sum(displacement)/len(displacement)\n", + "variance_displacement = []\n", + "x = 0\n", + "\n", + "for i in displacement:\n", + " variance_displacement.append(float(i-avg_displacement))\n", + " variance_displacement[x] **= 2\n", + " x+=1 \n", + "\n", + "print(\"Standard deviation of displacement: \", math.sqrt(sum(variance_displacement)/(len(variance_displacement)-1)))\n", + "print(\"Population standard deviation of displacement: \", math.sqrt(sum(variance_displacement)/len(variance_displacement)))\n", + "\n", + "import statistics\n", + "print(\"Standard deviation of displacement (using statistics import): \", statistics.stdev(displacement))\n", + "print(\"Population standard deviation of displacement (using statistics import): \", statistics.pstdev(displacement))\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.8.8" + } + }, + "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..30d068c32 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,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "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": 64, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 0" + ] }, { "cell_type": "markdown", @@ -58,10 +65,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 61, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while snail_position <= well_height:\n", + " days += 1\n", + " snail_position += daily_distance\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " " + ] }, { "cell_type": "markdown", @@ -72,10 +87,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 11 days and cleared the well by 5 cm\n" + ] + } + ], + "source": [ + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")" + ] }, { "cell_type": "markdown", @@ -96,10 +121,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 6 days and cleared the well by 25 cm\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "\n", + "while snail_position <= well_height:\n", + " snail_position += advance_cm[days]\n", + " days += 1\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " \n", + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")" + ] }, { "cell_type": "markdown", @@ -111,10 +155,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snale took 6 days and cleared the well by 25 cm\n", + "Its maximum displacement during it's escape was: 57 cm, achieved on day 4\n", + "Its minumum displacement during it's escape was: 1 cm, achieved on day 2\n" + ] + } + ], + "source": [ + "displacement = []\n", + "\n", + "while snail_position <= well_height:\n", + " snail_position += advance_cm[days]\n", + " displacement.append(advance_cm[days])\n", + " days += 1\n", + " if snail_position > well_height:\n", + " break\n", + " snail_position += nightly_distance\n", + " displacement[days-1] += nightly_distance\n", + " \n", + " \n", + "print(\"The snale took \", days, \" days and cleared the well by \", snail_position-well_height, \" cm\")\n", + "print(\"Its maximum displacement during it's escape was: \",max(displacement), \" cm, achieved on day \", displacement.index(max(displacement))+1)\n", + "print(\"Its minumum displacement during it's escape was: \",min(displacement), \" cm, achieved on day \", displacement.index(min(displacement))+1)" + ] }, { "cell_type": "markdown", @@ -125,10 +195,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average progress is: 25.0 cm\n" + ] + } + ], + "source": [ + "# takes in to account on the day of escape it will not slide back down.\n", + "print(\"Average progress is: \", sum(displacement)/len(displacement), \" cm\")" + ] }, { "cell_type": "markdown", @@ -139,10 +220,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation of displacement: 21.77154105707724\n", + "Population standard deviation of displacement: 19.87460691435179\n", + "Standard deviation of displacement (using statistics import): 21.77154105707724\n", + "Population standard deviation of displacement (using statistics import): 19.87460691435179\n" + ] + } + ], + "source": [ + "import math\n", + "avg_displacement = sum(displacement)/len(displacement)\n", + "variance_displacement = []\n", + "x = 0\n", + "\n", + "for i in displacement:\n", + " variance_displacement.append(float(i-avg_displacement))\n", + " variance_displacement[x] **= 2\n", + " x+=1 \n", + "\n", + "print(\"Standard deviation of displacement: \", math.sqrt(sum(variance_displacement)/(len(variance_displacement)-1)))\n", + "print(\"Population standard deviation of displacement: \", math.sqrt(sum(variance_displacement)/len(variance_displacement)))\n", + "\n", + "import statistics\n", + "print(\"Standard deviation of displacement (using statistics import): \", statistics.stdev(displacement))\n", + "print(\"Population standard deviation of displacement (using statistics import): \", statistics.pstdev(displacement))\n" + ] } ], "metadata": { @@ -161,7 +270,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "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..5be46ed36 --- /dev/null +++ b/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb @@ -0,0 +1,360 @@ +{ + "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": 2, + "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 = 0" + ] + }, + { + "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": 3, + "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": 6, + "metadata": {}, + "outputs": [], + "source": [ + "for g, s in zip(gandalf, saruman):\n", + " if g > s:\n", + " gandalf_wins += 1\n", + " elif s > g:\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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf 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(\"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": 8, + "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']" + ] + }, + { + "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": 9, + "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": 10, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "for spell in gandalf:\n", + " gandalf_power.append(POWER[spell])\n", + " \n", + "for spell in saruman:\n", + " saruman_power.append(POWER[spell])" + ] + }, + { + "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": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Wins\n" + ] + } + ], + "source": [ + "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", + " if gandalf_wins == 3:\n", + " print(\"Gandalf Wins\")\n", + " break;\n", + " elif saruman_wins == 3:\n", + " print(\"Saruman Wins\")\n", + " break;\n", + " \n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Find the average spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Wins\n", + "Gandalf\n", + "Average Spell Power: 37.77777777777778\n", + "Saruman\n", + "Average Spell Power: 32.77777777777778\n" + ] + } + ], + "source": [ + "gandalf_used_power = []\n", + "saruman_used_power = []\n", + "\n", + "for g, s in zip(gandalf_power, saruman_power):\n", + " \n", + " gandalf_used_power.append(g)\n", + " saruman_used_power.append(s)\n", + " \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", + " if gandalf_wins == 3:\n", + " print(\"Gandalf Wins\")\n", + " break;\n", + " elif saruman_wins == 3:\n", + " print(\"Saruman Wins\")\n", + " break;\n", + " \n", + "print(\"Gandalf\")\n", + "print(\"Average Spell Power: \", sum(gandalf_used_power)/len(gandalf_used_power))\n", + "print(\"Saruman\")\n", + "print(\"Average Spell Power: \", sum(saruman_used_power)/len(saruman_used_power))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Population Standard Deviation\n", + "Gandalf: 15.475986974649022\n", + "Saruman: 14.740554623801778\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Population Standard Deviation\")\n", + "print(\"Gandalf: \", statistics.pstdev(gandalf_used_power))\n", + "print(\"Saruman: \", statistics.pstdev(saruman_used_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.8.8" + } + }, + "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..5be46ed36 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": 2, "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 = 0" + ] }, { "cell_type": "markdown", @@ -64,10 +68,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -78,10 +85,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for g, s in zip(gandalf, saruman):\n", + " if g > s:\n", + " gandalf_wins += 1\n", + " elif s > g:\n", + " saruman_wins += 1\n", + " " + ] }, { "cell_type": "markdown", @@ -93,10 +107,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf 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(\"Tie\")" + ] }, { "cell_type": "markdown", @@ -128,10 +157,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "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']" + ] }, { "cell_type": "markdown", @@ -142,10 +184,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 +201,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "for spell in gandalf:\n", + " gandalf_power.append(POWER[spell])\n", + " \n", + "for spell in saruman:\n", + " saruman_power.append(POWER[spell])" + ] }, { "cell_type": "markdown", @@ -171,10 +225,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Wins\n" + ] + } + ], + "source": [ + "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", + " if gandalf_wins == 3:\n", + " print(\"Gandalf Wins\")\n", + " break;\n", + " elif saruman_wins == 3:\n", + " print(\"Saruman Wins\")\n", + " break;\n", + " \n", + " " + ] }, { "cell_type": "markdown", @@ -185,10 +263,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Wins\n", + "Gandalf\n", + "Average Spell Power: 37.77777777777778\n", + "Saruman\n", + "Average Spell Power: 32.77777777777778\n" + ] + } + ], + "source": [ + "gandalf_used_power = []\n", + "saruman_used_power = []\n", + "\n", + "for g, s in zip(gandalf_power, saruman_power):\n", + " \n", + " gandalf_used_power.append(g)\n", + " saruman_used_power.append(s)\n", + " \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", + " if gandalf_wins == 3:\n", + " print(\"Gandalf Wins\")\n", + " break;\n", + " elif saruman_wins == 3:\n", + " print(\"Saruman Wins\")\n", + " break;\n", + " \n", + "print(\"Gandalf\")\n", + "print(\"Average Spell Power: \", sum(gandalf_used_power)/len(gandalf_used_power))\n", + "print(\"Saruman\")\n", + "print(\"Average Spell Power: \", sum(saruman_used_power)/len(saruman_used_power))" + ] }, { "cell_type": "markdown", @@ -199,10 +315,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Population Standard Deviation\n", + "Gandalf: 15.475986974649022\n", + "Saruman: 14.740554623801778\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Population Standard Deviation\")\n", + "print(\"Gandalf: \", statistics.pstdev(gandalf_used_power))\n", + "print(\"Saruman: \", statistics.pstdev(saruman_used_power))" + ] } ], "metadata": { @@ -221,7 +352,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "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..e02b09649 --- /dev/null +++ b/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb @@ -0,0 +1,166 @@ +{ + "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": 3, + "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": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "print(len(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": [], + "source": [ + "passengers = []\n", + "running_total = 0\n", + "\n", + "for stop in stops:\n", + " passengers.append(running_total + stop[0] - stop[1])\n", + " running_total += stop[0] - stop[1]" + ] + }, + { + "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": [ + "Maximum occupation: 14\n" + ] + } + ], + "source": [ + "print(\"Maximum occupation: \", max(passengers))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average passengers: 9.333333333333334\n", + "Population Standard Deviation: 3.197221015541813\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Average passengers: \",statistics.mean(passengers))\n", + "#opting to show population as it is presented as if we are looking at entire dataset\n", + "print(\"Population Standard Deviation: \",statistics.pstdev(passengers))" + ] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8fd..e02b09649 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": 3, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "print(len(stops))" + ] }, { "cell_type": "markdown", @@ -67,10 +77,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "passengers = []\n", + "running_total = 0\n", + "\n", + "for stop in stops:\n", + " passengers.append(running_total + stop[0] - stop[1])\n", + " running_total += stop[0] - stop[1]" + ] }, { "cell_type": "markdown", @@ -81,10 +98,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Maximum occupation: 14\n" + ] + } + ], + "source": [ + "print(\"Maximum occupation: \", max(passengers))" + ] }, { "cell_type": "markdown", @@ -95,10 +122,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average passengers: 9.333333333333334\n", + "Population Standard Deviation: 3.197221015541813\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Average passengers: \",statistics.mean(passengers))\n", + "#opting to show population as it is presented as if we are looking at entire dataset\n", + "print(\"Population Standard Deviation: \",statistics.pstdev(passengers))" + ] } ], "metadata": { @@ -117,7 +158,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "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..91d9ab0b5 --- /dev/null +++ b/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb @@ -0,0 +1,237 @@ +{ + "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": 1, + "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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(5, 7), (4, 5), (2, 2), (-3, 2)]\n" + ] + } + ], + "source": [ + "unique_points = set(points)\n", + "dup=[]\n", + "for point in unique_points:\n", + " if(points.count(point)>1):\n", + " dup.append(point)\n", + "print(dup)" + ] + }, + { + "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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1 (top-right): 10\n", + "Q2 (bottom-right): 2\n", + "Q3 (bottom-left): 2\n", + "Q4 (top-left): 6\n", + "Between Quadrants: 2\n" + ] + } + ], + "source": [ + "Q1 = 0\n", + "Q2 = 0\n", + "Q3 = 0\n", + "Q4 = 0\n", + "BQ = 0\n", + "\n", + "for i in points:\n", + " if i[0] > 0:\n", + " if i[1] > 0:\n", + " Q1 += 1\n", + " elif i[1] < 0:\n", + " Q2 += 1\n", + " else:\n", + " BQ += 1\n", + " elif i[0] < 0:\n", + " if i[1] < 0:\n", + " Q3 += 1\n", + " elif i[1] > 0:\n", + " Q4 += 1\n", + " else:\n", + " BQ += 1\n", + " else:\n", + " BQ += 1\n", + "\n", + "print(\"Q1 (top-right):\", Q1)\n", + "print(\"Q2 (bottom-right):\", Q2)\n", + "print(\"Q3 (bottom-left):\", Q3)\n", + "print(\"Q4 (top-left):\", Q4)\n", + "print(\"Between Quadrants:\", BQ)\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": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Min distance from center: 2.0\n", + "Shot Numbers: [1, 11]\n", + "Locations: [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "distance_from_center = []\n", + "\n", + "for i in points:\n", + " distance_from_center.append(math.sqrt(i[0]**2+i[1]**2))\n", + "\n", + "min_distance = min(distance_from_center)\n", + "min_distance_shots_index = []\n", + "min_distance_shots = []\n", + " \n", + "for i in range(0,len(distance_from_center)):\n", + " if distance_from_center[i] == min_distance:\n", + " min_distance_shots_index.append(i)\n", + " min_distance_shots.append(points[i])\n", + " \n", + "\n", + "print(\"Min distance from center: \", min(distance_from_center))\n", + "print(\"Shot Numbers: \", min_distance_shots_index)\n", + "print(\"Locations: \", min_distance_shots)" + ] + }, + { + "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": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Missed target 2 times.\n" + ] + } + ], + "source": [ + "misses = 0\n", + "\n", + "for i in distance_from_center:\n", + " if i > 9:\n", + " misses += 1\n", + " \n", + "print(\"Missed target \", misses, \" times.\" )" + ] + } + ], + "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.8" + } + }, + "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..91d9ab0b5 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -55,10 +55,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(5, 7), (4, 5), (2, 2), (-3, 2)]\n" + ] + } + ], + "source": [ + "unique_points = set(points)\n", + "dup=[]\n", + "for point in unique_points:\n", + " if(points.count(point)>1):\n", + " dup.append(point)\n", + "print(dup)" + ] }, { "cell_type": "markdown", @@ -70,10 +85,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1 (top-right): 10\n", + "Q2 (bottom-right): 2\n", + "Q3 (bottom-left): 2\n", + "Q4 (top-left): 6\n", + "Between Quadrants: 2\n" + ] + } + ], + "source": [ + "Q1 = 0\n", + "Q2 = 0\n", + "Q3 = 0\n", + "Q4 = 0\n", + "BQ = 0\n", + "\n", + "for i in points:\n", + " if i[0] > 0:\n", + " if i[1] > 0:\n", + " Q1 += 1\n", + " elif i[1] < 0:\n", + " Q2 += 1\n", + " else:\n", + " BQ += 1\n", + " elif i[0] < 0:\n", + " if i[1] < 0:\n", + " Q3 += 1\n", + " elif i[1] > 0:\n", + " Q4 += 1\n", + " else:\n", + " BQ += 1\n", + " else:\n", + " BQ += 1\n", + "\n", + "print(\"Q1 (top-right):\", Q1)\n", + "print(\"Q2 (bottom-right):\", Q2)\n", + "print(\"Q3 (bottom-left):\", Q3)\n", + "print(\"Q4 (top-left):\", Q4)\n", + "print(\"Between Quadrants:\", BQ)\n", + " " + ] }, { "cell_type": "markdown", @@ -88,10 +146,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Min distance from center: 2.0\n", + "Shot Numbers: [1, 11]\n", + "Locations: [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "distance_from_center = []\n", + "\n", + "for i in points:\n", + " distance_from_center.append(math.sqrt(i[0]**2+i[1]**2))\n", + "\n", + "min_distance = min(distance_from_center)\n", + "min_distance_shots_index = []\n", + "min_distance_shots = []\n", + " \n", + "for i in range(0,len(distance_from_center)):\n", + " if distance_from_center[i] == min_distance:\n", + " min_distance_shots_index.append(i)\n", + " min_distance_shots.append(points[i])\n", + " \n", + "\n", + "print(\"Min distance from center: \", min(distance_from_center))\n", + "print(\"Shot Numbers: \", min_distance_shots_index)\n", + "print(\"Locations: \", min_distance_shots)" + ] }, { "cell_type": "markdown", @@ -103,10 +191,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Missed target 2 times.\n" + ] + } + ], + "source": [ + "misses = 0\n", + "\n", + "for i in distance_from_center:\n", + " if i > 9:\n", + " misses += 1\n", + " \n", + "print(\"Missed target \", misses, \" times.\" )" + ] } ], "metadata": { @@ -125,7 +229,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "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..151907c17 --- /dev/null +++ b/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb @@ -0,0 +1,401 @@ +{ + "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": [], + "source": [ + "min_temp = 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": 4, + "metadata": {}, + "outputs": [], + "source": [ + "max_temp = max(temperatures_C)" + ] + }, + { + "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": 8, + "metadata": {}, + "outputs": [], + "source": [ + "temperatures_over_70 = [i for i in temperatures_C if i >= 70]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Find the average temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "avg_temp = sum(temperatures_C)/len(temperatures_C)" + ] + }, + { + "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": 13, + "metadata": {}, + "outputs": [], + "source": [ + "temperatures_C[3] = int((temperatures_C[2]+temperatures_C[4])/2)" + ] + }, + { + "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": 16, + "metadata": {}, + "outputs": [], + "source": [ + "temperatures_F = [1.8*i+32 for i in temperatures_C]" + ] + }, + { + "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": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature recorded was over 70ºC on 8 occasions, higher than the 4 times threshold.\n", + "The maximum temperature recorded in the day was 90 , + 10 degrees over our threshold.\n", + "The average temperature recorded in the day was 62.833333333333336 which remains under our threshold.\n", + "As one or more criteria have been reached we should change the cooling system\n" + ] + } + ], + "source": [ + "change = False\n", + "\n", + "if len(temperatures_over_70) > 4:\n", + " print(\"The temperature recorded was over 70ºC on \", len(temperatures_over_70), \" occasions, higher than the 4 times threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The temperature recorded was over 70ºC on \", len(temperatures_over_70), \" occasions, within threshold.\")\n", + "if max_temp > 80:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", +\", max_temp - 80, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", and so remains without the threshold.\")\n", + "if avg_temp > 65:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \", \", avg_temp - 65, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \" which remains under our threshold.\")\n", + " \n", + "if change: \n", + " print(\"As one or more criteria have been reached we should change the cooling system\")\n", + "else:\n", + " print(\"All checks remain within threshold, no need to replace yet\")" + ] + }, + { + "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": 28, + "metadata": {}, + "outputs": [], + "source": [ + "# Well....the max temp is still 90 so...change it!\n", + "hours_over_70 = [i for i in range(0,len(temperatures_C)) if temperatures_C[i] > 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": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "consecutive_hours = 0\n", + "max_consecutive_hours = 0\n", + "\n", + "for i in range(len(hours_over_70)):\n", + " if hours_over_70[i] == 0:\n", + " consecutive_hours = 1\n", + " max_consecutive_hours = 1\n", + " elif hours_over_70[i] == hours_over_70[i-1]+1:\n", + " consecutive_hours += 1\n", + " if consecutive_hours > max_consecutive_hours:\n", + " max_consecutive_hours += 1\n", + " else: \n", + " consecutive_hours == 1\n", + " \n", + "print(max_consecutive_hours)" + ] + }, + { + "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": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature recorded was over 70ºC for 6 consective hours, higher than the threshold.\n", + "The maximum temperature recorded in the day was 90 , + 10 degrees over our threshold.\n", + "The average temperature recorded in the day was 62.833333333333336 which remains under our threshold.\n", + "As one or more criteria have been reached we should change the cooling system\n" + ] + } + ], + "source": [ + "change = False\n", + "\n", + "if max_consecutive_hours > 4:\n", + " print(\"The temperature recorded was over 70ºC for \", max_consecutive_hours, \" consective hours, higher than the threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The temperature recorded was over 70ºC on \", max_consecutive_hours, \" occasions, within threshold.\")\n", + "if max_temp > 80:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", +\", max_temp - 80, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", and so remains without the threshold.\")\n", + "if avg_temp > 65:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \", \", avg_temp - 65, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \" which remains under our threshold.\")\n", + " \n", + "if change: \n", + " print(\"As one or more criteria have been reached we should change the cooling system\")\n", + "else:\n", + " print(\"All checks remain within threshold, no need to replace yet\")" + ] + }, + { + "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": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average temperature: 62.833333333333336 °C\n", + "Average temperature: 145.1 °F\n", + "The relationship with the average remains consistant with the conversion forumula.\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Average temperature: \",statistics.mean(temperatures_C), \"°C\")\n", + "print(\"Average temperature: \",statistics.mean(temperatures_F), \"°F\")\n", + "print(\"The relationship with the average remains consistant with the conversion forumula.\")" + ] + }, + { + "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": 59, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation: 14.633485192833897 °C\n", + "Standard deviation: 26.340273347101014 °F\n", + "--------\n", + "The standard deviation in °F is +80% higher than °C. As the conversion to °F includes °C*1.8 effectively increaseing the scale and as such any variance by +80% and as such any variance from the mean this is to be expected. The +32 is a constant shift and doesn't impact the difference in the standard deviation.\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Standard deviation: \",statistics.pstdev(temperatures_C), \"°C\")\n", + "print(\"Standard deviation: \",statistics.pstdev(temperatures_F), \"°F\")\n", + "print(\"--------\")\n", + "print(\"The standard deviation in °F is +80% higher than °C. As the conversion to °F includes °C*1.8 effectively increaseing the scale and as such any variance by +80% and as such any variance from the mean this is to be expected. The +32 is a constant shift and doesn't impact the difference in the standard deviation.\")" + ] + } + ], + "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.8" + } + }, + "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..151907c17 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,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "min_temp = min(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -67,10 +69,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "max_temp = max(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -81,10 +85,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_over_70 = [i for i in temperatures_C if i >= 70]" + ] }, { "cell_type": "markdown", @@ -95,10 +101,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "avg_temp = sum(temperatures_C)/len(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -109,10 +117,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_C[3] = int((temperatures_C[2]+temperatures_C[4])/2)" + ] }, { "cell_type": "markdown", @@ -128,10 +138,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_F = [1.8*i+32 for i in temperatures_C]" + ] }, { "cell_type": "markdown", @@ -150,10 +162,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature recorded was over 70ºC on 8 occasions, higher than the 4 times threshold.\n", + "The maximum temperature recorded in the day was 90 , + 10 degrees over our threshold.\n", + "The average temperature recorded in the day was 62.833333333333336 which remains under our threshold.\n", + "As one or more criteria have been reached we should change the cooling system\n" + ] + } + ], + "source": [ + "change = False\n", + "\n", + "if len(temperatures_over_70) > 4:\n", + " print(\"The temperature recorded was over 70ºC on \", len(temperatures_over_70), \" occasions, higher than the 4 times threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The temperature recorded was over 70ºC on \", len(temperatures_over_70), \" occasions, within threshold.\")\n", + "if max_temp > 80:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", +\", max_temp - 80, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", and so remains without the threshold.\")\n", + "if avg_temp > 65:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \", \", avg_temp - 65, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \" which remains under our threshold.\")\n", + " \n", + "if change: \n", + " print(\"As one or more criteria have been reached we should change the cooling system\")\n", + "else:\n", + " print(\"All checks remain within threshold, no need to replace yet\")" + ] }, { "cell_type": "markdown", @@ -175,10 +221,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# Well....the max temp is still 90 so...change it!\n", + "hours_over_70 = [i for i in range(0,len(temperatures_C)) if temperatures_C[i] > 70]" + ] }, { "cell_type": "markdown", @@ -189,10 +238,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "consecutive_hours = 0\n", + "max_consecutive_hours = 0\n", + "\n", + "for i in range(len(hours_over_70)):\n", + " if hours_over_70[i] == 0:\n", + " consecutive_hours = 1\n", + " max_consecutive_hours = 1\n", + " elif hours_over_70[i] == hours_over_70[i-1]+1:\n", + " consecutive_hours += 1\n", + " if consecutive_hours > max_consecutive_hours:\n", + " max_consecutive_hours += 1\n", + " else: \n", + " consecutive_hours == 1\n", + " \n", + "print(max_consecutive_hours)" + ] }, { "cell_type": "markdown", @@ -204,10 +277,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature recorded was over 70ºC for 6 consective hours, higher than the threshold.\n", + "The maximum temperature recorded in the day was 90 , + 10 degrees over our threshold.\n", + "The average temperature recorded in the day was 62.833333333333336 which remains under our threshold.\n", + "As one or more criteria have been reached we should change the cooling system\n" + ] + } + ], + "source": [ + "change = False\n", + "\n", + "if max_consecutive_hours > 4:\n", + " print(\"The temperature recorded was over 70ºC for \", max_consecutive_hours, \" consective hours, higher than the threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The temperature recorded was over 70ºC on \", max_consecutive_hours, \" occasions, within threshold.\")\n", + "if max_temp > 80:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", +\", max_temp - 80, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The maximum temperature recorded in the day was \", max_temp, \", and so remains without the threshold.\")\n", + "if avg_temp > 65:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \", \", avg_temp - 65, \" degrees over our threshold.\")\n", + " change = True\n", + "else:\n", + " print(\"The average temperature recorded in the day was \", avg_temp, \" which remains under our threshold.\")\n", + " \n", + "if change: \n", + " print(\"As one or more criteria have been reached we should change the cooling system\")\n", + "else:\n", + " print(\"All checks remain within threshold, no need to replace yet\")" + ] }, { "cell_type": "markdown", @@ -218,10 +325,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average temperature: 62.833333333333336 °C\n", + "Average temperature: 145.1 °F\n", + "The relationship with the average remains consistant with the conversion forumula.\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Average temperature: \",statistics.mean(temperatures_C), \"°C\")\n", + "print(\"Average temperature: \",statistics.mean(temperatures_F), \"°F\")\n", + "print(\"The relationship with the average remains consistant with the conversion forumula.\")" + ] }, { "cell_type": "markdown", @@ -232,10 +354,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation: 14.633485192833897 °C\n", + "Standard deviation: 26.340273347101014 °F\n", + "--------\n", + "The standard deviation in °F is +80% higher than °C. As the conversion to °F includes °C*1.8 effectively increaseing the scale and as such any variance by +80% and as such any variance from the mean this is to be expected. The +32 is a constant shift and doesn't impact the difference in the standard deviation.\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Standard deviation: \",statistics.pstdev(temperatures_C), \"°C\")\n", + "print(\"Standard deviation: \",statistics.pstdev(temperatures_F), \"°F\")\n", + "print(\"--------\")\n", + "print(\"The standard deviation in °F is +80% higher than °C. As the conversion to °F includes °C*1.8 effectively increaseing the scale and as such any variance by +80% and as such any variance from the mean this is to be expected. The +32 is a constant shift and doesn't impact the difference in the standard deviation.\")" + ] } ], "metadata": { @@ -254,7 +393,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "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..018924d4c --- /dev/null +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" @@ -0,0 +1,519 @@ +{ + "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": 71, + "metadata": {}, + "outputs": [], + "source": [ + "from random import choice" + ] + }, + { + "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": 72, + "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": 73, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many rounds would you like to play (must be an odd number)5\n" + ] + } + ], + "source": [ + "while True:\n", + " try: \n", + " n_rounds = int(input(\"How many rounds would you like to play (must be an odd number)\"))\n", + " if n_rounds % 2 == 0:\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again - enter odd number in numerals\")\n", + " except Exception:\n", + " print(\"Needs to be an odd number: 1,3,5.. etc\")" + ] + }, + { + "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": 74, + "metadata": {}, + "outputs": [], + "source": [ + "rounds_to_win = int(n_rounds / 2)+1" + ] + }, + { + "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": 101, + "metadata": {}, + "outputs": [], + "source": [ + "global cpu_score\n", + "cpu_score = 0\n", + "global player_score\n", + "player_score = 0\n", + "global ties\n", + "ties = 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": 98, + "metadata": {}, + "outputs": [], + "source": [ + "def computer_choose():\n", + " return choice(['rock','paper','scissors'])" + ] + }, + { + "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": 85, + "metadata": {}, + "outputs": [], + "source": [ + "def player_choose():\n", + " while True:\n", + " try: \n", + " player_choice = str(input(\"Pick Rock(r), Paper(p) or Scissors(s):\"))\n", + " player_choice = player_choice.lower()\n", + " if player_choice == 'r': \n", + " player_choice = \"rock\"\n", + " elif player_choice == 'p': \n", + " player_choice = 'paper'\n", + " elif player_choice == 's': \n", + " player_choice = 'scissors'\n", + " if player_choice not in gestures:\n", + " print(player_choice, \" is not in \", gestures)\n", + " raise Exception()\n", + " break\n", + " except Exception:\n", + " print(\"You chose:\",player_choice, \". Please enter one of the options - you can use the single letter keys too \")\n", + " return player_choice" + ] + }, + { + "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": 103, + "metadata": {}, + "outputs": [], + "source": [ + "def who_won(computer_go, player_go):\n", + " if computer_go == player_go:\n", + " return 0\n", + " elif (computer_go == 'rock' and player_go == 'scissors') or (computer_go == 'scissors' and player_go == 'paper') or (computer_go == 'paper' and player_go == 'rock'):\n", + " return 1\n", + " else:\n", + " return 2\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": 102, + "metadata": {}, + "outputs": [], + "source": [ + "def game_round():\n", + " global player_score\n", + " global cpu_score\n", + " global ties\n", + " player_pick = player_choose()\n", + " computer_pick = computer_choose()\n", + " winner = who_won(computer_pick,player_pick)\n", + " \n", + " if winner == 0:\n", + " print(\"It is a tie! You both selected \", player_pick)\n", + " ties += 1\n", + " elif winner == 1: \n", + " print(computer_pick, \" beats\", player_pick, \" - computer takes the round!\")\n", + " cpu_score += 1\n", + " elif winner == 2:\n", + " print(player_pick, \" beats\", computer_pick, \" - you win this round!\")\n", + " player_score += 1\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": 104, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "scissors beats paper - you win this round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "scissors beats paper - you win this round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "rock beats scissors - computer takes the round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):p\n", + "paper beats rock - you win this round!\n" + ] + } + ], + "source": [ + "rounds = 0\n", + "\n", + "\n", + "while rounds <= n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " game_round()\n", + " rounds+=1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 11. Print the winner of the game based on who won more rounds.\n", + "Remember that the game might be tied. " + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player_wins: 3\n", + "Computer_wins: 1\n", + "Ties: 0\n", + "You win the game!\n" + ] + } + ], + "source": [ + "print(\"Player_wins:\", player_score)\n", + "print(\"Computer_wins:\", cpu_score)\n", + "print(\"Ties:\", ties)\n", + "\n", + "if player_score > cpu_score:\n", + " print(\"You win the game!\")\n", + "elif player_score < cpu_score:\n", + " print(\"Computer won this game!\")\n", + "else:\n", + " print(\"The game is a Tie!\")" + ] + }, + { + "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": 117, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many rounds would you like to play (must be an odd number)3\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):v\n", + "<< spock >> beats << rock >> - you win this round!\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):p\n", + "<< scissors >> beats << paper >> - computer takes the round!\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):l\n", + "<< lizard >> beats << spock >> - you win this round!\n", + "Player_wins: 2\n", + "Computer_wins: 1\n", + "Ties: 0\n", + "You win the game!\n", + "Play again? y/nn\n", + "Farewell!\n" + ] + } + ], + "source": [ + "from random import choice\n", + "from time import sleep\n", + "\n", + "gestures = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "global n_rounds\n", + "global cpu_score\n", + "global player_score\n", + "global ties\n", + "\n", + "\n", + "def select_rounds():\n", + " global n_rounds\n", + " while True:\n", + " try: \n", + " n_rounds = int(input(\"How many rounds would you like to play (must be an odd number): \"))\n", + " if n_rounds % 2 == 0:\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again - enter odd number in numerals\")\n", + " except Exception:\n", + " print(\"Needs to be an odd number: 1,3,5.. etc\")\n", + " \n", + "\n", + "def computer_choose():\n", + " return choice(['rock','paper','scissors','lizard','spock'])\n", + "\n", + "\n", + "def player_choose():\n", + " while True:\n", + " try: \n", + " player_choice = str(input(\"Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):\"))\n", + " player_choice = player_choice.lower()\n", + " if player_choice == 'r': \n", + " player_choice = \"rock\"\n", + " elif player_choice == 'p': \n", + " player_choice = 'paper'\n", + " elif player_choice == 's': \n", + " player_choice = 'scissors'\n", + " elif player_choice == 'l': \n", + " player_choice = 'lizard'\n", + " elif player_choice == 'v': \n", + " player_choice = 'spock'\n", + " if player_choice not in gestures:\n", + " print(player_choice, \" is not in \", gestures)\n", + " raise Exception()\n", + " break\n", + " except Exception:\n", + " print(\"You chose:\",player_choice, \". Please enter one of the options - you can use the single letter keys too \")\n", + " return player_choice\n", + "\n", + "def who_won(computer_go, player_go):\n", + " if computer_go == player_go:\n", + " return 0\n", + " elif (computer_go == 'rock' and (player_go == 'scissors' or player_go == 'lizard')):\n", + " return 1\n", + " elif (computer_go == 'scissors' and (player_go == 'paper' or player_go == 'lizard' )):\n", + " return 1\n", + " elif (computer_go == 'paper' and (player_go == 'rock' or player_go == 'spock')):\n", + " return 1\n", + " elif (computer_go == 'lizard' and (player_go == 'spock' or player_go == 'paper')):\n", + " return 1\n", + " elif (computer_go == 'spock' and (player_go == 'rock' or player_go == 'scissors')):\n", + " return 1\n", + " else:\n", + " return 2\n", + "\n", + "def game_round():\n", + " global player_score\n", + " global cpu_score\n", + " global ties\n", + " player_pick = player_choose()\n", + " computer_pick = computer_choose()\n", + " winner = who_won(computer_pick,player_pick)\n", + " \n", + " if winner == 0:\n", + " print(\"It is a tie! You both selected <<\", player_pick, \">>\")\n", + " ties += 1\n", + " elif winner == 1: \n", + " print(\"<<\",computer_pick, \">> beats <<\", player_pick, \">> - computer takes the round!\")\n", + " cpu_score += 1\n", + " elif winner == 2:\n", + " print(\"<<\",player_pick, \">> beats <<\", computer_pick, \">> - you win this round!\")\n", + " player_score += 1\n", + " \n", + " sleep(2)\n", + "\n", + "continue_play = True\n", + "\n", + "while continue_play:\n", + "\n", + " select_rounds()\n", + " rounds_to_win = int(n_rounds / 2)+1\n", + " rounds = 0\n", + " ties = 0\n", + " cpu_score = 0\n", + " player_score = 0\n", + "\n", + " while rounds <= n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " game_round()\n", + " rounds+=1\n", + " \n", + " print(\"Player_wins:\", player_score)\n", + " print(\"Computer_wins:\", cpu_score)\n", + " print(\"Ties:\", ties)\n", + "\n", + " if player_score > cpu_score:\n", + " print(\"You win the game!\")\n", + " elif player_score < cpu_score:\n", + " print(\"Computer won this game!\")\n", + " else:\n", + " print(\"The game is a Tie!\")\n", + " \n", + " while True:\n", + " try: \n", + " play = input(\"Play again? (y/n): \")\n", + " play = play.lower()\n", + " if play != 'y' and play != 'n' :\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again = 'y' or 'n'\")\n", + " except Exception:\n", + " print(\"Try again = 'y' or 'n'\")\n", + " \n", + " if play == 'n':\n", + " continue_play = False\n", + " print(\"Farewell!\")\n", + " \n", + " " + ] + }, + { + "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.8" + } + }, + "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..018924d4c 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,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 71, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from random import choice" + ] }, { "cell_type": "markdown", @@ -46,10 +48,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 72, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gestures = [\"rock\", \"paper\", \"scissors\"]" + ] }, { "cell_type": "markdown", @@ -61,10 +65,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many rounds would you like to play (must be an odd number)5\n" + ] + } + ], + "source": [ + "while True:\n", + " try: \n", + " n_rounds = int(input(\"How many rounds would you like to play (must be an odd number)\"))\n", + " if n_rounds % 2 == 0:\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again - enter odd number in numerals\")\n", + " except Exception:\n", + " print(\"Needs to be an odd number: 1,3,5.. etc\")" + ] }, { "cell_type": "markdown", @@ -76,10 +99,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 74, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "rounds_to_win = int(n_rounds / 2)+1" + ] }, { "cell_type": "markdown", @@ -90,10 +115,17 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 101, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "global cpu_score\n", + "cpu_score = 0\n", + "global player_score\n", + "player_score = 0\n", + "global ties\n", + "ties = 0" + ] }, { "cell_type": "markdown", @@ -105,10 +137,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def computer_choose():\n", + " return choice(['rock','paper','scissors'])" + ] }, { "cell_type": "markdown", @@ -120,10 +155,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 85, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def player_choose():\n", + " while True:\n", + " try: \n", + " player_choice = str(input(\"Pick Rock(r), Paper(p) or Scissors(s):\"))\n", + " player_choice = player_choice.lower()\n", + " if player_choice == 'r': \n", + " player_choice = \"rock\"\n", + " elif player_choice == 'p': \n", + " player_choice = 'paper'\n", + " elif player_choice == 's': \n", + " player_choice = 'scissors'\n", + " if player_choice not in gestures:\n", + " print(player_choice, \" is not in \", gestures)\n", + " raise Exception()\n", + " break\n", + " except Exception:\n", + " print(\"You chose:\",player_choice, \". Please enter one of the options - you can use the single letter keys too \")\n", + " return player_choice" + ] }, { "cell_type": "markdown", @@ -135,10 +189,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def who_won(computer_go, player_go):\n", + " if computer_go == player_go:\n", + " return 0\n", + " elif (computer_go == 'rock' and player_go == 'scissors') or (computer_go == 'scissors' and player_go == 'paper') or (computer_go == 'paper' and player_go == 'rock'):\n", + " return 1\n", + " else:\n", + " return 2\n", + " " + ] }, { "cell_type": "markdown", @@ -150,10 +213,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 102, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def game_round():\n", + " global player_score\n", + " global cpu_score\n", + " global ties\n", + " player_pick = player_choose()\n", + " computer_pick = computer_choose()\n", + " winner = who_won(computer_pick,player_pick)\n", + " \n", + " if winner == 0:\n", + " print(\"It is a tie! You both selected \", player_pick)\n", + " ties += 1\n", + " elif winner == 1: \n", + " print(computer_pick, \" beats\", player_pick, \" - computer takes the round!\")\n", + " cpu_score += 1\n", + " elif winner == 2:\n", + " print(player_pick, \" beats\", computer_pick, \" - you win this round!\")\n", + " player_score += 1\n", + "\n", + " " + ] }, { "cell_type": "markdown", @@ -168,10 +251,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "scissors beats paper - you win this round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "scissors beats paper - you win this round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):s\n", + "rock beats scissors - computer takes the round!\n", + "Pick Rock(r), Paper(p) or Scissors(s):p\n", + "paper beats rock - you win this round!\n" + ] + } + ], + "source": [ + "rounds = 0\n", + "\n", + "\n", + "while rounds <= n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " game_round()\n", + " rounds+=1" + ] }, { "cell_type": "markdown", @@ -183,10 +288,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 105, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Player_wins: 3\n", + "Computer_wins: 1\n", + "Ties: 0\n", + "You win the game!\n" + ] + } + ], + "source": [ + "print(\"Player_wins:\", player_score)\n", + "print(\"Computer_wins:\", cpu_score)\n", + "print(\"Ties:\", ties)\n", + "\n", + "if player_score > cpu_score:\n", + " print(\"You win the game!\")\n", + "elif player_score < cpu_score:\n", + " print(\"Computer won this game!\")\n", + "else:\n", + " print(\"The game is a Tie!\")" + ] }, { "cell_type": "markdown", @@ -202,6 +329,164 @@ "**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": 117, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many rounds would you like to play (must be an odd number)3\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):v\n", + "<< spock >> beats << rock >> - you win this round!\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):p\n", + "<< scissors >> beats << paper >> - computer takes the round!\n", + "Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):l\n", + "<< lizard >> beats << spock >> - you win this round!\n", + "Player_wins: 2\n", + "Computer_wins: 1\n", + "Ties: 0\n", + "You win the game!\n", + "Play again? y/nn\n", + "Farewell!\n" + ] + } + ], + "source": [ + "from random import choice\n", + "from time import sleep\n", + "\n", + "gestures = [\"rock\", \"paper\", \"scissors\", \"lizard\", \"spock\"]\n", + "global n_rounds\n", + "global cpu_score\n", + "global player_score\n", + "global ties\n", + "\n", + "\n", + "def select_rounds():\n", + " global n_rounds\n", + " while True:\n", + " try: \n", + " n_rounds = int(input(\"How many rounds would you like to play (must be an odd number): \"))\n", + " if n_rounds % 2 == 0:\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again - enter odd number in numerals\")\n", + " except Exception:\n", + " print(\"Needs to be an odd number: 1,3,5.. etc\")\n", + " \n", + "\n", + "def computer_choose():\n", + " return choice(['rock','paper','scissors','lizard','spock'])\n", + "\n", + "\n", + "def player_choose():\n", + " while True:\n", + " try: \n", + " player_choice = str(input(\"Pick Rock(r), Paper(p), Scissors(s), Lizard(l) or Spock(v):\"))\n", + " player_choice = player_choice.lower()\n", + " if player_choice == 'r': \n", + " player_choice = \"rock\"\n", + " elif player_choice == 'p': \n", + " player_choice = 'paper'\n", + " elif player_choice == 's': \n", + " player_choice = 'scissors'\n", + " elif player_choice == 'l': \n", + " player_choice = 'lizard'\n", + " elif player_choice == 'v': \n", + " player_choice = 'spock'\n", + " if player_choice not in gestures:\n", + " print(player_choice, \" is not in \", gestures)\n", + " raise Exception()\n", + " break\n", + " except Exception:\n", + " print(\"You chose:\",player_choice, \". Please enter one of the options - you can use the single letter keys too \")\n", + " return player_choice\n", + "\n", + "def who_won(computer_go, player_go):\n", + " if computer_go == player_go:\n", + " return 0\n", + " elif (computer_go == 'rock' and (player_go == 'scissors' or player_go == 'lizard')):\n", + " return 1\n", + " elif (computer_go == 'scissors' and (player_go == 'paper' or player_go == 'lizard' )):\n", + " return 1\n", + " elif (computer_go == 'paper' and (player_go == 'rock' or player_go == 'spock')):\n", + " return 1\n", + " elif (computer_go == 'lizard' and (player_go == 'spock' or player_go == 'paper')):\n", + " return 1\n", + " elif (computer_go == 'spock' and (player_go == 'rock' or player_go == 'scissors')):\n", + " return 1\n", + " else:\n", + " return 2\n", + "\n", + "def game_round():\n", + " global player_score\n", + " global cpu_score\n", + " global ties\n", + " player_pick = player_choose()\n", + " computer_pick = computer_choose()\n", + " winner = who_won(computer_pick,player_pick)\n", + " \n", + " if winner == 0:\n", + " print(\"It is a tie! You both selected <<\", player_pick, \">>\")\n", + " ties += 1\n", + " elif winner == 1: \n", + " print(\"<<\",computer_pick, \">> beats <<\", player_pick, \">> - computer takes the round!\")\n", + " cpu_score += 1\n", + " elif winner == 2:\n", + " print(\"<<\",player_pick, \">> beats <<\", computer_pick, \">> - you win this round!\")\n", + " player_score += 1\n", + " \n", + " sleep(2)\n", + "\n", + "continue_play = True\n", + "\n", + "while continue_play:\n", + "\n", + " select_rounds()\n", + " rounds_to_win = int(n_rounds / 2)+1\n", + " rounds = 0\n", + " ties = 0\n", + " cpu_score = 0\n", + " player_score = 0\n", + "\n", + " while rounds <= n_rounds and cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " game_round()\n", + " rounds+=1\n", + " \n", + " print(\"Player_wins:\", player_score)\n", + " print(\"Computer_wins:\", cpu_score)\n", + " print(\"Ties:\", ties)\n", + "\n", + " if player_score > cpu_score:\n", + " print(\"You win the game!\")\n", + " elif player_score < cpu_score:\n", + " print(\"Computer won this game!\")\n", + " else:\n", + " print(\"The game is a Tie!\")\n", + " \n", + " while True:\n", + " try: \n", + " play = input(\"Play again? (y/n): \")\n", + " play = play.lower()\n", + " if play != 'y' and play != 'n' :\n", + " raise Exception()\n", + " break\n", + " except ValueError:\n", + " print(\"Try again = 'y' or 'n'\")\n", + " except Exception:\n", + " print(\"Try again = 'y' or 'n'\")\n", + " \n", + " if play == 'n':\n", + " continue_play = False\n", + " print(\"Farewell!\")\n", + " \n", + " " + ] + }, { "cell_type": "code", "execution_count": null, @@ -226,7 +511,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/2.-Statistics/1-1 Practice quiz on Sets.png b/2.-Statistics/1-1 Practice quiz on Sets.png new file mode 100644 index 000000000..ae156661b Binary files /dev/null and b/2.-Statistics/1-1 Practice quiz on Sets.png differ diff --git a/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities.png b/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities.png new file mode 100644 index 000000000..76932d04a Binary files /dev/null and b/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities.png differ diff --git a/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities2.png b/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities2.png new file mode 100644 index 000000000..ca8ae9a67 Binary files /dev/null and b/2.-Statistics/1-2 Practice quiz on the Number Line including Inequalities2.png differ diff --git a/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation.png b/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation.png new file mode 100644 index 000000000..16e358860 Binary files /dev/null and b/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation.png differ diff --git a/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation2.png b/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation2.png new file mode 100644 index 000000000..b19b09dca Binary files /dev/null and b/2.-Statistics/1-3 Practice quiz on Simplification Rules and Sigma Notation2.png differ diff --git a/2.-Statistics/1-4 Graded Quiz.png b/2.-Statistics/1-4 Graded Quiz.png new file mode 100644 index 000000000..371cf936a Binary files /dev/null and b/2.-Statistics/1-4 Graded Quiz.png differ diff --git a/2.-Statistics/1-4 Graded Quiz2.png b/2.-Statistics/1-4 Graded Quiz2.png new file mode 100644 index 000000000..057116dbf Binary files /dev/null and b/2.-Statistics/1-4 Graded Quiz2.png differ diff --git a/2.-Statistics/1-4 Graded Quiz3.png b/2.-Statistics/1-4 Graded Quiz3.png new file mode 100644 index 000000000..16313ab62 Binary files /dev/null and b/2.-Statistics/1-4 Graded Quiz3.png differ diff --git a/2.-Statistics/2-1 Practice quiz on Cartesian Plane.png b/2.-Statistics/2-1 Practice quiz on Cartesian Plane.png new file mode 100644 index 000000000..9edc92c1e Binary files /dev/null and b/2.-Statistics/2-1 Practice quiz on Cartesian Plane.png differ diff --git a/2.-Statistics/2-1 Practice quiz on Cartesian Plane2.png b/2.-Statistics/2-1 Practice quiz on Cartesian Plane2.png new file mode 100644 index 000000000..ead3e6dbb Binary files /dev/null and b/2.-Statistics/2-1 Practice quiz on Cartesian Plane2.png differ diff --git a/2.-Statistics/2-2 Types of Functions.png b/2.-Statistics/2-2 Types of Functions.png new file mode 100644 index 000000000..006043241 Binary files /dev/null and b/2.-Statistics/2-2 Types of Functions.png differ diff --git a/2.-Statistics/2-2 Types of Functions2.png b/2.-Statistics/2-2 Types of Functions2.png new file mode 100644 index 000000000..2fdf4d586 Binary files /dev/null and b/2.-Statistics/2-2 Types of Functions2.png differ diff --git a/2.-Statistics/2-2 Types of Functions3.png b/2.-Statistics/2-2 Types of Functions3.png new file mode 100644 index 000000000..2f4926a92 Binary files /dev/null and b/2.-Statistics/2-2 Types of Functions3.png differ diff --git a/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function.png b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function.png new file mode 100644 index 000000000..8cbbd1156 Binary files /dev/null and b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function.png differ diff --git a/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function2.png b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function2.png new file mode 100644 index 000000000..36d950120 Binary files /dev/null and b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function2.png differ diff --git a/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function3.png b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function3.png new file mode 100644 index 000000000..cf6120025 Binary files /dev/null and b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function3.png differ diff --git a/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function4.png b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function4.png new file mode 100644 index 000000000..c094d88e9 Binary files /dev/null and b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function4.png differ diff --git a/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function5.png b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function5.png new file mode 100644 index 000000000..f56c8d9be Binary files /dev/null and b/2.-Statistics/2-3 Graded Quiz on Cartesian PLane and Types of Function5.png differ diff --git a/2.-Statistics/3-1 Practice Quiz on Tangent Lines to Functions.png b/2.-Statistics/3-1 Practice Quiz on Tangent Lines to Functions.png new file mode 100644 index 000000000..39f31b403 Binary files /dev/null and b/2.-Statistics/3-1 Practice Quiz on Tangent Lines to Functions.png differ diff --git a/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms.png b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms.png new file mode 100644 index 000000000..d1af3470c Binary files /dev/null and b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms.png differ diff --git a/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms2.png b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms2.png new file mode 100644 index 000000000..56fd02852 Binary files /dev/null and b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms2.png differ diff --git a/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms3.png b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms3.png new file mode 100644 index 000000000..2fc094080 Binary files /dev/null and b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms3.png differ diff --git a/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms4.png b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms4.png new file mode 100644 index 000000000..7e8308d5a Binary files /dev/null and b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms4.png differ diff --git a/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms5.png b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms5.png new file mode 100644 index 000000000..2508bce10 Binary files /dev/null and b/2.-Statistics/3-2 Practice Quiz on Exponents and Logarithms5.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms.png new file mode 100644 index 000000000..a87231f5c Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms2.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms2.png new file mode 100644 index 000000000..35a48c4c9 Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms2.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms3.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms3.png new file mode 100644 index 000000000..637347152 Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms3.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms4.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms4.png new file mode 100644 index 000000000..ddecb4a8b Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms4.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms5.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms5.png new file mode 100644 index 000000000..f2f7541f5 Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms5.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms6.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms6.png new file mode 100644 index 000000000..1268169c2 Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms6.png differ diff --git a/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms7.png b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms7.png new file mode 100644 index 000000000..49012c6da Binary files /dev/null and b/2.-Statistics/3-3 Graded quiz -on Tangent lines to functions exponents and logarithms7.png differ diff --git a/2.-Statistics/4-1 Practice quiz -Probability concepts.png b/2.-Statistics/4-1 Practice quiz -Probability concepts.png new file mode 100644 index 000000000..a358fe510 Binary files /dev/null and b/2.-Statistics/4-1 Practice quiz -Probability concepts.png differ diff --git a/2.-Statistics/4-1 Practice quiz -Probability concepts2.png b/2.-Statistics/4-1 Practice quiz -Probability concepts2.png new file mode 100644 index 000000000..7e9b9c9c4 Binary files /dev/null and b/2.-Statistics/4-1 Practice quiz -Probability concepts2.png differ diff --git a/2.-Statistics/4-1 Practice quiz -Probability concepts3.png b/2.-Statistics/4-1 Practice quiz -Probability concepts3.png new file mode 100644 index 000000000..e070cb675 Binary files /dev/null and b/2.-Statistics/4-1 Practice quiz -Probability concepts3.png differ diff --git a/2.-Statistics/4-1 Practice quiz -Probability concepts4.png b/2.-Statistics/4-1 Practice quiz -Probability concepts4.png new file mode 100644 index 000000000..883e6ac76 Binary files /dev/null and b/2.-Statistics/4-1 Practice quiz -Probability concepts4.png differ diff --git a/2.-Statistics/4-2 Practice quiz -Problem Solving.png b/2.-Statistics/4-2 Practice quiz -Problem Solving.png new file mode 100644 index 000000000..4e353d4f9 Binary files /dev/null and b/2.-Statistics/4-2 Practice quiz -Problem Solving.png differ diff --git a/2.-Statistics/4-2 Practice quiz -Problem Solving2.png b/2.-Statistics/4-2 Practice quiz -Problem Solving2.png new file mode 100644 index 000000000..c13a88273 Binary files /dev/null and b/2.-Statistics/4-2 Practice quiz -Problem Solving2.png differ diff --git a/2.-Statistics/4-2 Practice quiz -Problem Solving3.png b/2.-Statistics/4-2 Practice quiz -Problem Solving3.png new file mode 100644 index 000000000..13a112fed Binary files /dev/null and b/2.-Statistics/4-2 Practice quiz -Problem Solving3.png differ diff --git a/2.-Statistics/4-2 Practice quiz -Problem Solving4.png b/2.-Statistics/4-2 Practice quiz -Problem Solving4.png new file mode 100644 index 000000000..e5f827e40 Binary files /dev/null and b/2.-Statistics/4-2 Practice quiz -Problem Solving4.png differ diff --git a/2.-Statistics/4-2 Practice quiz -Problem Solving5.png b/2.-Statistics/4-2 Practice quiz -Problem Solving5.png new file mode 100644 index 000000000..5610c7cf1 Binary files /dev/null and b/2.-Statistics/4-2 Practice quiz -Problem Solving5.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial.png new file mode 100644 index 000000000..b1d02b736 Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial2.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial2.png new file mode 100644 index 000000000..26a7d2d74 Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial2.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial3.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial3.png new file mode 100644 index 000000000..27260913f Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial3.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial4.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial4.png new file mode 100644 index 000000000..4e95c0edf Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial4.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial5.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial5.png new file mode 100644 index 000000000..97be77ee2 Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial5.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial6.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial6.png new file mode 100644 index 000000000..64294d96c Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial6.png differ diff --git a/2.-Statistics/4-3 Practice quiz -Bayes and Binomial7.png b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial7.png new file mode 100644 index 000000000..72a3cdb41 Binary files /dev/null and b/2.-Statistics/4-3 Practice quiz -Bayes and Binomial7.png differ diff --git a/2.-Statistics/4-4 Graded quiz -Probability.png b/2.-Statistics/4-4 Graded quiz -Probability.png new file mode 100644 index 000000000..cca2ecb16 Binary files /dev/null and b/2.-Statistics/4-4 Graded quiz -Probability.png differ diff --git a/2.-Statistics/4-4 Graded quiz -Probability2.png b/2.-Statistics/4-4 Graded quiz -Probability2.png new file mode 100644 index 000000000..127824749 Binary files /dev/null and b/2.-Statistics/4-4 Graded quiz -Probability2.png differ diff --git a/2.-Statistics/4-4 Graded quiz -Probability3.png b/2.-Statistics/4-4 Graded quiz -Probability3.png new file mode 100644 index 000000000..a243b5e7d Binary files /dev/null and b/2.-Statistics/4-4 Graded quiz -Probability3.png differ diff --git a/robot.md b/robot.md new file mode 100644 index 000000000..e69de29bb