diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..dfdad60cc Binary files /dev/null and b/.DS_Store differ diff --git a/1.-Python/.DS_Store b/1.-Python/.DS_Store new file mode 100644 index 000000000..bb082bd22 Binary files /dev/null and b/1.-Python/.DS_Store differ 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..0c4ff4e65 --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/.ipynb_checkpoints/snail-and-well-checkpoint.ipynb @@ -0,0 +1,335 @@ +{ + "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": 93, + "metadata": {}, + "outputs": [], + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = 20\n", + "snail_position = 30" + ] + }, + { + "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": 94, + "metadata": {}, + "outputs": [], + "source": [ + "days = 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "on day number 1 the snail_position value is 30\n", + "on day number 2 the snail_position value is 40\n", + "on day number 3 the snail_position value is 50\n", + "on day number 4 the snail_position value is 60\n", + "on day number 5 the snail_position value is 70\n", + "on day number 6 the snail_position value is 80\n", + "on day number 7 the snail_position value is 90\n", + "on day number 8 the snail_position value is 100\n", + "on day number 9 the snail_position value is 110\n", + "on day number 10 the snail_position value is 120\n", + "on day number 11 the snail_position value is 130\n" + ] + } + ], + "source": [ + "print(\"on day number\", days, \"the snail_position value is\", snail_position, )\n", + "while snail_position <= 125:\n", + " snail_position = snail_position - nightly_distance + daily_distance\n", + " days += 1\n", + " print(\"on day number\", days, \"the snail_position value is\", snail_position, )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number 11\n" + ] + } + ], + "source": [ + "print(\"counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number\", days)" + ] + }, + { + "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": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "on day 2 temporary position is 31\n", + "on day 3 temporary position is 44\n", + "on day 4 temporary position is 101\n", + "on day 5 temporary position is 125\n", + "on day 6 temporary position is 150\n", + "Day number where snails gets out is 6 and its theoretical position would have been 150\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "\n", + "days = 1\n", + "snail_position = 30\n", + "iteration = 1\n", + "\n", + "while snail_position <= 125:\n", + " snail_position = snail_position - nightly_distance + advance_cm[iteration]\n", + " iteration += 1\n", + " days += 1\n", + " print(\"on day\", days, \"temporary position is\", snail_position)\n", + "print(\"Day number where snails gets out is\", days, \"and its theoretical position would have been\", snail_position)" + ] + }, + { + "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": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "maximum displacement in one day is 77 and the minimum displacement in one day is 12\n", + "total displacement (sum of total distance risen) is 206 cm\n", + "total displacement (including slidden distances) is 306 cm\n" + ] + } + ], + "source": [ + "print(\"maximum displacement in one day is\", max(advance_cm), \"and the minimum displacement in one day is\", min(advance_cm))\n", + "\n", + "print(\"total displacement (sum of total distance risen) is\", sum(advance_cm[:5])+1, \"cm\")\n", + "\n", + "total_risen = sum(advance_cm[:5])+1\n", + "total_slid = nightly_distance * 5\n", + "\n", + "print(\"total displacement (including slidden distances) is\", total_risen + total_slid, \"cm\")\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. What is its average progress? Take into account the snail slides at night." + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "day_number 1 \n", + "distance 30 \n", + "previous_day_distance saved for next day 30 \n", + "progress 30\n", + "day_number 2 \n", + "distance 21 \n", + "previous_day_distance saved for next day 21 \n", + "progress 1\n", + "day_number 3 \n", + "distance 33 \n", + "previous_day_distance saved for next day 33 \n", + "progress 13\n", + "day_number 4 \n", + "distance 77 \n", + "previous_day_distance saved for next day 77 \n", + "progress 57\n", + "day_number 5 \n", + "distance 44 \n", + "previous_day_distance saved for next day 44 \n", + "progress 24\n", + "day_number 6 \n", + "distance 45 \n", + "previous_day_distance saved for next day 45 \n", + "progress 25\n", + "[30, 1, 13, 57, 24, 25]\n", + "average progress is 25.0\n" + ] + } + ], + "source": [ + "previous_day_distance = 0\n", + "nightly_distance = 0\n", + "progress = 0\n", + "days = 0\n", + "progress_array = []\n", + "\n", + "for distance in advance_cm[:6]:\n", + " progress = previous_day_distance - nightly_distance + distance - previous_day_distance\n", + " nightly_distance = 20\n", + " previous_day_distance = distance\n", + " days += 1\n", + " print(\"day_number\", days, \"\\ndistance\", distance, \"\\nprevious_day_distance saved for next day\", previous_day_distance, \"\\nprogress\", progress)\n", + " progress_array.append(progress)\n", + " \n", + "print(progress_array)\n", + "\n", + "average_progress_value = sum(progress_array)/len(progress_array)\n", + " \n", + "print(\"average progress is\", average_progress_value)" + ] + }, + { + "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": 100, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[25.0, 576.0, 144.0, 1024.0, 1.0, 0.0]\n", + "variance 295.0\n", + "stardard variation is the square root of 295\n" + ] + } + ], + "source": [ + "total_displacement = sum(progress_array)\n", + "distance_to_average = []\n", + "\n", + "for progress in progress_array:\n", + " distance_to_average.append((progress - average_progress_value)**2)\n", + "\n", + "print(distance_to_average)\n", + "\n", + "variance = sum(distance_to_average)/len(distance_to_average)\n", + "\n", + "print(\"variance\", variance)\n", + "\n", + "print(\"stardard variation is the square root of 295\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "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..0c4ff4e65 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": 93, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = 20\n", + "snail_position = 30" + ] }, { "cell_type": "markdown", @@ -44,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 94, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 1" + ] }, { "cell_type": "markdown", @@ -58,10 +65,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "on day number 1 the snail_position value is 30\n", + "on day number 2 the snail_position value is 40\n", + "on day number 3 the snail_position value is 50\n", + "on day number 4 the snail_position value is 60\n", + "on day number 5 the snail_position value is 70\n", + "on day number 6 the snail_position value is 80\n", + "on day number 7 the snail_position value is 90\n", + "on day number 8 the snail_position value is 100\n", + "on day number 9 the snail_position value is 110\n", + "on day number 10 the snail_position value is 120\n", + "on day number 11 the snail_position value is 130\n" + ] + } + ], + "source": [ + "print(\"on day number\", days, \"the snail_position value is\", snail_position, )\n", + "while snail_position <= 125:\n", + " snail_position = snail_position - nightly_distance + daily_distance\n", + " days += 1\n", + " print(\"on day number\", days, \"the snail_position value is\", snail_position, )" + ] }, { "cell_type": "markdown", @@ -72,10 +103,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 96, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number 11\n" + ] + } + ], + "source": [ + "print(\"counting the day where the snail falls into the well as day 1 and supposing the snail will try to get out that day as well, the snail should be out of the well on day number\", days)" + ] }, { "cell_type": "markdown", @@ -96,10 +139,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "on day 2 temporary position is 31\n", + "on day 3 temporary position is 44\n", + "on day 4 temporary position is 101\n", + "on day 5 temporary position is 125\n", + "on day 6 temporary position is 150\n", + "Day number where snails gets out is 6 and its theoretical position would have been 150\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "\n", + "days = 1\n", + "snail_position = 30\n", + "iteration = 1\n", + "\n", + "while snail_position <= 125:\n", + " snail_position = snail_position - nightly_distance + advance_cm[iteration]\n", + " iteration += 1\n", + " days += 1\n", + " print(\"on day\", days, \"temporary position is\", snail_position)\n", + "print(\"Day number where snails gets out is\", days, \"and its theoretical position would have been\", snail_position)" + ] }, { "cell_type": "markdown", @@ -111,10 +180,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 98, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "maximum displacement in one day is 77 and the minimum displacement in one day is 12\n", + "total displacement (sum of total distance risen) is 206 cm\n", + "total displacement (including slidden distances) is 306 cm\n" + ] + } + ], + "source": [ + "print(\"maximum displacement in one day is\", max(advance_cm), \"and the minimum displacement in one day is\", min(advance_cm))\n", + "\n", + "print(\"total displacement (sum of total distance risen) is\", sum(advance_cm[:5])+1, \"cm\")\n", + "\n", + "total_risen = sum(advance_cm[:5])+1\n", + "total_slid = nightly_distance * 5\n", + "\n", + "print(\"total displacement (including slidden distances) is\", total_risen + total_slid, \"cm\")\n", + "\n" + ] }, { "cell_type": "markdown", @@ -125,10 +214,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "day_number 1 \n", + "distance 30 \n", + "previous_day_distance saved for next day 30 \n", + "progress 30\n", + "day_number 2 \n", + "distance 21 \n", + "previous_day_distance saved for next day 21 \n", + "progress 1\n", + "day_number 3 \n", + "distance 33 \n", + "previous_day_distance saved for next day 33 \n", + "progress 13\n", + "day_number 4 \n", + "distance 77 \n", + "previous_day_distance saved for next day 77 \n", + "progress 57\n", + "day_number 5 \n", + "distance 44 \n", + "previous_day_distance saved for next day 44 \n", + "progress 24\n", + "day_number 6 \n", + "distance 45 \n", + "previous_day_distance saved for next day 45 \n", + "progress 25\n", + "[30, 1, 13, 57, 24, 25]\n", + "average progress is 25.0\n" + ] + } + ], + "source": [ + "previous_day_distance = 0\n", + "nightly_distance = 0\n", + "progress = 0\n", + "days = 0\n", + "progress_array = []\n", + "\n", + "for distance in advance_cm[:6]:\n", + " progress = previous_day_distance - nightly_distance + distance - previous_day_distance\n", + " nightly_distance = 20\n", + " previous_day_distance = distance\n", + " days += 1\n", + " print(\"day_number\", days, \"\\ndistance\", distance, \"\\nprevious_day_distance saved for next day\", previous_day_distance, \"\\nprogress\", progress)\n", + " progress_array.append(progress)\n", + " \n", + "print(progress_array)\n", + "\n", + "average_progress_value = sum(progress_array)/len(progress_array)\n", + " \n", + "print(\"average progress is\", average_progress_value)" + ] }, { "cell_type": "markdown", @@ -139,10 +281,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 100, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[25.0, 576.0, 144.0, 1024.0, 1.0, 0.0]\n", + "variance 295.0\n", + "stardard variation is the square root of 295\n" + ] + } + ], + "source": [ + "total_displacement = sum(progress_array)\n", + "distance_to_average = []\n", + "\n", + "for progress in progress_array:\n", + " distance_to_average.append((progress - average_progress_value)**2)\n", + "\n", + "print(distance_to_average)\n", + "\n", + "variance = sum(distance_to_average)/len(distance_to_average)\n", + "\n", + "print(\"variance\", variance)\n", + "\n", + "print(\"stardard variation is the square root of 295\")" + ] } ], "metadata": { @@ -161,7 +327,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "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..d8b14e799 --- /dev/null +++ b/1.-Python/2.-Duel-of-Sorcerers/.ipynb_checkpoints/duel-of-sorcerers-checkpoint.ipynb @@ -0,0 +1,424 @@ +{ + "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": 19, + "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": 20, + "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": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "spell_number 0 saruman_spell 23 gandalf_spell 10 exchange_result -13 gandalf_score 0 saruman_score 1\n", + "spell_number 1 saruman_spell 66 gandalf_spell 11 exchange_result -55 gandalf_score 0 saruman_score 2\n", + "spell_number 2 saruman_spell 12 gandalf_spell 13 exchange_result 1 gandalf_score 1 saruman_score 2\n", + "spell_number 3 saruman_spell 43 gandalf_spell 30 exchange_result -13 gandalf_score 1 saruman_score 3\n", + "spell_number 4 saruman_spell 12 gandalf_spell 22 exchange_result 10 gandalf_score 2 saruman_score 3\n", + "spell_number 5 saruman_spell 10 gandalf_spell 11 exchange_result 1 gandalf_score 3 saruman_score 3\n", + "spell_number 6 saruman_spell 44 gandalf_spell 10 exchange_result -34 gandalf_score 3 saruman_score 4\n", + "spell_number 7 saruman_spell 23 gandalf_spell 33 exchange_result 10 gandalf_score 4 saruman_score 4\n", + "spell_number 8 saruman_spell 12 gandalf_spell 22 exchange_result 10 gandalf_score 5 saruman_score 4\n", + "spell_number 9 saruman_spell 17 gandalf_spell 22 exchange_result 5 gandalf_score 6 saruman_score 4\n" + ] + } + ], + "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\n", + "\n", + "gandalf_wins = 0\n", + "saruman_wins = 0\n", + "\n", + "while spells <= 9:\n", + " cast_result = gandalf[spells] - saruman[spells]\n", + " \n", + " if cast_result > 0:\n", + " gandalf_wins += 1\n", + " elif cast_result < 0:\n", + " saruman_wins += 1\n", + " \n", + " print(\"spell_number\", spells, \"saruman_spell\", saruman[spells], \"gandalf_spell\", gandalf[spells], \"exchange_result\", cast_result, \"gandalf_score\", gandalf_wins, \"saruman_score\", saruman_wins)\n", + " spells += 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": 27, + "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": 28, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "\n", + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "spells = 0" + ] + }, + { + "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": 29, + "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": 30, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "clash_result = 0" + ] + }, + { + "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": 76, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "clash_number 0 clash_result 5 gandalf_score 1 saruman_score 0\n", + "clash_number 1 clash_result -5 gandalf_score 0 saruman_score 1\n", + "clash_number 2 clash_result 15 gandalf_score 1 saruman_score 0\n", + "clash_number 3 clash_result -40 gandalf_score 0 saruman_score 1\n", + "clash_number 4 clash_result 25 gandalf_score 1 saruman_score 0\n", + "clash_number 5 clash_result -30 gandalf_score 0 saruman_score 1\n", + "clash_number 6 clash_result 30 gandalf_score 1 saruman_score 0\n", + "clash_number 7 clash_result 5 gandalf_score 2 saruman_score 0\n", + "clash_number 8 clash_result 40 gandalf_score 3 saruman_score 0\n", + "Gandalf wins\n" + ] + } + ], + "source": [ + "spells = 0\n", + "gandalf_wins = 0\n", + "saruman_wins = 0\n", + "clash_result = 0\n", + "\n", + "\n", + "while gandalf_wins <= 2 and saruman_wins <= 2:\n", + " clash_result = POWER[gandalf[spells]] - POWER[saruman[spells]]\n", + " if clash_result == 0:\n", + " gandalf_wins += 1\n", + " saruman_wins += 1\n", + " elif clash_result > 0:\n", + " gandalf_wins += 1\n", + " saruman_wins = 0\n", + " else:\n", + " gandalf_wins = 0\n", + " saruman_wins += 1\n", + " print(\"clash_number\", spells, \"clash_result\", clash_result, \"gandalf_score\", gandalf_wins, \"saruman_score\", saruman_wins,)\n", + " spells += 1\n", + "\n", + " \n", + "if gandalf_wins == 3 & saruman_wins == 3:\n", + " print(\"It's a tie - or they're both dead!\")\n", + "elif gandalf_wins == 3:\n", + " print(\"Gandalf wins\")\n", + "else:\n", + " print(\"Saruman wins\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Find the average spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[50, 40, 40, 10, 50, 10, 40, 50, 50, 50]\n", + "[45, 45, 25, 50, 25, 40, 10, 45, 10, 10]\n", + "Average spell power Gandalf: 39.0 \n", + "Average spell power Saruman: 30.5\n" + ] + } + ], + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "for power in gandalf:\n", + " gandalf_power.append(POWER[power])\n", + "print(gandalf_power)\n", + "\n", + "for power in saruman:\n", + " saruman_power.append(POWER[power])\n", + "print(saruman_power)\n", + "\n", + "average_power_gandalf = sum(gandalf_power)/len(gandalf_power)\n", + "average_power_saruman = sum(saruman_power)/len(saruman_power)\n", + "\n", + "print(\"Average spell power Gandalf:\", average_power_gandalf, \"\\nAverage spell power Saruman:\", average_power_saruman)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pre_variance_gandalf [121.0, 1.0, 1.0, 841.0, 121.0, 841.0, 1.0, 121.0, 121.0, 121.0]\n", + "variance_gandalf 229.0\n", + "pre_variance_saruman [210.25, 210.25, 30.25, 380.25, 30.25, 90.25, 420.25, 210.25, 420.25, 420.25]\n", + "variance_saruman 242.25\n", + "Gandalf spell power standard deviation is: 15.132745950421556 \n", + "Saruman spell power standard deviation is: 15.56438241627338\n" + ] + } + ], + "source": [ + "index = 0\n", + "pre_variance_gandalf = []\n", + "variance_gandalf = 0\n", + "\n", + "pre_variance_saruman = []\n", + "variance_saruman = 0\n", + "\n", + "for pre_variance in gandalf_power:\n", + " pre_variance_gandalf.append((gandalf_power[index]-average_power_gandalf)**2)\n", + " index += 1\n", + "print(\"pre_variance_gandalf\", pre_variance_gandalf)\n", + "\n", + "variance_gandalf = sum(pre_variance_gandalf)/len(gandalf_power)\n", + "print(\"variance_gandalf\", variance_gandalf)\n", + "\n", + "index = 0\n", + "for pre_variance in saruman_power:\n", + " pre_variance_saruman.append((saruman_power[index]-average_power_saruman)**2)\n", + " index += 1\n", + "print(\"pre_variance_saruman\", pre_variance_saruman)\n", + "\n", + "variance_saruman = sum(pre_variance_saruman)/len(saruman_power)\n", + "print(\"variance_saruman\", variance_saruman)\n", + "\n", + "import math\n", + "\n", + "print(\"Gandalf spell power standard deviation is:\", math.sqrt(variance_gandalf),\n", + " \"\\nSaruman spell power standard deviation is:\", math.sqrt(variance_saruman))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "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..d8b14e799 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": 19, "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": 20, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -78,10 +85,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "spell_number 0 saruman_spell 23 gandalf_spell 10 exchange_result -13 gandalf_score 0 saruman_score 1\n", + "spell_number 1 saruman_spell 66 gandalf_spell 11 exchange_result -55 gandalf_score 0 saruman_score 2\n", + "spell_number 2 saruman_spell 12 gandalf_spell 13 exchange_result 1 gandalf_score 1 saruman_score 2\n", + "spell_number 3 saruman_spell 43 gandalf_spell 30 exchange_result -13 gandalf_score 1 saruman_score 3\n", + "spell_number 4 saruman_spell 12 gandalf_spell 22 exchange_result 10 gandalf_score 2 saruman_score 3\n", + "spell_number 5 saruman_spell 10 gandalf_spell 11 exchange_result 1 gandalf_score 3 saruman_score 3\n", + "spell_number 6 saruman_spell 44 gandalf_spell 10 exchange_result -34 gandalf_score 3 saruman_score 4\n", + "spell_number 7 saruman_spell 23 gandalf_spell 33 exchange_result 10 gandalf_score 4 saruman_score 4\n", + "spell_number 8 saruman_spell 12 gandalf_spell 22 exchange_result 10 gandalf_score 5 saruman_score 4\n", + "spell_number 9 saruman_spell 17 gandalf_spell 22 exchange_result 5 gandalf_score 6 saruman_score 4\n" + ] + } + ], + "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\n", + "\n", + "gandalf_wins = 0\n", + "saruman_wins = 0\n", + "\n", + "while spells <= 9:\n", + " cast_result = gandalf[spells] - saruman[spells]\n", + " \n", + " if cast_result > 0:\n", + " gandalf_wins += 1\n", + " elif cast_result < 0:\n", + " saruman_wins += 1\n", + " \n", + " print(\"spell_number\", spells, \"saruman_spell\", saruman[spells], \"gandalf_spell\", gandalf[spells], \"exchange_result\", cast_result, \"gandalf_score\", gandalf_wins, \"saruman_score\", saruman_wins)\n", + " spells += 1\n", + " " + ] }, { "cell_type": "markdown", @@ -93,10 +136,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "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 +186,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "\n", + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "spells = 0" + ] }, { "cell_type": "markdown", @@ -142,10 +216,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -156,10 +233,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "clash_result = 0" + ] }, { "cell_type": "markdown", @@ -171,10 +252,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 76, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "clash_number 0 clash_result 5 gandalf_score 1 saruman_score 0\n", + "clash_number 1 clash_result -5 gandalf_score 0 saruman_score 1\n", + "clash_number 2 clash_result 15 gandalf_score 1 saruman_score 0\n", + "clash_number 3 clash_result -40 gandalf_score 0 saruman_score 1\n", + "clash_number 4 clash_result 25 gandalf_score 1 saruman_score 0\n", + "clash_number 5 clash_result -30 gandalf_score 0 saruman_score 1\n", + "clash_number 6 clash_result 30 gandalf_score 1 saruman_score 0\n", + "clash_number 7 clash_result 5 gandalf_score 2 saruman_score 0\n", + "clash_number 8 clash_result 40 gandalf_score 3 saruman_score 0\n", + "Gandalf wins\n" + ] + } + ], + "source": [ + "spells = 0\n", + "gandalf_wins = 0\n", + "saruman_wins = 0\n", + "clash_result = 0\n", + "\n", + "\n", + "while gandalf_wins <= 2 and saruman_wins <= 2:\n", + " clash_result = POWER[gandalf[spells]] - POWER[saruman[spells]]\n", + " if clash_result == 0:\n", + " gandalf_wins += 1\n", + " saruman_wins += 1\n", + " elif clash_result > 0:\n", + " gandalf_wins += 1\n", + " saruman_wins = 0\n", + " else:\n", + " gandalf_wins = 0\n", + " saruman_wins += 1\n", + " print(\"clash_number\", spells, \"clash_result\", clash_result, \"gandalf_score\", gandalf_wins, \"saruman_score\", saruman_wins,)\n", + " spells += 1\n", + "\n", + " \n", + "if gandalf_wins == 3 & saruman_wins == 3:\n", + " print(\"It's a tie - or they're both dead!\")\n", + "elif gandalf_wins == 3:\n", + " print(\"Gandalf wins\")\n", + "else:\n", + " print(\"Saruman wins\")" + ] }, { "cell_type": "markdown", @@ -185,10 +311,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[50, 40, 40, 10, 50, 10, 40, 50, 50, 50]\n", + "[45, 45, 25, 50, 25, 40, 10, 45, 10, 10]\n", + "Average spell power Gandalf: 39.0 \n", + "Average spell power Saruman: 30.5\n" + ] + } + ], + "source": [ + "gandalf_power = []\n", + "saruman_power = []\n", + "\n", + "for power in gandalf:\n", + " gandalf_power.append(POWER[power])\n", + "print(gandalf_power)\n", + "\n", + "for power in saruman:\n", + " saruman_power.append(POWER[power])\n", + "print(saruman_power)\n", + "\n", + "average_power_gandalf = sum(gandalf_power)/len(gandalf_power)\n", + "average_power_saruman = sum(saruman_power)/len(saruman_power)\n", + "\n", + "print(\"Average spell power Gandalf:\", average_power_gandalf, \"\\nAverage spell power Saruman:\", average_power_saruman)" + ] }, { "cell_type": "markdown", @@ -199,10 +352,52 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "pre_variance_gandalf [121.0, 1.0, 1.0, 841.0, 121.0, 841.0, 1.0, 121.0, 121.0, 121.0]\n", + "variance_gandalf 229.0\n", + "pre_variance_saruman [210.25, 210.25, 30.25, 380.25, 30.25, 90.25, 420.25, 210.25, 420.25, 420.25]\n", + "variance_saruman 242.25\n", + "Gandalf spell power standard deviation is: 15.132745950421556 \n", + "Saruman spell power standard deviation is: 15.56438241627338\n" + ] + } + ], + "source": [ + "index = 0\n", + "pre_variance_gandalf = []\n", + "variance_gandalf = 0\n", + "\n", + "pre_variance_saruman = []\n", + "variance_saruman = 0\n", + "\n", + "for pre_variance in gandalf_power:\n", + " pre_variance_gandalf.append((gandalf_power[index]-average_power_gandalf)**2)\n", + " index += 1\n", + "print(\"pre_variance_gandalf\", pre_variance_gandalf)\n", + "\n", + "variance_gandalf = sum(pre_variance_gandalf)/len(gandalf_power)\n", + "print(\"variance_gandalf\", variance_gandalf)\n", + "\n", + "index = 0\n", + "for pre_variance in saruman_power:\n", + " pre_variance_saruman.append((saruman_power[index]-average_power_saruman)**2)\n", + " index += 1\n", + "print(\"pre_variance_saruman\", pre_variance_saruman)\n", + "\n", + "variance_saruman = sum(pre_variance_saruman)/len(saruman_power)\n", + "print(\"variance_saruman\", variance_saruman)\n", + "\n", + "import math\n", + "\n", + "print(\"Gandalf spell power standard deviation is:\", math.sqrt(variance_gandalf),\n", + " \"\\nSaruman spell power standard deviation is:\", math.sqrt(variance_saruman))" + ] } ], "metadata": { @@ -221,7 +416,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "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..0e5289c2a --- /dev/null +++ b/1.-Python/3.-Bus/.ipynb_checkpoints/bus-checkpoint.ipynb @@ -0,0 +1,224 @@ +{ + "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": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "all elements of tuple: (4, 1)\n", + "first element of indexed tuple: 4\n", + "second element of indexed tuple: 1\n" + ] + } + ], + "source": [ + "# Variables\n", + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]\n", + "\n", + "print(\"all elements of tuple:\", stops[1])\n", + "print(\"first element of indexed tuple:\", stops[1][0])\n", + "print(\"second element of indexed tuple:\", stops[1][1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1. Calculate the number of stops." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of stops: 9\n" + ] + } + ], + "source": [ + "stop_number = len(stops)\n", + "print(\"Number of stops:\", stop_number)\n", + "\n" + ] + }, + { + "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": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 3, -2, -1, 4, -4, -3, -2, -1]\n" + ] + } + ], + "source": [ + "passengers_difference_per_stop = []\n", + "index = 0\n", + "\n", + "for x in range(9):\n", + " passengers_difference_per_stop.append(stops[x][0] - stops[x][1])\n", + " \n", + "print(passengers_difference_per_stop)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the maximum occupation of the bus." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10]\n", + "[10, 13]\n", + "[10, 13, 11]\n", + "[10, 13, 11, 10]\n", + "[10, 13, 11, 10, 14]\n", + "[10, 13, 11, 10, 14, 10]\n", + "[10, 13, 11, 10, 14, 10, 7]\n", + "[10, 13, 11, 10, 14, 10, 7, 5]\n", + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\n", + "Maximum occupation of the bus: 14\n" + ] + } + ], + "source": [ + "ongoing_occupation = 0\n", + "bus_occupation_list = []\n", + "\n", + "for x in range(len(passengers_difference_per_stop)):\n", + " bus_occupation_list.append(ongoing_occupation + passengers_difference_per_stop[x])\n", + " ongoing_occupation = bus_occupation_list[x]\n", + " print(bus_occupation_list)\n", + " \n", + "print(\"Maximum occupation of the bus:\", max(bus_occupation_list))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average occupation: 9.333333333333334\n", + "[0.44444444444444364, 13.44444444444444, 2.777777777777776, 0.44444444444444364, 21.77777777777777, 0.44444444444444364, 5.444444444444447, 18.777777777777782, 28.44444444444445]\n", + "variance: 10.222222222222221\n", + "standard deviation: 3.197221015541813\n" + ] + } + ], + "source": [ + "average_occupation = sum(bus_occupation_list)/len(bus_occupation_list)\n", + "\n", + "print(\"Average occupation:\", Average_occupation)\n", + "\n", + "pre_variance = []\n", + "variance = float()\n", + "\n", + "for occupation in bus_occupation_list:\n", + " pre_variance.append((occupation - average_occupation)**2)\n", + "\n", + "print(pre_variance)\n", + "\n", + "variance = sum(pre_variance)/len(pre_variance)\n", + "print(\"variance:\", variance)\n", + "\n", + "import math\n", + "\n", + "print(\"standard deviation:\", math.sqrt(variance))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8fd..0e5289c2a 100644 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -35,12 +35,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "all elements of tuple: (4, 1)\n", + "first element of indexed tuple: 4\n", + "second element of indexed tuple: 1\n" + ] + } + ], "source": [ "# Variables\n", - "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]" + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]\n", + "\n", + "print(\"all elements of tuple:\", stops[1])\n", + "print(\"first element of indexed tuple:\", stops[1][0])\n", + "print(\"second element of indexed tuple:\", stops[1][1])" ] }, { @@ -52,10 +66,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of stops: 9\n" + ] + } + ], + "source": [ + "stop_number = len(stops)\n", + "print(\"Number of stops:\", stop_number)\n", + "\n" + ] }, { "cell_type": "markdown", @@ -67,10 +93,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 3, -2, -1, 4, -4, -3, -2, -1]\n" + ] + } + ], + "source": [ + "passengers_difference_per_stop = []\n", + "index = 0\n", + "\n", + "for x in range(9):\n", + " passengers_difference_per_stop.append(stops[x][0] - stops[x][1])\n", + " \n", + "print(passengers_difference_per_stop)" + ] }, { "cell_type": "markdown", @@ -81,10 +123,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10]\n", + "[10, 13]\n", + "[10, 13, 11]\n", + "[10, 13, 11, 10]\n", + "[10, 13, 11, 10, 14]\n", + "[10, 13, 11, 10, 14, 10]\n", + "[10, 13, 11, 10, 14, 10, 7]\n", + "[10, 13, 11, 10, 14, 10, 7, 5]\n", + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\n", + "Maximum occupation of the bus: 14\n" + ] + } + ], + "source": [ + "ongoing_occupation = 0\n", + "bus_occupation_list = []\n", + "\n", + "for x in range(len(passengers_difference_per_stop)):\n", + " bus_occupation_list.append(ongoing_occupation + passengers_difference_per_stop[x])\n", + " ongoing_occupation = bus_occupation_list[x]\n", + " print(bus_occupation_list)\n", + " \n", + "print(\"Maximum occupation of the bus:\", max(bus_occupation_list))" + ] }, { "cell_type": "markdown", @@ -95,10 +164,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average occupation: 9.333333333333334\n", + "[0.44444444444444364, 13.44444444444444, 2.777777777777776, 0.44444444444444364, 21.77777777777777, 0.44444444444444364, 5.444444444444447, 18.777777777777782, 28.44444444444445]\n", + "variance: 10.222222222222221\n", + "standard deviation: 3.197221015541813\n" + ] + } + ], + "source": [ + "average_occupation = sum(bus_occupation_list)/len(bus_occupation_list)\n", + "\n", + "print(\"Average occupation:\", Average_occupation)\n", + "\n", + "pre_variance = []\n", + "variance = float()\n", + "\n", + "for occupation in bus_occupation_list:\n", + " pre_variance.append((occupation - average_occupation)**2)\n", + "\n", + "print(pre_variance)\n", + "\n", + "variance = sum(pre_variance)/len(pre_variance)\n", + "print(\"variance:\", variance)\n", + "\n", + "import math\n", + "\n", + "print(\"standard deviation:\", math.sqrt(variance))" + ] } ], "metadata": { @@ -117,7 +216,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "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..596c596c6 --- /dev/null +++ b/1.-Python/4.-Robin-Hood/.ipynb_checkpoints/robin-hood-checkpoint.ipynb @@ -0,0 +1,268 @@ +{ + "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": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "22" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "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)]\n", + "\n", + "len(points)\n" + ] + }, + { + "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": 30, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(4, 5)\n", + "(5, 7)\n", + "(5, 7)\n", + "(2, 2)\n", + "(-3, 2)\n", + "(5, 7)\n", + "Unique coordinates of arrow in arrow: {(-3, 2), (4, 5), (5, 7), (2, 2)}\n" + ] + } + ], + "source": [ + "index1 = 0\n", + "index2 = 1\n", + "\n", + "arrow_on_arrow = set()\n", + "\n", + "while index1 <= len(points)-2:\n", + " for x in range(index2, len(points)):\n", + " #print(points[index1], \"compared to\", points[index2])\n", + " if points[index1] == points[index2]:\n", + " print(points[index1])\n", + " arrow_on_arrow.add(points[index1])\n", + " index2 += 1\n", + " index1 += 1\n", + " index2 = index1 + 1\n", + " \n", + "print(\"Unique coordinates of arrow in arrow:\", arrow_on_arrow)" + ] + }, + { + "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": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Arrows in Q1: 11 \n", + "Arrows in Q2: 3 \n", + "Arrows in Q3: 6 \n", + "Arrows in Q4: 2\n" + ] + } + ], + "source": [ + "#Q1: x positive, y positive\n", + "#Q2: x positive, y negative\n", + "#Q1: x negative, y positive\n", + "#Q1: x negative, y negative\n", + "#QX: x=0,y=0 \n", + "\n", + "qx_arrows = 0\n", + "q1_arrows = 0\n", + "q2_arrows = 0\n", + "q3_arrows = 0\n", + "q4_arrows = 0\n", + "\n", + "for x in range(len(points)):\n", + " if points[x][0] == 0 and points[x][1] == 0:\n", + " qx_arrows += 1\n", + " elif points[x][0] >= 0 and points[x][1] >= 0:\n", + " q1_arrows += 1\n", + " elif points[x][0] >= 0 and points[x][1] <= 0:\n", + " q2_arrows += 1\n", + " elif points[x][0] <= 0 and points[x][1] >= 0:\n", + " q3_arrows += 1\n", + " elif points[x][0] <= 0 and points[x][1] <= 0:\n", + " q4_arrows += 1\n", + " \n", + "print(\"Arrows in Q1:\", q1_arrows,\n", + " \"\\nArrows in Q2:\", q2_arrows,\n", + " \"\\nArrows in Q3:\", q3_arrows,\n", + " \"\\nArrows in Q4:\", q4_arrows,\n", + " )\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the point closest to the center. Calculate its distance to the center. \n", + "Take into account that there might be more than one point at the minimum distance to the center.\n", + "\n", + "**Hint**: Use the Euclidean distance. You can find more information about it [here](https://en.wikipedia.org/wiki/Euclidean_distance). \n", + "**Hint**: Defining a function that calculates the distance to the center can help." + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 12.727922061357855, 12.041594578792296]\n", + "2.0\n", + "There are 2 arrows that are closest to the center and their positions are: [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def distance(a,b):\n", + " return math.sqrt(a**2 + b**2)\n", + "\n", + "distances_list = []\n", + "\n", + "for x in range(len(points)):\n", + " distances_list.append(distance(points[x][0],points[x][1])) \n", + " \n", + "print(distances_list)\n", + " \n", + "shortest_distance = min(distances_list)\n", + "\n", + "print(shortest_distance)\n", + "\n", + "closest_arrows = []\n", + "\n", + "for x in range(len(points)):\n", + " if distance(points[x][0],points[x][1]) == shortest_distance:\n", + " closest_arrows.append(points[x])\n", + " \n", + "print(\"There are\", len(closest_arrows), \"arrows that are closest to the center and their positions are:\", closest_arrows)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. If the archery target has a radius of 9, calculate the number of arrows that won't hit the target. \n", + "**Hint**: Use the function created in step 3. " + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 arrows will miss the target\n" + ] + } + ], + "source": [ + "arrows_count = 0\n", + "\n", + "for x in range(len(points)):\n", + " if distance(points[x][0],points[x][1]) > 9:\n", + " arrows_count += 1\n", + "\n", + "print(arrows_count, \"arrows will miss the target\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "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..596c596c6 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -38,12 +38,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "22" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "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)]" + " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]\n", + "\n", + "len(points)\n" ] }, { @@ -55,10 +68,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(4, 5)\n", + "(5, 7)\n", + "(5, 7)\n", + "(2, 2)\n", + "(-3, 2)\n", + "(5, 7)\n", + "Unique coordinates of arrow in arrow: {(-3, 2), (4, 5), (5, 7), (2, 2)}\n" + ] + } + ], + "source": [ + "index1 = 0\n", + "index2 = 1\n", + "\n", + "arrow_on_arrow = set()\n", + "\n", + "while index1 <= len(points)-2:\n", + " for x in range(index2, len(points)):\n", + " #print(points[index1], \"compared to\", points[index2])\n", + " if points[index1] == points[index2]:\n", + " print(points[index1])\n", + " arrow_on_arrow.add(points[index1])\n", + " index2 += 1\n", + " index1 += 1\n", + " index2 = index1 + 1\n", + " \n", + "print(\"Unique coordinates of arrow in arrow:\", arrow_on_arrow)" + ] }, { "cell_type": "markdown", @@ -70,10 +114,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Arrows in Q1: 11 \n", + "Arrows in Q2: 3 \n", + "Arrows in Q3: 6 \n", + "Arrows in Q4: 2\n" + ] + } + ], + "source": [ + "#Q1: x positive, y positive\n", + "#Q2: x positive, y negative\n", + "#Q1: x negative, y positive\n", + "#Q1: x negative, y negative\n", + "#QX: x=0,y=0 \n", + "\n", + "qx_arrows = 0\n", + "q1_arrows = 0\n", + "q2_arrows = 0\n", + "q3_arrows = 0\n", + "q4_arrows = 0\n", + "\n", + "for x in range(len(points)):\n", + " if points[x][0] == 0 and points[x][1] == 0:\n", + " qx_arrows += 1\n", + " elif points[x][0] >= 0 and points[x][1] >= 0:\n", + " q1_arrows += 1\n", + " elif points[x][0] >= 0 and points[x][1] <= 0:\n", + " q2_arrows += 1\n", + " elif points[x][0] <= 0 and points[x][1] >= 0:\n", + " q3_arrows += 1\n", + " elif points[x][0] <= 0 and points[x][1] <= 0:\n", + " q4_arrows += 1\n", + " \n", + "print(\"Arrows in Q1:\", q1_arrows,\n", + " \"\\nArrows in Q2:\", q2_arrows,\n", + " \"\\nArrows in Q3:\", q3_arrows,\n", + " \"\\nArrows in Q4:\", q4_arrows,\n", + " )\n" + ] }, { "cell_type": "markdown", @@ -88,10 +173,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 12.727922061357855, 12.041594578792296]\n", + "2.0\n", + "There are 2 arrows that are closest to the center and their positions are: [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def distance(a,b):\n", + " return math.sqrt(a**2 + b**2)\n", + "\n", + "distances_list = []\n", + "\n", + "for x in range(len(points)):\n", + " distances_list.append(distance(points[x][0],points[x][1])) \n", + " \n", + "print(distances_list)\n", + " \n", + "shortest_distance = min(distances_list)\n", + "\n", + "print(shortest_distance)\n", + "\n", + "closest_arrows = []\n", + "\n", + "for x in range(len(points)):\n", + " if distance(points[x][0],points[x][1]) == shortest_distance:\n", + " closest_arrows.append(points[x])\n", + " \n", + "print(\"There are\", len(closest_arrows), \"arrows that are closest to the center and their positions are:\", closest_arrows)\n" + ] }, { "cell_type": "markdown", @@ -103,10 +222,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 arrows will miss the target\n" + ] + } + ], + "source": [ + "arrows_count = 0\n", + "\n", + "for x in range(len(points)):\n", + " if distance(points[x][0],points[x][1]) > 9:\n", + " arrows_count += 1\n", + "\n", + "print(arrows_count, \"arrows will miss the target\")" + ] } ], "metadata": { @@ -125,7 +260,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "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..ea23a3a8d --- /dev/null +++ b/1.-Python/5.-Temperature-Processor/.ipynb_checkpoints/temperature-checkpoint.ipynb @@ -0,0 +1,500 @@ +{ + "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": 17, + "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": 18, + "metadata": {}, + "outputs": [], + "source": [ + "min_day_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": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n" + ] + } + ], + "source": [ + "day_max_temp = max(temperatures_C)\n", + "\n", + "print(day_max_temp)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Create a list with the temperatures that are greater than or equal to 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "temps_more_or_equel_to_70 = []\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " if temperatures_C[x] >= 70:\n", + " temps_more_or_equel_to_70.append(temperatures_C[x])\n", + " \n", + "print(temps_more_or_equel_to_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": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "day_avg_temp = sum(temperatures_C)/len(temperatures_C)\n", + "\n", + "print(day_avg_temp)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Imagine that there was a sensor failure at 3am and the data for that specific hour was not recorded. How would you estimate the missing value? Replace the current value of the list at 3am for an estimation. " + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average of before and after temperature = estimation of lost temperature at 3 am = 62.0\n" + ] + } + ], + "source": [ + "lost_value = ((temperatures_C[2] + temperatures_C[4])/2)\n", + "\n", + "print(\"Average of before and after temperature = estimation of lost temperature at 3 am =\", lost_value)" + ] + }, + { + "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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[91.4, 150.8, 149.0, 32.0, 138.2, 140.0, 143.60000000000002, 147.2, 158.0, 168.8, 176.0, 177.8, 176.0, 181.4, 194.0, 174.20000000000002, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n" + ] + } + ], + "source": [ + "def celsius_to_fahrenheit(a):\n", + " return (a*1.8)+32\n", + "\n", + "temperatures_F = []\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " temperatures_F.append(celsius_to_fahrenheit(temperatures_C[x]))\n", + "\n", + "print(temperatures_F)" + ] + }, + { + "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": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "temperatures_equal_or_above_70: 8\n", + "Risk_assessment for above 80: 3\n", + "Average temperature: 60.25\n", + "Risk_assessment for above 80 + average over 65: 3\n", + "Final risk assessment (addition of 4 temperatures above 70): 4\n", + "We need to replace the cooling system\n" + ] + } + ], + "source": [ + "risk_assessment = 0\n", + "\n", + "temperatures_equal_or_above_70 = 0\n", + "\n", + "for x in (range(len(temperatures_C))):\n", + " if temperatures_C[x] >= 70:\n", + " temperatures_equal_or_above_70 += 1\n", + " if temperatures_C[x] > 80:\n", + " risk_assessment += 1\n", + "\n", + "print(\"temperatures_equal_or_above_70:\", temperatures_equal_or_above_70)\n", + "print(\"Risk_assessment for above 80:\", risk_assessment)\n", + " \n", + "if sum(temperatures_C)/len(temperatures_C) > 65:\n", + " risk_assessment += 1\n", + " \n", + "print(\"Average temperature:\", sum(temperatures_C)/len(temperatures_C))\n", + " \n", + "print(\"Risk_assessment for above 80 + average over 65:\", risk_assessment) \n", + " \n", + "if temperatures_equal_or_above_70 > 4:\n", + " risk_assessment += 1\n", + " \n", + "print(\"Final risk assessment (addition of 4 temperatures above 70):\", risk_assessment) \n", + "\n", + " \n", + "if risk_assessment > 0:\n", + " print(\"We need to replace the cooling system\")\n", + "else:\n", + " print(\"We can keep the current cooling system\")" + ] + }, + { + "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": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 10, 11, 12, 13, 14, 15]\n" + ] + } + ], + "source": [ + "hours_with_temp_more_than_70 = []\n", + "\n", + "for x in (range(len(temperatures_C))):\n", + " if temperatures_C[x] > 70:\n", + " hours_with_temp_more_than_70.append(x)\n", + " \n", + "print(hours_with_temp_more_than_70)\n", + "\n" + ] + }, + { + "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": 81, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "[7]\n" + ] + } + ], + "source": [ + "consecutive_hours = []\n", + "counter = 0\n", + "\n", + "for x in range(1,len(hours_with_temp_more_than_70)):\n", + " if x == len(hours_with_temp_more_than_70)-1 and counter > 0:\n", + " counter += 2\n", + " consecutive_hours.append(counter)\n", + " elif hours_with_temp_more_than_70[x-1]+1 == hours_with_temp_more_than_70[x]:\n", + " counter += 1\n", + " else:\n", + " counter += 1\n", + " consecutive_hours.append(counter)\n", + " counter = 0\n", + "\n", + "print(counter)\n", + "print(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": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "4\n", + "We need to replace the cooling system\n" + ] + } + ], + "source": [ + "new_test_count = 0\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " if temperatures_C[x] > 80:\n", + " new_test_count += 1\n", + "print(new_test_count)\n", + " \n", + "if sum(temperatures_C)/len(temperatures_C) > 65:\n", + " new_test_count += 1\n", + " \n", + "for x in range(len(consecutive_hours)):\n", + " if consecutive_hours[x] > 4:\n", + " new_test_count += 1\n", + " \n", + "print(new_test_count)\n", + "\n", + "\n", + "if new_test_count > 0:\n", + " print(\"We need to replace the cooling system\")\n", + "else:\n", + " print(\"We can keep the current cooling system\")" + ] + }, + { + "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": 90, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average value temp in C: 60.25 \n", + "Average value temp in F: 140.45\n", + "Relation between both average values is: 'Average value in F = 1.8∗Average value in F+32'\n" + ] + } + ], + "source": [ + "avg_value_temperatures_F = sum(temperatures_F)/len(temperatures_F)\n", + "avg_value_temperatures_C = sum(temperatures_C)/len(temperatures_C)\n", + "\n", + "print(\"Average value temp in C:\", avg_value_temperatures_C, \"\\nAverage value temp in F:\", avg_value_temperatures_F)\n", + "\n", + "celsius_to_fahrenheit(avg_value_temperatures_C)\n", + "\n", + "print(\"Relation between both average values is: 'Average value in F = 1.8∗Average value in F+32'\")" + ] + }, + { + "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": 97, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation temperatures in F = 170.06428196420316 \n", + "Standard deviation temperatures in C = 94.48015664677953\n", + "Relation between both standard deviations is a factor of 1.8\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def std_dev(average, list):\n", + " deviation_calculation = 0\n", + " deviation_sum = 0\n", + " for x in range(len(list)):\n", + " deviation_calculation = (list[x]-average)**2\n", + " deviation_sum = deviation_sum + deviation_calculation\n", + " return math.sqrt(deviation_sum)\n", + "\n", + "std_dev_temps_in_F = std_dev(avg_value_temperatures_F, temperatures_F)\n", + "\n", + "std_dev_temps_in_C = std_dev(avg_value_temperatures_C, temperatures_C)\n", + "\n", + "print(\"Standard deviation temperatures in F =\", std_dev_temps_in_F, \"\\nStandard deviation temperatures in C =\", std_dev_temps_in_C)\n", + "\n", + "print(\"Relation between both standard deviations is a factor of\", std_dev_temps_in_F/std_dev_temps_in_C)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "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..ea23a3a8d 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": 17, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "min_day_temp = min(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -67,10 +69,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n" + ] + } + ], + "source": [ + "day_max_temp = max(temperatures_C)\n", + "\n", + "print(day_max_temp)" + ] }, { "cell_type": "markdown", @@ -81,10 +95,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "temps_more_or_equel_to_70 = []\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " if temperatures_C[x] >= 70:\n", + " temps_more_or_equel_to_70.append(temperatures_C[x])\n", + " \n", + "print(temps_more_or_equel_to_70)" + ] }, { "cell_type": "markdown", @@ -95,10 +125,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "day_avg_temp = sum(temperatures_C)/len(temperatures_C)\n", + "\n", + "print(day_avg_temp)" + ] }, { "cell_type": "markdown", @@ -109,10 +151,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average of before and after temperature = estimation of lost temperature at 3 am = 62.0\n" + ] + } + ], + "source": [ + "lost_value = ((temperatures_C[2] + temperatures_C[4])/2)\n", + "\n", + "print(\"Average of before and after temperature = estimation of lost temperature at 3 am =\", lost_value)" + ] }, { "cell_type": "markdown", @@ -128,10 +182,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[91.4, 150.8, 149.0, 32.0, 138.2, 140.0, 143.60000000000002, 147.2, 158.0, 168.8, 176.0, 177.8, 176.0, 181.4, 194.0, 174.20000000000002, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n" + ] + } + ], + "source": [ + "def celsius_to_fahrenheit(a):\n", + " return (a*1.8)+32\n", + "\n", + "temperatures_F = []\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " temperatures_F.append(celsius_to_fahrenheit(temperatures_C[x]))\n", + "\n", + "print(temperatures_F)" + ] }, { "cell_type": "markdown", @@ -150,10 +222,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "temperatures_equal_or_above_70: 8\n", + "Risk_assessment for above 80: 3\n", + "Average temperature: 60.25\n", + "Risk_assessment for above 80 + average over 65: 3\n", + "Final risk assessment (addition of 4 temperatures above 70): 4\n", + "We need to replace the cooling system\n" + ] + } + ], + "source": [ + "risk_assessment = 0\n", + "\n", + "temperatures_equal_or_above_70 = 0\n", + "\n", + "for x in (range(len(temperatures_C))):\n", + " if temperatures_C[x] >= 70:\n", + " temperatures_equal_or_above_70 += 1\n", + " if temperatures_C[x] > 80:\n", + " risk_assessment += 1\n", + "\n", + "print(\"temperatures_equal_or_above_70:\", temperatures_equal_or_above_70)\n", + "print(\"Risk_assessment for above 80:\", risk_assessment)\n", + " \n", + "if sum(temperatures_C)/len(temperatures_C) > 65:\n", + " risk_assessment += 1\n", + " \n", + "print(\"Average temperature:\", sum(temperatures_C)/len(temperatures_C))\n", + " \n", + "print(\"Risk_assessment for above 80 + average over 65:\", risk_assessment) \n", + " \n", + "if temperatures_equal_or_above_70 > 4:\n", + " risk_assessment += 1\n", + " \n", + "print(\"Final risk assessment (addition of 4 temperatures above 70):\", risk_assessment) \n", + "\n", + " \n", + "if risk_assessment > 0:\n", + " print(\"We need to replace the cooling system\")\n", + "else:\n", + " print(\"We can keep the current cooling system\")" + ] }, { "cell_type": "markdown", @@ -175,10 +291,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 10, 11, 12, 13, 14, 15]\n" + ] + } + ], + "source": [ + "hours_with_temp_more_than_70 = []\n", + "\n", + "for x in (range(len(temperatures_C))):\n", + " if temperatures_C[x] > 70:\n", + " hours_with_temp_more_than_70.append(x)\n", + " \n", + "print(hours_with_temp_more_than_70)\n", + "\n" + ] }, { "cell_type": "markdown", @@ -189,10 +322,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "7\n", + "[7]\n" + ] + } + ], + "source": [ + "consecutive_hours = []\n", + "counter = 0\n", + "\n", + "for x in range(1,len(hours_with_temp_more_than_70)):\n", + " if x == len(hours_with_temp_more_than_70)-1 and counter > 0:\n", + " counter += 2\n", + " consecutive_hours.append(counter)\n", + " elif hours_with_temp_more_than_70[x-1]+1 == hours_with_temp_more_than_70[x]:\n", + " counter += 1\n", + " else:\n", + " counter += 1\n", + " consecutive_hours.append(counter)\n", + " counter = 0\n", + "\n", + "print(counter)\n", + "print(consecutive_hours)" + ] }, { "cell_type": "markdown", @@ -204,10 +363,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 83, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "4\n", + "We need to replace the cooling system\n" + ] + } + ], + "source": [ + "new_test_count = 0\n", + "\n", + "for x in range(len(temperatures_C)):\n", + " if temperatures_C[x] > 80:\n", + " new_test_count += 1\n", + "print(new_test_count)\n", + " \n", + "if sum(temperatures_C)/len(temperatures_C) > 65:\n", + " new_test_count += 1\n", + " \n", + "for x in range(len(consecutive_hours)):\n", + " if consecutive_hours[x] > 4:\n", + " new_test_count += 1\n", + " \n", + "print(new_test_count)\n", + "\n", + "\n", + "if new_test_count > 0:\n", + " print(\"We need to replace the cooling system\")\n", + "else:\n", + " print(\"We can keep the current cooling system\")" + ] }, { "cell_type": "markdown", @@ -218,10 +409,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 90, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average value temp in C: 60.25 \n", + "Average value temp in F: 140.45\n", + "Relation between both average values is: 'Average value in F = 1.8∗Average value in F+32'\n" + ] + } + ], + "source": [ + "avg_value_temperatures_F = sum(temperatures_F)/len(temperatures_F)\n", + "avg_value_temperatures_C = sum(temperatures_C)/len(temperatures_C)\n", + "\n", + "print(\"Average value temp in C:\", avg_value_temperatures_C, \"\\nAverage value temp in F:\", avg_value_temperatures_F)\n", + "\n", + "celsius_to_fahrenheit(avg_value_temperatures_C)\n", + "\n", + "print(\"Relation between both average values is: 'Average value in F = 1.8∗Average value in F+32'\")" + ] }, { "cell_type": "markdown", @@ -232,10 +442,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation temperatures in F = 170.06428196420316 \n", + "Standard deviation temperatures in C = 94.48015664677953\n", + "Relation between both standard deviations is a factor of 1.8\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def std_dev(average, list):\n", + " deviation_calculation = 0\n", + " deviation_sum = 0\n", + " for x in range(len(list)):\n", + " deviation_calculation = (list[x]-average)**2\n", + " deviation_sum = deviation_sum + deviation_calculation\n", + " return math.sqrt(deviation_sum)\n", + "\n", + "std_dev_temps_in_F = std_dev(avg_value_temperatures_F, temperatures_F)\n", + "\n", + "std_dev_temps_in_C = std_dev(avg_value_temperatures_C, temperatures_C)\n", + "\n", + "print(\"Standard deviation temperatures in F =\", std_dev_temps_in_F, \"\\nStandard deviation temperatures in C =\", std_dev_temps_in_C)\n", + "\n", + "print(\"Relation between both standard deviations is a factor of\", std_dev_temps_in_F/std_dev_temps_in_C)" + ] } ], "metadata": { @@ -254,7 +492,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "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..83e20ad5e --- /dev/null +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/.ipynb_checkpoints/rock-paper-scissors-checkpoint.ipynb" @@ -0,0 +1,525 @@ +{ + "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": 194, + "metadata": {}, + "outputs": [], + "source": [ + "import random" + ] + }, + { + "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": 195, + "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": 196, + "metadata": {}, + "outputs": [], + "source": [ + "n_rounds = int" + ] + }, + { + "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": 197, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rounds_to_win 1\n" + ] + } + ], + "source": [ + "n_rounds = int()\n", + "rounds_to_win = n_rounds//2+1\n", + "\n", + "print(\"rounds_to_win\", rounds_to_win)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." + ] + }, + { + "cell_type": "code", + "execution_count": 198, + "metadata": {}, + "outputs": [], + "source": [ + "cpu_score = int\n", + "player_score = int" + ] + }, + { + "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": 199, + "metadata": {}, + "outputs": [], + "source": [ + "def cpu_gesture():\n", + " return random.choice(gestures)" + ] + }, + { + "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": 200, + "metadata": {}, + "outputs": [], + "source": [ + "def player_gesture():\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors'\")\n", + " while player_choice not in gestures:\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors'\")\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": 201, + "metadata": {}, + "outputs": [], + "source": [ + "def who_wins():\n", + " if computer_move == 'rock' and player_move == 'rock':\n", + " return 0\n", + " elif computer_move == 'rock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'rock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'paper':\n", + " return 0\n", + " elif computer_move == 'paper' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'scissors':\n", + " return 0\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": 202, + "metadata": {}, + "outputs": [], + "source": [ + "def result():\n", + " global cpu_score\n", + " global player_score\n", + " global rounds_played\n", + " print(\"Round number:\", rounds_played,\n", + " \"\\nComputer plays:\", computer_move,\n", + " \"\\nPlayer plays: \", player_move \n", + " )\n", + " if round_score == 0:\n", + " print(\"It's a tie!\")\n", + " elif round_score == 1:\n", + " print(\"Computer wins!\")\n", + " cpu_score += 1\n", + " else:\n", + " print(\"Player wins!\")\n", + " player_score += 1\n", + " rounds_played += 1\n", + " print(\"Player score:\", player_score,\n", + " \"Computer score\", cpu_score\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": 203, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose your gesture amongst: 'rock', 'paper', 'scissors'rock\n", + "Round number: 1 \n", + "Computer plays: scissors \n", + "Player plays: rock\n", + "Player wins!\n", + "Player score: 1 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 2 \n", + "Computer plays: rock \n", + "Player plays: paper\n", + "Player wins!\n", + "Player score: 2 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'island\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'scissors\n", + "Round number: 3 \n", + "Computer plays: rock \n", + "Player plays: scissors\n", + "Computer wins!\n", + "Player score: 2 Computer score 1\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 4 \n", + "Computer plays: paper \n", + "Player plays: paper\n", + "It's a tie!\n", + "Player score: 2 Computer score 1\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 5 \n", + "Computer plays: scissors \n", + "Player plays: paper\n", + "Computer wins!\n", + "Player score: 2 Computer score 2\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = ['rock', 'paper', 'scissors']\n", + "n_rounds = 5\n", + "rounds_to_win = n_rounds//2+1\n", + "cpu_score = 0\n", + "player_score = 0\n", + "rounds_played = 1\n", + "\n", + "while cpu_score <= rounds_to_win and player_score <= rounds_to_win and rounds_played <= n_rounds:\n", + " player_move = player_gesture()\n", + " computer_move = cpu_gesture()\n", + " round_score = who_wins()\n", + " result()" + ] + }, + { + "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": 204, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No winner. It's a tie!\n" + ] + } + ], + "source": [ + "if cpu_score == rounds_to_win or cpu_score > player_score:\n", + " print(\"The machine has won!\")\n", + "elif player_score == rounds_to_win or player_score > cpu_score:\n", + " print(\"The human has won!\")\n", + "else:\n", + " print(\"No winner. It's 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": 226, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of rounds to play (number must be odd):3\n", + "You chose to play 3 rounds. Prepare to have fun!\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'lizard\n", + "Round number: 1 \n", + "Computer plays: paper \n", + "Player plays: lizard\n", + "Player wins!\n", + "Player score: 1 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'spock\n", + "Round number: 2 \n", + "Computer plays: scissors \n", + "Player plays: spock\n", + "Player wins!\n", + "Player score: 2 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'island\n", + "Choose your gesture amongst: rock', 'paper', 'scissors', 'lizard' and 'spock'lizard\n", + "Round number: 3 \n", + "Computer plays: rock \n", + "Player plays: lizard\n", + "Computer wins!\n", + "Player score: 2 Computer score 1\n", + "The human has won!\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = ['rock', 'paper', 'scissors', 'lizard', 'spock']\n", + "\n", + "def rounds_request():\n", + " rounds_choice = 2\n", + " while int(rounds_choice) % 2 == 0:\n", + " rounds_choice = input(\"Enter the number of rounds to play (number must be odd):\")\n", + " return rounds_choice\n", + "\n", + "n_rounds = rounds_request()\n", + "\n", + "print(\"You chose to play\", n_rounds, \"rounds. Prepare to have fun!\")\n", + "\n", + "rounds_to_win = int(int(n_rounds)//2+1)\n", + "\n", + "def cpu_gesture():\n", + " return random.choice(gestures)\n", + "\n", + "def player_gesture():\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'\")\n", + " while player_choice not in gestures:\n", + " player_choice = input(\"Choose your gesture amongst: rock', 'paper', 'scissors', 'lizard' and 'spock'\")\n", + " return player_choice\n", + "\n", + "#0 tie\n", + "#1 computer wins\n", + "#2 player wins\n", + "\n", + "def who_wins():\n", + " if computer_move == 'rock' and player_move == 'rock':\n", + " return 0\n", + " elif computer_move == 'rock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'rock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'rock' and player_move == 'lizard':\n", + " return 1\n", + " elif computer_move == 'rock' and player_move == 'spock':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'paper':\n", + " return 0\n", + " elif computer_move == 'paper' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'lizard':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'spock':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'scissors':\n", + " return 0\n", + " elif computer_move == 'scissors' and player_move == 'lizard':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'spock':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'lizard':\n", + " return 0\n", + " elif computer_move == 'lizard' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'lizard' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'spock':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'spock':\n", + " return 0\n", + " elif computer_move == 'spock' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'spock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'lizard':\n", + " return 2\n", + " \n", + "def result():\n", + " global cpu_score\n", + " global player_score\n", + " global rounds_played\n", + " print(\"Round number:\", rounds_played,\n", + " \"\\nComputer plays:\", computer_move,\n", + " \"\\nPlayer plays: \", player_move \n", + " )\n", + " if round_score == 0:\n", + " print(\"It's a tie!\")\n", + " elif round_score == 1:\n", + " print(\"Computer wins!\")\n", + " cpu_score += 1\n", + " else:\n", + " print(\"Player wins!\")\n", + " player_score += 1\n", + " rounds_played += 1\n", + " print(\"Player score:\", player_score,\n", + " \"Computer score\", cpu_score\n", + " )\n", + "\n", + "\n", + "cpu_score = int(0)\n", + "player_score = int(0)\n", + "rounds_played = int(1)\n", + "\n", + "while cpu_score <= int(rounds_to_win) and player_score <= rounds_to_win and rounds_played <= int(n_rounds):\n", + " player_move = player_gesture()\n", + " computer_move = cpu_gesture()\n", + " round_score = who_wins()\n", + " result()\n", + " \n", + " \n", + "if cpu_score == rounds_to_win or cpu_score > player_score:\n", + " print(\"The machine has won!\")\n", + "elif player_score == rounds_to_win or player_score > cpu_score:\n", + " print(\"The human has won!\")\n", + "else:\n", + " print(\"No winner. It's a tie!\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.5" + } + }, + "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..83e20ad5e 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": 194, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import random" + ] }, { "cell_type": "markdown", @@ -46,10 +48,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 195, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gestures = ['rock', 'paper', 'scissors']" + ] }, { "cell_type": "markdown", @@ -61,10 +65,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 196, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n_rounds = int" + ] }, { "cell_type": "markdown", @@ -76,10 +82,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 197, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "rounds_to_win 1\n" + ] + } + ], + "source": [ + "n_rounds = int()\n", + "rounds_to_win = n_rounds//2+1\n", + "\n", + "print(\"rounds_to_win\", rounds_to_win)" + ] }, { "cell_type": "markdown", @@ -90,10 +109,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 198, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "cpu_score = int\n", + "player_score = int" + ] }, { "cell_type": "markdown", @@ -105,10 +127,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 199, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def cpu_gesture():\n", + " return random.choice(gestures)" + ] }, { "cell_type": "markdown", @@ -120,10 +145,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 200, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def player_gesture():\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors'\")\n", + " while player_choice not in gestures:\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors'\")\n", + " return player_choice" + ] }, { "cell_type": "markdown", @@ -135,10 +166,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 201, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def who_wins():\n", + " if computer_move == 'rock' and player_move == 'rock':\n", + " return 0\n", + " elif computer_move == 'rock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'rock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'paper':\n", + " return 0\n", + " elif computer_move == 'paper' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'scissors':\n", + " return 0\n" + ] }, { "cell_type": "markdown", @@ -150,10 +201,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 202, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def result():\n", + " global cpu_score\n", + " global player_score\n", + " global rounds_played\n", + " print(\"Round number:\", rounds_played,\n", + " \"\\nComputer plays:\", computer_move,\n", + " \"\\nPlayer plays: \", player_move \n", + " )\n", + " if round_score == 0:\n", + " print(\"It's a tie!\")\n", + " elif round_score == 1:\n", + " print(\"Computer wins!\")\n", + " cpu_score += 1\n", + " else:\n", + " print(\"Player wins!\")\n", + " player_score += 1\n", + " rounds_played += 1\n", + " print(\"Player score:\", player_score,\n", + " \"Computer score\", cpu_score\n", + " )\n", + " " + ] }, { "cell_type": "markdown", @@ -168,10 +241,63 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 203, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose your gesture amongst: 'rock', 'paper', 'scissors'rock\n", + "Round number: 1 \n", + "Computer plays: scissors \n", + "Player plays: rock\n", + "Player wins!\n", + "Player score: 1 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 2 \n", + "Computer plays: rock \n", + "Player plays: paper\n", + "Player wins!\n", + "Player score: 2 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'island\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'scissors\n", + "Round number: 3 \n", + "Computer plays: rock \n", + "Player plays: scissors\n", + "Computer wins!\n", + "Player score: 2 Computer score 1\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 4 \n", + "Computer plays: paper \n", + "Player plays: paper\n", + "It's a tie!\n", + "Player score: 2 Computer score 1\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors'paper\n", + "Round number: 5 \n", + "Computer plays: scissors \n", + "Player plays: paper\n", + "Computer wins!\n", + "Player score: 2 Computer score 2\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = ['rock', 'paper', 'scissors']\n", + "n_rounds = 5\n", + "rounds_to_win = n_rounds//2+1\n", + "cpu_score = 0\n", + "player_score = 0\n", + "rounds_played = 1\n", + "\n", + "while cpu_score <= rounds_to_win and player_score <= rounds_to_win and rounds_played <= n_rounds:\n", + " player_move = player_gesture()\n", + " computer_move = cpu_gesture()\n", + " round_score = who_wins()\n", + " result()" + ] }, { "cell_type": "markdown", @@ -183,10 +309,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 204, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No winner. It's a tie!\n" + ] + } + ], + "source": [ + "if cpu_score == rounds_to_win or cpu_score > player_score:\n", + " print(\"The machine has won!\")\n", + "elif player_score == rounds_to_win or player_score > cpu_score:\n", + " print(\"The human has won!\")\n", + "else:\n", + " print(\"No winner. It's a tie!\")" + ] }, { "cell_type": "markdown", @@ -204,10 +345,160 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 226, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of rounds to play (number must be odd):3\n", + "You chose to play 3 rounds. Prepare to have fun!\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'lizard\n", + "Round number: 1 \n", + "Computer plays: paper \n", + "Player plays: lizard\n", + "Player wins!\n", + "Player score: 1 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'spock\n", + "Round number: 2 \n", + "Computer plays: scissors \n", + "Player plays: spock\n", + "Player wins!\n", + "Player score: 2 Computer score 0\n", + "Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'island\n", + "Choose your gesture amongst: rock', 'paper', 'scissors', 'lizard' and 'spock'lizard\n", + "Round number: 3 \n", + "Computer plays: rock \n", + "Player plays: lizard\n", + "Computer wins!\n", + "Player score: 2 Computer score 1\n", + "The human has won!\n" + ] + } + ], + "source": [ + "import random\n", + "\n", + "gestures = ['rock', 'paper', 'scissors', 'lizard', 'spock']\n", + "\n", + "def rounds_request():\n", + " rounds_choice = 2\n", + " while int(rounds_choice) % 2 == 0:\n", + " rounds_choice = input(\"Enter the number of rounds to play (number must be odd):\")\n", + " return rounds_choice\n", + "\n", + "n_rounds = rounds_request()\n", + "\n", + "print(\"You chose to play\", n_rounds, \"rounds. Prepare to have fun!\")\n", + "\n", + "rounds_to_win = int(int(n_rounds)//2+1)\n", + "\n", + "def cpu_gesture():\n", + " return random.choice(gestures)\n", + "\n", + "def player_gesture():\n", + " player_choice = input(\"Choose your gesture amongst: 'rock', 'paper', 'scissors', 'lizard' and 'spock'\")\n", + " while player_choice not in gestures:\n", + " player_choice = input(\"Choose your gesture amongst: rock', 'paper', 'scissors', 'lizard' and 'spock'\")\n", + " return player_choice\n", + "\n", + "#0 tie\n", + "#1 computer wins\n", + "#2 player wins\n", + "\n", + "def who_wins():\n", + " if computer_move == 'rock' and player_move == 'rock':\n", + " return 0\n", + " elif computer_move == 'rock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'rock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'rock' and player_move == 'lizard':\n", + " return 1\n", + " elif computer_move == 'rock' and player_move == 'spock':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'paper' and player_move == 'paper':\n", + " return 0\n", + " elif computer_move == 'paper' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'lizard':\n", + " return 2\n", + " elif computer_move == 'paper' and player_move == 'spock':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'scissors' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'scissors':\n", + " return 0\n", + " elif computer_move == 'scissors' and player_move == 'lizard':\n", + " return 1\n", + " elif computer_move == 'scissors' and player_move == 'spock':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'lizard':\n", + " return 0\n", + " elif computer_move == 'lizard' and player_move == 'rock':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'paper':\n", + " return 1\n", + " elif computer_move == 'lizard' and player_move == 'scissors':\n", + " return 2\n", + " elif computer_move == 'lizard' and player_move == 'spock':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'spock':\n", + " return 0\n", + " elif computer_move == 'spock' and player_move == 'rock':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'paper':\n", + " return 2\n", + " elif computer_move == 'spock' and player_move == 'scissors':\n", + " return 1\n", + " elif computer_move == 'spock' and player_move == 'lizard':\n", + " return 2\n", + " \n", + "def result():\n", + " global cpu_score\n", + " global player_score\n", + " global rounds_played\n", + " print(\"Round number:\", rounds_played,\n", + " \"\\nComputer plays:\", computer_move,\n", + " \"\\nPlayer plays: \", player_move \n", + " )\n", + " if round_score == 0:\n", + " print(\"It's a tie!\")\n", + " elif round_score == 1:\n", + " print(\"Computer wins!\")\n", + " cpu_score += 1\n", + " else:\n", + " print(\"Player wins!\")\n", + " player_score += 1\n", + " rounds_played += 1\n", + " print(\"Player score:\", player_score,\n", + " \"Computer score\", cpu_score\n", + " )\n", + "\n", + "\n", + "cpu_score = int(0)\n", + "player_score = int(0)\n", + "rounds_played = int(1)\n", + "\n", + "while cpu_score <= int(rounds_to_win) and player_score <= rounds_to_win and rounds_played <= int(n_rounds):\n", + " player_move = player_gesture()\n", + " computer_move = cpu_gesture()\n", + " round_score = who_wins()\n", + " result()\n", + " \n", + " \n", + "if cpu_score == rounds_to_win or cpu_score > player_score:\n", + " print(\"The machine has won!\")\n", + "elif player_score == rounds_to_win or player_score > cpu_score:\n", + " print(\"The human has won!\")\n", + "else:\n", + " print(\"No winner. It's a tie!\")" + ] } ], "metadata": { @@ -226,7 +517,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/2.-Statistics/Bayes Theorem and the Binomial Theorem.png b/2.-Statistics/Bayes Theorem and the Binomial Theorem.png new file mode 100644 index 000000000..e585cb3be Binary files /dev/null and b/2.-Statistics/Bayes Theorem and the Binomial Theorem.png differ diff --git a/2.-Statistics/Cartesian Plane and Types of Function.png b/2.-Statistics/Cartesian Plane and Types of Function.png new file mode 100644 index 000000000..cf75fcd70 Binary files /dev/null and b/2.-Statistics/Cartesian Plane and Types of Function.png differ diff --git a/2.-Statistics/Exponents and Logarithms.png b/2.-Statistics/Exponents and Logarithms.png new file mode 100644 index 000000000..1a96a1382 Binary files /dev/null and b/2.-Statistics/Exponents and Logarithms.png differ diff --git a/2.-Statistics/Functions, Exponents and Logarithms.png b/2.-Statistics/Functions, Exponents and Logarithms.png new file mode 100644 index 000000000..21cd7e5a7 Binary files /dev/null and b/2.-Statistics/Functions, Exponents and Logarithms.png differ diff --git a/2.-Statistics/NumberLine,_including Inequalities.png b/2.-Statistics/NumberLine,_including Inequalities.png new file mode 100644 index 000000000..041b80764 Binary files /dev/null and b/2.-Statistics/NumberLine,_including Inequalities.png differ diff --git a/2.-Statistics/Practice quiz on Sets.png b/2.-Statistics/Practice quiz on Sets.png new file mode 100644 index 000000000..28961f7cb Binary files /dev/null and b/2.-Statistics/Practice quiz on Sets.png differ diff --git a/2.-Statistics/Probability (basic and Intermediate) Graded Quiz.png b/2.-Statistics/Probability (basic and Intermediate) Graded Quiz.png new file mode 100644 index 000000000..9362ba147 Binary files /dev/null and b/2.-Statistics/Probability (basic and Intermediate) Graded Quiz.png differ diff --git a/2.-Statistics/Probability Concepts.png b/2.-Statistics/Probability Concepts.png new file mode 100644 index 000000000..3c28651f7 Binary files /dev/null and b/2.-Statistics/Probability Concepts.png differ diff --git a/2.-Statistics/Problem Solving.png b/2.-Statistics/Problem Solving.png new file mode 100644 index 000000000..6ceb1aa1c Binary files /dev/null and b/2.-Statistics/Problem Solving.png differ diff --git a/2.-Statistics/Sets, Number Line, Inequalities, Simplification, and Sigma Notation.png b/2.-Statistics/Sets, Number Line, Inequalities, Simplification, and Sigma Notation.png new file mode 100644 index 000000000..c8341e148 Binary files /dev/null and b/2.-Statistics/Sets, Number Line, Inequalities, Simplification, and Sigma Notation.png differ diff --git a/2.-Statistics/Simplification Rules and Sigma Notation.png b/2.-Statistics/Simplification Rules and Sigma Notation.png new file mode 100644 index 000000000..b656bbac3 Binary files /dev/null and b/2.-Statistics/Simplification Rules and Sigma Notation.png differ diff --git a/2.-Statistics/Tangent Lines to Functions.png b/2.-Statistics/Tangent Lines to Functions.png new file mode 100644 index 000000000..366742593 Binary files /dev/null and b/2.-Statistics/Tangent Lines to Functions.png differ diff --git a/2.-Statistics/Types of Functions.png b/2.-Statistics/Types of Functions.png new file mode 100644 index 000000000..55764d38a Binary files /dev/null and b/2.-Statistics/Types of Functions.png differ diff --git a/2.-Statistics/the Cartesian Plane.png b/2.-Statistics/the Cartesian Plane.png new file mode 100644 index 000000000..f3659222f Binary files /dev/null and b/2.-Statistics/the Cartesian Plane.png differ diff --git a/robot.md b/robot.md new file mode 100644 index 000000000..e69de29bb