diff --git a/1.-Python/1.-Snail-and-Well/snail-and-well-solution-TG.ipynb b/1.-Python/1.-Snail-and-Well/snail-and-well-solution-TG.ipynb new file mode 100644 index 0000000..350354f --- /dev/null +++ b/1.-Python/1.-Snail-and-Well/snail-and-well-solution-TG.ipynb @@ -0,0 +1,263 @@ +{ + "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": 15, + "metadata": {}, + "outputs": [], + "source": [ + "well_height=125\n", + "daily_distance=30\n", + "nightly_distance=20\n", + "snail_position=0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create a variable `days` to keep count of the days that pass until the snail escapes the well. " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "days=0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the solution to the challenge using the variables defined above. " + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "while snail_position < well_height:\n", + " days += 1\n", + " snail_position += daily_distance\n", + " if snail_position >= well_height:\n", + " break\n", + " else: snail_position = snail_position - nightly_distance" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Print the solution." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It takes 11 days\n" + ] + } + ], + "source": [ + "print(\"It takes\",days,\"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": 19, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It takes 5 days\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "nigthly_distance=20\n", + "snail_position=0\n", + "days=0\n", + "i=0\n", + "while snail_position < well_height:\n", + " days += 1\n", + " \n", + " snail_position += advance_cm[i]\n", + " if snail_position >= well_height:\n", + " break\n", + " else: snail_position = snail_position - nightly_distance \n", + " i+=1\n", + " \n", + "print(\"It takes\",days,\"days\")\n", + "\n" + ] + }, + { + "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": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Maximun displacement is: 57\n", + "Minimum discplacement is: 1\n" + ] + } + ], + "source": [ + "# Max:\n", + "print(\"Maximun displacement is:\",max(advance_cm[0:6])-20)\n", + "\n", + "# Min:\n", + "print(\"Minimum discplacement is:\",min(advance_cm[0:6])-20)\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": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average progress: 18.09090909090909\n" + ] + } + ], + "source": [ + "mean=(sum(advance_cm)-nightly_distance*len(advance_cm))/len(advance_cm)\n", + "print(\"Average progress:\",mean)" + ] + }, + { + "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": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 1, 13, 57, 24, 25, 3, 25, -8, 14, 35]\n", + "Standard deviation: 17.159437082600803\n" + ] + } + ], + "source": [ + "displacement=[]\n", + "range1=range(len(advance_cm))\n", + "for i in range1:\n", + " displacement.append(advance_cm[i]-20)\n", + "print(displacement) \n", + "import statistics\n", + "print(\"Standard deviation:\",statistics.pstdev(displacement)) #To calculate standard deviation of all data points as we don't have a sample" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.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 3402144..15829a3 100644 --- a/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb +++ b/1.-Python/1.-Snail-and-Well/snail-and-well.ipynb @@ -161,7 +161,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.5" } }, "nbformat": 4, diff --git a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers-solution-TG.ipynb b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers-solution-TG.ipynb new file mode 100644 index 0000000..e93af9e --- /dev/null +++ b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers-solution-TG.ipynb @@ -0,0 +1,352 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Duel of Sorcerers\n", + "You are witnessing an epic battle between two powerful sorcerers: Gandalf and Saruman. Each sorcerer has 10 spells of variable power in their mind and they are going to throw them one after the other. The winner of the duel will be the one who wins more of those clashes between spells. Spells are represented as a list of 10 integers whose value equals the power of the spell.\n", + "```\n", + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "```\n", + "For example:\n", + "- The first clash is won by Saruman: 10 against 23.\n", + "- The second clash is won by Saruman: 11 against 66.\n", + "- ...\n", + "\n", + "You will create two variables, one for each sorcerer, where the sum of clashes won will be stored. Depending on which variable is greater at the end of the duel, you will show one of the following three results on the screen:\n", + "* Gandalf wins\n", + "* Saruman wins\n", + "* Tie\n", + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "\n", + "1. Data structures: **lists, dictionaries**\n", + "2. Loop: **for loop**\n", + "3. Conditional statements: **if-elif-else**\n", + "4. Functions: **range(), len(), print()**\n", + "\n", + "## Tasks\n", + "\n", + "#### 1. Create two variables called `gandalf` and `saruman` and assign them the spell power lists. Create a variable called `spells` to store the number of spells that the sorcerers cast. " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "spells=range(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. \n", + "You will use these variables to count the number of clashes each sorcerer wins. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_wins, saruman_wins = 0,0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Using the lists of spells of both sorcerers, update variables `gandalf_wins` and `saruman_wins` to count the number of times each sorcerer wins a clash. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins: 6\n", + "Saruman wins: 4\n" + ] + } + ], + "source": [ + "gandalf_wins, saruman_wins = 0,0\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", + "spells=range(10)\n", + "for i in spells:\n", + " if gandalf[i]>saruman[i]:\n", + " gandalf_wins += 1\n", + " elif saruman[i]>gandalf[i]:\n", + " saruman_wins+=1\n", + "print(\"Gandalf wins:\",gandalf_wins) \n", + "print(\"Saruman wins:\",saruman_wins)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Who won the battle?\n", + "Print `Gandalf wins`, `Saruman wins` or `Tie` depending on the result. " + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], + "source": [ + "if gandalf_wins > saruman_wins:\n", + " print(\"Gandalf wins\")\n", + "elif gandalf_wins > saruman_wins==False:\n", + " print(\"Saruman wins\")\n", + "elif gandals_wins == saruman_wins:\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": 5, + "metadata": {}, + "outputs": [], + "source": [ + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "spells=range(10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create two variables called `gandalf_wins` and `saruman_wins`. Set both of them to 0. " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_wins, saruman_wins = 0,0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Create two variables called `gandalf_power` and `saruman_power` to store the list of spell powers of each sorcerer." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "gandalf_power,saruman_power=[],[]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. The battle starts! Using the variables you've created above, code the execution of spell clashes. Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row. \n", + "If a clash ends up in a tie, the counter of wins in a row is not restarted to 0. Remember to print who is the winner of the battle. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf Power: [50, 40, 40, 10, 50, 10, 40, 50, 50, 50]\n", + "Saruman Power: [45, 45, 25, 50, 25, 40, 10, 45, 10, 10]\n", + "The winner is Gandalf\n" + ] + } + ], + "source": [ + "gandalf_wins, saruman_wins = 0,0\n", + "gandalf_power,saruman_power=[],[]\n", + "spells=range(10)\n", + " \n", + "for power in gandalf:\n", + " if power in POWER:\n", + " gandalf_power.append(POWER[power])\n", + " \n", + "for power in saruman:\n", + " if power in POWER:\n", + " saruman_power.append(POWER[power])\n", + "print(\"Gandalf Power:\",gandalf_power) \n", + "print(\"Saruman Power:\",saruman_power)\n", + "\n", + "for i in spells: #ties ignored in this formula as there are none\n", + " if gandalf_power[i]>saruman_power[i] and gandalf_power[i+1]>saruman_power[i+1] and gandalf_power[i+2]>saruman_power[i+2]:\n", + " gandalf_wins += 1\n", + " elif gandalf_power[i]0 or saruman_wins > 0:\n", + " break \n", + "\n", + "if gandalf_wins>saruman_wins:\n", + " print(\"The winner is Gandalf\")\n", + "elif gandalf_wins" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bus\n", + "\n", + "This bus has a passenger entry and exit control system to monitor the number of occupants it carries and thus detect when there are too many.\n", + "\n", + "At each stop, the entry and exit of passengers is represented by a tuple consisting of two integer numbers.\n", + "```\n", + "bus_stop = (in, out)\n", + "```\n", + "The succession of stops is represented by a list of these tuples.\n", + "```\n", + "stops = [(in1, out1), (in2, out2), (in3, out3), (in4, out4)]\n", + "```\n", + "\n", + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "* Data structures: **lists, tuples**\n", + "* Loop: **while/for loops**\n", + "* Functions: **min, max, len**\n", + "\n", + "## Tasks" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Variables\n", + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8), (4, 6), (2, 3)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1. Calculate the number of stops." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of stops: 9\n" + ] + } + ], + "source": [ + "print(\"Number of stops:\",len(stops))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Assign to a variable a list whose elements are the number of passengers at each stop (in-out).\n", + "Each item depends on the previous item in the list + in - out." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of passengers at each stop: [10, 13, 11, 10, 14, 10, 7, 5, 4]\n" + ] + } + ], + "source": [ + "passengers=[]\n", + "passengers_count=0\n", + "for on,out in stops:\n", + " passengers_count = passengers_count + on - out\n", + " passengers.append(passengers_count)\n", + " \n", + "print(\"Number of passengers at each stop:\",passengers)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 3. Find the maximum occupation of the bus." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Maximum occupation of the bus: 14\n" + ] + } + ], + "source": [ + "print(\"Maximum occupation of the bus:\",max(passengers))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 4. Calculate the average occupation. And the standard deviation." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average occupation: 1.5555555555555556\n", + "Standard deviation: 3.197221015541813\n" + ] + } + ], + "source": [ + "#Mean:\n", + "print(\"Average occupation:\",max(passengers)/len(passengers))\n", + "\n", + "#Standard Deviation:\n", + "import statistics\n", + "print(\"Standard deviation:\",statistics.pstdev(passengers))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8..31bb63c 100755 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -117,7 +117,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.5" } }, "nbformat": 4, diff --git a/1.-Python/4.-Robin-Hood/robin-hood-Solution-TG.ipynb b/1.-Python/4.-Robin-Hood/robin-hood-Solution-TG.ipynb new file mode 100644 index 0000000..d87ca22 --- /dev/null +++ b/1.-Python/4.-Robin-Hood/robin-hood-Solution-TG.ipynb @@ -0,0 +1,224 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Robin Hood\n", + "Robin Hood has entered a competition to win the archery contest in Sherwood. With his bow and arrows, he needs to shoot on a target and try to hit as close as possible to the center.\n", + "\n", + "![](images/arrows.jpg)\n", + "\n", + "## Context\n", + "In this challenge, the landing position of arrows shot by archers in the competition will be represented using 2-dimensional coordinates. \n", + "\n", + "In the 2-dimensional space, a point can be defined by a pair of values that correspond to the horizontal coordinate (x) and the vertical coordinate (y). For example, in our case, an arrow that hits the center of the archery target will land in position (0, 0) on the coordinate axes. \n", + "\n", + "The space can be divided into 4 zones (quadrants): Q1, Q2, Q3, Q4. If a point is in Q1, both its x coordinate and y coordinate are positive. Any point with a null x or y coordinate is considered to not belong to any quadrant. \n", + "\n", + "If you want to know more about the cartesian coordinate system, you can check this [link](https://en.wikipedia.org/wiki/Cartesian_coordinate_system). \n", + "\n", + "## Tools\n", + "You don't necessarily need to use all the tools. Maybe you opt to use some of them or completely different ones, they are given to help you shape the exercise. Programming exercises can be solved in many different ways.\n", + "* Data structures: **lists, sets, tuples**\n", + "* Conditional statements: **if-elif-else**\n", + "* Loop: **while/for**\n", + "* Minimum (optional sorting)\n", + "\n", + "## Tasks\n", + "Robin Hood has hit the following points:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5), (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),\n", + " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 1. Robin Hood is famous for hitting an arrow with another arrow. Find the coordinates of the points where an arrow hits another arrow." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Coordinates of the points where an arrow hits another arrow: [(4, 5), (5, 7), (2, 2), (-3, 2)]\n" + ] + } + ], + "source": [ + "arrow_hits_arrow=[]\n", + "for coordinate in points:\n", + " if arrow_hits_arrow.count(coordinate)==1:\n", + " continue\n", + " if points.count(coordinate) > 1:\n", + " arrow_hits_arrow.append(coordinate)\n", + " \n", + "print(\"Coordinates of the points where an arrow hits another arrow:\",arrow_hits_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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Arrows in Q1: 10\n", + "Arrows in Q2: 6\n", + "Arrows in Q3: 2\n", + "Arrows in Q4: 2\n" + ] + } + ], + "source": [ + "Q1=0\n", + "Q2=0\n", + "Q3=0\n", + "Q4=0\n", + "for x,y in points:\n", + " if x>0 and y>0:\n", + " Q1+=1\n", + " elif x<0 and y>0:\n", + " Q2+=1\n", + " elif x<0 and y<0:\n", + " Q3+=1\n", + " elif x>0 and y<0:\n", + " Q4+=1\n", + "print(\"Arrows in Q1:\",Q1)\n", + "print(\"Arrows in Q2:\",Q2)\n", + "print(\"Arrows in Q3:\",Q3)\n", + "print(\"Arrows in Q4:\",Q4)" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Minimum distance to the center: 2.0\n", + "\n", + "Point(s) with closest distance to center: [(0, 2), (0, -2)]\n" + ] + } + ], + "source": [ + "closest_point=[]\n", + "distance_center=[]\n", + "for x in range(len(points)):\n", + " distance_center.append((points[x][0]**2.0+points[x][1]**2.0)**(0.5))\n", + "\n", + "print(\"Minimum distance to the center:\",min(distance_center))\n", + "print(\"\")\n", + "\n", + "for x in range(len(points)):\n", + " if ((points[x][0]**2.0+points[x][1]**2.0)**(0.5)) == 2:\n", + " closest_point.append(points[x])\n", + " \n", + "print(\"Point(s) with closest distance to center:\",closest_point)\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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of arrows that won't hit the target: 2\n", + "\n", + "Coordinates of arrows that won't hit hte target: [(9, 9), (-8, -9)]\n" + ] + } + ], + "source": [ + "target_not_hit=0\n", + "coordi_not_hit=[]\n", + "\n", + "for x in range(len(points)):\n", + " if ((points[x][0]**2.0+points[x][1]**2.0)**(0.5))>9:\n", + " target_not_hit+=1\n", + " coordi_not_hit.append(points[x])\n", + "print(\"Number of arrows that won't hit the target:\",target_not_hit)\n", + "print(\"\")\n", + "print(\"Coordinates of arrows that won't hit hte target:\",coordi_not_hit)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.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 01de29d..fc325aa 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -125,7 +125,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.5" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/temperature-Solution-TG.ipynb b/1.-Python/5.-Temperature-Processor/temperature-Solution-TG.ipynb new file mode 100644 index 0000000..239800a --- /dev/null +++ b/1.-Python/5.-Temperature-Processor/temperature-Solution-TG.ipynb @@ -0,0 +1,459 @@ +{ + "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": 1, + "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": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Minimum temperature of the day is: 0\n" + ] + } + ], + "source": [ + "min_temp=min(temperatures_C)\n", + "print(\"Minimum temperature of the day is:\",min_temp)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Find the maximum temperature of the day and store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Maximum temperature of the day is: 90\n" + ] + } + ], + "source": [ + "max_temp=max(temperatures_C)\n", + "print(\"Maximum temperature of the day is:\",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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Temperatures >= 70C: [70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "temp_greater_equal_70=[]\n", + "for temp in temperatures_C:\n", + " if temp >= 70:\n", + " temp_greater_equal_70.append(temp)\n", + "print(\"Temperatures >= 70C:\",temp_greater_equal_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": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average temperature of the day is: 60.25\n" + ] + } + ], + "source": [ + "avg_temp=sum(temperatures_C)/len(temperatures_C)\n", + "print(\"Average temperature of the day is:\",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": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Estimating missing value by calculating mean of i+1 and i-1: 62\n", + "List of temperatures updated with estimated value: [33, 66, 65, 62, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n" + ] + } + ], + "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]\n", + "\n", + "temp_est=int((temperatures_C[2]+temperatures_C[4])/2)\n", + "print(\"Estimating missing value by calculating mean of i+1 and i-1:\",temp_est)\n", + "\n", + "for i in range(3,4):\n", + " if temperatures_C[3]==0:\n", + " temperatures_C[3]=temp_est\n", + "print(\"List of temperatures updated with estimated value:\",temperatures_C)" + ] + }, + { + "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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List of temperatures in Fahrenheit: [91.4, 150.8, 149.0, 143.60000000000002, 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": [ + "temperatures_F=[]\n", + "\n", + "for i in range(len(temperatures_C)):\n", + " temperatures_F.append(1.8*temperatures_C[i]+32)\n", + "print(\"List of temperatures in Fahrenheit:\",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": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cooling system needs to be changed\n" + ] + } + ], + "source": [ + "if len(temp_greater_equal_70)>=4 or max(temperatures_C)>80 or avg_temp>65:\n", + " print(\"Cooling system needs to be changed\")\n", + "else: print(\"Cooling system does not need to be changed\") " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Bonus\n", + "\n", + "The company has decided that the decision you made is not valid. They want you to analyze the data again but this time, the conditions that need to be met in order to change the cooling system are different.\n", + "\n", + "This time, if one of the following events occurs, then the cooling system needs to be replaced:\n", + "* The temperature is greater than 70ºC during more than 4 consecutive hours.\n", + "* Any temperature is above 80ºC.\n", + "* The average temperature exceeds 65ºC.\n", + "\n", + "Follow the steps so that you can make the decision.\n", + "\n", + "#### 1. Create a list with the hours where the temperature is greater than 70ºC. Store it in a variable." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hours >70C (incl. duplicates due to .index method): [9, 10, 11, 10, 13, 14, 15]\n", + "Hours >70C with correct 'index': [9, 10, 11, 12, 13, 14, 15]\n", + "Hours shown in correct time of day: [9, 10, 11, 12, 1, 2, 3]\n" + ] + } + ], + "source": [ + "temperatures_C = [33, 66, 65, 62, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n", + "hours_temp_greater_70=[]\n", + "\n", + "#index based list of temperatures >70 (might contain duplicates as index of first \n", + "#occurrence of an element will be shown for every occurrence of this element)\n", + "for temp in temperatures_C:\n", + " if temp >70:\n", + " hours_temp_greater_70.append(temperatures_C.index(temp))\n", + "print(\"Hours >70C (incl. duplicates due to .index method):\",hours_temp_greater_70)\n", + "\n", + "#removing duplicates caused by .index method\n", + "index2=[]\n", + "for i in range(len(temperatures_C)):\n", + " if temperatures_C[i]>70:\n", + " index2.append(i)\n", + "print(\"Hours >70C with correct 'index':\",index2)\n", + "\n", + "#convert in times of day:\n", + "hours=[]\n", + "for i in index2:\n", + " if i <=12:\n", + " hours.append(i)\n", + " else: hours.append(i-12)\n", + "print(\"Hours shown in correct time of day:\",hours)\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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "List has more than 4 consecutive hours.\n" + ] + } + ], + "source": [ + "consecutive_hours=[]\n", + "for n in range(3): \n", + " if index2[n+1]==index2[n]+1 and index2[n+2]==index2[n]+2 and index2[n+3]==index2[n]+3 and index2[n+4]==index2[n]+4:\n", + " consecutive_hours.append(\"Yes\")\n", + " break\n", + " else: \n", + " consecutive_hours.append(\"No\")\n", + "if \"Yes\" in consecutive_hours:\n", + " print(\"List has more than 4 consecutive hours.\")\n", + "else: print(\"List hasn't more than 4 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": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cooling system needs to be changed\n" + ] + } + ], + "source": [ + "if \"Yes\" in consecutive_hours or max(temperatures_C)>80 or avg_temp>65:\n", + " print(\"Cooling system needs to be changed\")\n", + "else: print(\"Cooling system does not need to be changed\") " + ] + }, + { + "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": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average temperature (C) of the day is: 62.833333333333336\n", + "Average temperature (F) of the day is: 145.1\n", + "The relation between both is that avg_temp_F is equal to 1.8∗avg_temp+32\n" + ] + } + ], + "source": [ + "#Calculations based on the temp list with updated value at 3:\n", + "\n", + "avg_temp=sum(temperatures_C)/len(temperatures_C)\n", + "print(\"Average temperature (C) of the day is:\",avg_temp)\n", + "\n", + "avg_temp_F=sum(temperatures_F)/len(temperatures_F)\n", + "print(\"Average temperature (F) of the day is:\",avg_temp_F)\n", + "\n", + "print(\"The relation between both is that avg_temp_F is equal to 1.8∗avg_temp+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": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation of temperature list in Celsius: 14.633485192833897\n", + "Standard deviation of temperature list in Fahrenheit: 26.340273347101014\n", + "Dividing Standard Deviation (F) by Standard Deviation (C): 1.8\n", + "Standard deviation in Fahrenheit is 1.8x higher than the standard deviation in Celsius, as the spread is affected by factor 1.8 (adding 32 does not affect the spread)\n" + ] + } + ], + "source": [ + "import statistics\n", + "print(\"Standard deviation of temperature list in Celsius:\",statistics.pstdev(temperatures_C))\n", + "\n", + "print(\"Standard deviation of temperature list in Fahrenheit:\",statistics.pstdev(temperatures_F))\n", + "print(\"Dividing Standard Deviation (F) by Standard Deviation (C):\",statistics.pstdev(temperatures_F)/statistics.pstdev(temperatures_C))\n", + "print(\"Standard deviation in Fahrenheit is 1.8x higher than the standard deviation in Celsius, as the spread is affected by factor 1.8 (adding 32 does not affect the spread)\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.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 4b597aa..6e90094 100644 --- a/1.-Python/5.-Temperature-Processor/temperature.ipynb +++ b/1.-Python/5.-Temperature-Processor/temperature.ipynb @@ -254,7 +254,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.5" } }, "nbformat": 4, diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors-solution-tg.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors-solution-tg.ipynb" new file mode 100644 index 0000000..06c0710 --- /dev/null +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors-solution-tg.ipynb" @@ -0,0 +1,534 @@ +{ + "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": 1, + "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": 2, + "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of rounds, number has to be odd: 5\n" + ] + } + ], + "source": [ + "while True:\n", + " n_rounds = int(input(\"Enter the number of rounds, number has to be odd: \"))\n", + " if n_rounds%2==0 or n_rounds<1:\n", + " continue\n", + " elif n_rounds%2!=0 and n_rounds>=1:\n", + " break\n", + "\n", + "\n", + "#check if text inputs can be excluded" + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [], + "source": [ + "rounds_to_win=int(n_rounds/2)+1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 5. Create two variables to store the number of rounds that the computer and the player have won. Call these variables `cpu_score` and `player_score`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "cpu_score=0\n", + "player_score=0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 6. Define a function that randomly returns one of the 3 gesture options.\n", + "You will use this function to simulate the gesture choice of the computer. " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "choice_computer=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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Choose between one of the 3 gesture options: Rock, Paper or Scissors: jhgghghj\n", + "Choose between one of the 3 gesture options: Rock, Paper or Scissors: rock\n" + ] + } + ], + "source": [ + "while True:\n", + " choice_player = input(\"Choose between one of the 3 gesture options: Rock, Paper or Scissors: \")\n", + " if choice_player.lower()==\"rock\" or choice_player.lower()==\"paper\" or choice_player.lower()==\"scissors\":\n", + " break\n", + " else: continue " + ] + }, + { + "cell_type": "raw", + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "if choice_computer==\"scissors\" and choice_player.lower()==\"paper\":\n", + " winner=1\n", + "elif choice_computer==\"paper\" and choice_player.lower()==\"rock\":\n", + " winner=1\n", + "elif choice_computer==\"rock\" and choice_player.lower()==\"scissors\":\n", + " winner=1\n", + "elif choice_computer==\"paper\" and choice_player.lower()==\"scissors\":\n", + " winner=2\n", + "elif choice_computer==\"rock\" and choice_player.lower()==\"paper\":\n", + " winner=2\n", + "elif choice_computer==\"scissors\" and choice_player.lower()==\"rock\":\n", + " winner=2\n", + "else: winner=0\n", + "print(winner) " + ] + }, + { + "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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Computer chose: rock\n", + "Player chose: rock\n", + "No winner of this round: Tie\n", + "Player Score: 0\n", + "Computer Score: 0\n" + ] + } + ], + "source": [ + "print(\"Computer chose:\",choice_computer)\n", + "print(\"Player chose:\",choice_player.lower())\n", + "if winner==1:\n", + " print(\"Winner of this round is: Computer\")\n", + " cpu_score+=1\n", + "elif winner==2:\n", + " print(\"Winner of this round is: Player\")\n", + " player_score+=1\n", + "else:\n", + " print(\"No winner of this round: Tie\")\n", + "print(\"Player Score:\",player_score)\n", + "print(\"Computer Score:\",cpu_score)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 10. Now it's time to code the execution of the game using the functions and variables you defined above. \n", + "\n", + "First, create a loop structure that repeats while no player reaches the minimum score necessary to win and the number of rounds is less than the maximum number of rounds to play in a game. \n", + "\n", + "Inside the loop, use the functions and variables above to create the execution of a round: ask for the player's choice, generate the random choice of the computer, show the round results, update the scores, etc. " + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of rounds, number has to be odd: 1\n", + "\n", + "Choose between one of the 3 gesture options: Rock, Paper or Scissors: rock\n", + "\n", + "Computer chose: scissors\n", + "Player chose: rock\n", + "Winner of this round is: Player\n", + "Player Score: 1\n", + "Computer Score: 0\n", + "\n", + "Game finished - required # of rounds for winning this game have been won\n" + ] + } + ], + "source": [ + "import random\n", + "while True:\n", + " n_rounds = int(input(\"Enter the number of rounds, number has to be odd: \"))\n", + " if n_rounds%2==0 or n_rounds<1:\n", + " continue\n", + " elif n_rounds%2!=0 and n_rounds>=1:\n", + " break\n", + "print(\"\") \n", + "gestures=['rock','paper','scissors']\n", + "rounds_to_win=int(n_rounds/2)+1\n", + "cpu_score=0\n", + "player_score=0\n", + "while True:\n", + " n_rounds-=1\n", + " if n_rounds <0:\n", + " print(\"Game finished - all rounds have been played\")\n", + " break\n", + " while True:\n", + " choice_player = input(\"Choose between one of the 3 gesture options: Rock, Paper or Scissors: \")\n", + " if choice_player.lower()==\"rock\" or choice_player.lower()==\"paper\" or choice_player.lower()==\"scissors\":\n", + " break\n", + " else: continue \n", + " print(\"\")\n", + " choice_computer=random.choice(gestures)\n", + " if choice_computer==\"scissors\" and choice_player.lower()==\"paper\":\n", + " winner=1\n", + " elif choice_computer==\"paper\" and choice_player.lower()==\"rock\":\n", + " winner=1\n", + " elif choice_computer==\"rock\" and choice_player.lower()==\"scissors\":\n", + " winner=1\n", + " elif choice_computer==\"paper\" and choice_player.lower()==\"scissors\":\n", + " winner=2\n", + " elif choice_computer==\"rock\" and choice_player.lower()==\"paper\":\n", + " winner=2\n", + " elif choice_computer==\"scissors\" and choice_player.lower()==\"rock\":\n", + " winner=2\n", + " else: winner=0\n", + " \n", + " print(\"Computer chose:\",choice_computer)\n", + " print(\"Player chose:\",choice_player.lower())\n", + " if winner==1:\n", + " print(\"Winner of this round is: Computer\")\n", + " cpu_score+=1\n", + " elif winner==2:\n", + " print(\"Winner of this round is: Player\")\n", + " player_score+=1\n", + " elif winner==0:\n", + " print(\"No winner of this round: Tie\")\n", + " print(\"Player Score:\",player_score)\n", + " print(\"Computer Score:\",cpu_score)\n", + " print(\"\")\n", + " if player_score==rounds_to_win or cpu_score==rounds_to_win:\n", + " print(\"Game finished - required # of rounds for winning this game have been won\")\n", + " break" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "#### 11. Print the winner of the game based on who won more rounds.\n", + "Remember that the game might be tied. " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rounds won by player: 1\n", + "Rounds won by computer: 0\n", + "The winner is: Player\n" + ] + } + ], + "source": [ + "print(\"Rounds won by player:\",player_score)\n", + "print(\"Rounds won by computer:\",cpu_score)\n", + "\n", + "\n", + "if cpu_score>player_score:\n", + " print(\"The winner is: Computer\")\n", + "if cpu_score=1:\n", + " break\n", + "print(\"\") \n", + "gestures=['rock','paper','scissors','spock','scissors']\n", + "rounds_to_win=int(n_rounds/2)+1\n", + "cpu_score=0\n", + "player_score=0\n", + "while True:\n", + " n_rounds-=1\n", + " if n_rounds <0:\n", + " print(\"Game finished - all rounds have been played\")\n", + " break\n", + " while True:\n", + " choice_player = input(\"Choose between one of the 5 gesture options: Rock, Paper, Scissors, Lizard or Spock: \")\n", + " if choice_player.lower()==\"rock\" or choice_player.lower()==\"paper\" or choice_player.lower()==\"scissors\" or choice_player.lower()==\"lizard\" or choice_player.lower()==\"spock\":\n", + " break\n", + " else: continue \n", + " print(\"\")\n", + " choice_computer=random.choice(gestures)\n", + " if choice_computer==\"scissors\" and (choice_player.lower()==\"paper\" or choice_player.lower()==\"lizard\"):\n", + " winner=1\n", + " elif choice_computer==\"paper\" and (choice_player.lower()==\"rock\" or choice_player.lower()==\"spock\"):\n", + " winner=1\n", + " elif choice_computer==\"rock\" and (choice_player.lower()==\"scissors\" or choice_player.lower()==\"lizard\"):\n", + " winner=1\n", + " elif choice_computer==\"lizard\" and (choice_player.lower()==\"spock\" or choice_player.lower()==\"paper\"):\n", + " winner=1 \n", + " elif choice_computer==\"spock\" and (choice_player.lower()==\"rock\" or choice_player.lower()==\"scissors\"):\n", + " winner=1 \n", + " \n", + " elif (choice_computer==\"paper\" or choice_computer==\"lizard\") and choice_player.lower()==\"scissors\":\n", + " winner=2\n", + " elif (choice_computer==\"rock\" or choice_computer==\"spock\") and choice_player.lower()==\"paper\":\n", + " winner=2\n", + " elif (choice_computer==\"scissors\" or choice_computer==\"lizard\") and choice_player.lower()==\"rock\":\n", + " winner=2\n", + " elif (choice_computer==\"spock\" or choice_computer==\"paper\") and choice_player.lower()==\"lizard\":\n", + " winner=2\n", + " elif (choice_computer==\"rock\" or choice_computer==\"scissors\") and choice_player.lower()==\"spock\":\n", + " winner=2 \n", + " else: winner=0\n", + " \n", + " print(\"Computer chose:\",choice_computer)\n", + " print(\"Player chose:\",choice_player.lower())\n", + " if winner==1:\n", + " print(\"Winner of this round is: Computer\")\n", + " cpu_score+=1\n", + " elif winner==2:\n", + " print(\"Winner of this round is: Player\")\n", + " player_score+=1\n", + " elif winner==0:\n", + " print(\"No winner of this round: Tie\")\n", + " print(\"Player Score:\",player_score)\n", + " print(\"Computer Score:\",cpu_score)\n", + " print(\"\")\n", + " if player_score==rounds_to_win or cpu_score==rounds_to_win:\n", + " print(\"Game finished - required # of rounds for winning this game have been won\")\n", + " break\n", + "print(\"\")\n", + "print(\"Rounds won by player:\",player_score)\n", + "print(\"Rounds won by computer:\",cpu_score)\n", + "\n", + "\n", + "if cpu_score>player_score:\n", + " print(\"The winner is: Computer\")\n", + "if cpu_score 75)\n", + "print(93 < 80)\n", + "print(3 in [1,2,3,4,5])\n", + "print(3 not in [1,2,3,4,5])" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "print((5 + 5 == 10) & ('apple' != 'orange'))\n", + "print((100 > 75) & (93 < 80))\n", + "print((100 > 75) | (93 < 80))\n", + "print((93 < 80) | (3 not in [1,2,3,4,5]))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number is greater than 10\n" + ] + } + ], + "source": [ + "number = 10.3\n", + "\n", + "if number < 10:\n", + " print(\"Number is less than 10\")\n", + "elif number > 10:\n", + " print(\"Number is greater than 10\")\n", + "elif number == 10:\n", + " print(\"Number is exactly 10\")\n", + "else:\n", + " print(\"Number is probably not a number at all\")" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your total commute time is expected to be 45 minutes.\n" + ] + } + ], + "source": [ + "commute = 30\n", + "rain = True\n", + "traffic = False\n", + "\n", + "if (rain == True) | (traffic == True):\n", + " if (rain == True) & (traffic == True):\n", + " total_commute = commute + 15 + 20\n", + " elif (rain == True) & (traffic == False):\n", + " total_commute = commute + 15\n", + " elif (rain == False) & (traffic == True):\n", + " total_commute = commute + 20\n", + "else:\n", + " total_commute = commute\n", + "\n", + "print(\"Your total commute time is expected to be\", total_commute, \"minutes.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(0, 10)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(5, 15)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(5, 15)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "range(0, 10)\n" + ] + } + ], + "source": [ + "print(range(10))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(range(5, 15))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n" + ] + } + ], + "source": [ + "for i in range(5, 15):\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "apple\n", + "orange\n", + "banana\n", + "grapes\n", + "pineapple\n" + ] + } + ], + "source": [ + "fruits = ['apple', 'orange', 'banana', 'grapes', 'pineapple']\n", + "\n", + "for fruit in fruits:\n", + " print(fruit)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['apple', 'orange', 'banana', 'grapes', 'pineapple']\n" + ] + } + ], + "source": [ + "print(fruits)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Brian is 23 years old.\n", + "Amy is 22 years old.\n", + "Darlene is 47 years old.\n", + "Ralph is 32 years old.\n", + "Jordan is 28 years old.\n", + "Stephanie is 35 years old.\n" + ] + } + ], + "source": [ + "ages = {'Brian':23, 'Amy':22, 'Darlene':47, 'Ralph':32, 'Jordan':28, 'Stephanie':35}\n", + "\n", + "for name, age in ages.items():\n", + " print(name, \"is\", age, \"years old.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total is currently 34\n", + "Total is currently 46\n", + "Total is currently 139\n", + "Total is currently 922\n", + "Total is currently 1252\n", + "Total is currently 2148\n", + "Total is currently 2149\n", + "Total is currently 2204\n" + ] + } + ], + "source": [ + "num_list = [34, 12, 93, 783, 330, 896, 1, 55]\n", + "\n", + "total = 0\n", + "\n", + "for i in num_list:\n", + " total = total + i\n", + " print(\"Total is currently\", total)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1]\n", + "[1, 4]\n", + "[1, 4, 9]\n", + "[1, 4, 9, 16]\n", + "[1, 4, 9, 16, 25]\n", + "[1, 4, 9, 16, 25, 36]\n", + "[1, 4, 9, 16, 25, 36, 49]\n", + "[1, 4, 9, 16, 25, 36, 49, 64]\n", + "[1, 4, 9, 16, 25, 36, 49, 64, 81]\n", + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "new_list = []\n", + "\n", + "for i in range(1, 11):\n", + " square = i**2\n", + " new_list.append(square)\n", + "\n", + " print(new_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cake is in the oven.\n", + "Cake is not done yet.\n", + "Cake is not done yet.\n", + "Cake is not done yet.\n", + "It's done. Let's eat cake!\n" + ] + } + ], + "source": [ + "total_time = 60\n", + "minutes_elapsed = 0\n", + "wait = 15\n", + "\n", + "print(\"Cake is in the oven.\")\n", + "minutes_elapsed += wait\n", + "\n", + "while minutes_elapsed < total_time:\n", + " print(\"Cake is not done yet.\")\n", + " minutes_elapsed += wait\n", + "\n", + "print(\"It's done. Let's eat cake!\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Lessons/Lesson - Working with Files in Python.ipynb b/Lessons/Lesson - Working with Files in Python.ipynb new file mode 100644 index 0000000..9dda8eb --- /dev/null +++ b/Lessons/Lesson - Working with Files in Python.ipynb @@ -0,0 +1,526 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework'" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: '/Users/username/Desktop'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mchdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'/Users/username/Desktop'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetcwd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/Users/username/Desktop'" + ] + } + ], + "source": [ + "os.chdir('/Users/username/Desktop')\n", + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Desktop'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.chdir('/Users/tobiasglinzer/Desktop')\n", + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C:/ironhack/module_0/python-beginner/data'" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "root = 'C:'\n", + "level_1 = 'ironhack'\n", + "level_2 = 'module_0'\n", + "level_3 = 'python-beginner'\n", + "level_4 = 'data'\n", + "\n", + "os.path.join(root, level_1, level_2, level_3, level_4)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Desktop'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.chdir('/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework')\n", + "os.getcwd()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "folder_name = 'new_folder'\n", + "\n", + "if os.path.exists(folder_name) == False:\n", + " os.makedirs(folder_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework/new_folder'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "os.chdir('/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework/new_folder')\n", + "os.getcwd()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "with open(\"example.txt\", \"w\") as f:\n", + " f.write(\"Hello World! \\n\")\n", + " f.write(\"How are you? \\n\")\n", + " f.write(\"I'm fine.\")" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello World! \n", + "\n", + "How are you? \n", + "\n", + "I'm fine.\n" + ] + } + ], + "source": [ + "with open(\"example.txt\", \"r\") as f:\n", + " lines = f.readlines()\n", + " for line in lines:\n", + " print(line)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework\n" + ] + } + ], + "source": [ + "cd .." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34m1.-Python\u001b[m\u001b[m/\r\n", + "\u001b[34m2.-Statistics\u001b[m\u001b[m/\r\n", + "Lesson - Control Flow - Conditional Logic and Loops.ipynb\r\n", + "Lesson - Working with Files in Python.ipynb\r\n", + "\u001b[31mREADME.md\u001b[m\u001b[m*\r\n", + "\u001b[34mnew_folder\u001b[m\u001b[m/\r\n", + "robot.md\r\n" + ] + } + ], + "source": [ + "ls" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework/new_folder\n" + ] + } + ], + "source": [ + "cd new_folder" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "example.txt weight_height.csv\r\n" + ] + } + ], + "source": [ + "ls" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m $ echo `pwd`/`ls weight_height.csv`\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "$ echo `pwd`/`ls weight_height.csv`" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m echo `pwd`/`ls weight_height.csv`\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "echo `pwd`/`ls weight_height.csv`" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m echo 'pwd'/'ls weight_height.csv'\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "echo 'pwd'/'ls weight_height.csv'" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "data = []\n", + "\n", + "with open(\"/Users/tobiasglinzer/Documents/Ironhack/Prework/GIT/data-bcn-prework/new_folder/weight_height.csv\", \"r\") as f:\n", + " lines = f.readlines()\n", + " for line in lines:\n", + " data.append(line.split()[0].split(\",\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['gender',\n", + " 'actual_weight',\n", + " 'actual_height',\n", + " 'reported_weight',\n", + " 'reported_height']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[['gender', 'actual_weight', 'actual_height', 'reported_weight', 'reported_height'], ['M', '77', '182', '77', '180'], ['F', '58', '161', '51', '159'], ['F', '53', '161', '54', '158'], ['M', '68', '177', '70', '175'], ['F', '59', '157', '59', '155'], ['M', '76', '170', '76', '165'], ['M', '76', '167', '77', '165'], ['M', '69', '186', '73', '180'], ['M', '71', '178', '71', '175'], ['M', '65', '171', '64', '170'], ['M', '70', '175', '75', '174'], ['F', '166', '57', '56', '163'], ['F', '51', '161', '52', '158'], ['F', '64', '168', '64', '165'], ['F', '52', '163', '57', '160'], ['F', '65', '166', '66', '165'], ['M', '92', '187', '101', '185'], ['F', '62', '168', '62', '165'], ['M', '76', '197', '75', '200'], ['F', '61', '175', '61', '171'], ['M', '119', '180', '124', '178'], ['F', '61', '170', '61', '170'], ['M', '65', '175', '66', '173'], ['M', '66', '173', '70', '170'], ['F', '54', '171', '59', '168'], ['F', '50', '166', '50', '165'], ['F', '63', '169', '61', '168'], ['F', '58', '166', '60', '160'], ['F', '39', '157', '41', '153'], ['M', '101', '183', '100', '180'], ['F', '71', '166', '71', '165'], ['M', '75', '178', '73', '175'], ['M', '79', '173', '76', '173'], ['F', '52', '164', '52', '161'], ['F', '68', '169', '63', '170'], ['M', '64', '176', '65', '175'], ['F', '56', '166', '54', '165'], ['M', '69', '174', '69', '171'], ['M', '88', '178', '86', '175'], ['M', '65', '187', '67', '188'], ['F', '54', '164', '53', '160'], ['M', '80', '178', '80', '178'], ['F', '63', '163', '59', '159'], ['M', '78', '183', '80', '180'], ['M', '85', '179', '82', '175'], ['F', '54', '160', '55', '158'], ['F', '54', '174', '56', '173'], ['F', '75', '162', '75', '158'], ['M', '82', '182', '85', '183'], ['F', '56', '165', '57', '163'], ['M', '74', '169', '73', '170'], ['M', '102', '185', '107', '185'], ['M', '65', '176', '64', '172'], ['M', '73', '183', '74', '180'], ['M', '75', '172', '70', '169'], ['M', '57', '173', '58', '170'], ['M', '68', '165', '69', '165'], ['M', '71', '177', '71', '170'], ['M', '71', '180', '76', '175'], ['F', '78', '173', '75', '169'], ['M', '97', '189', '98', '185'], ['F', '60', '162', '59', '160'], ['F', '64', '165', '63', '163'], ['F', '64', '164', '62', '161'], ['F', '52', '158', '51', '155'], ['M', '80', '178', '76', '175'], ['F', '62', '175', '61', '171'], ['M', '66', '173', '66', '175'], ['F', '55', '165', '54', '163'], ['F', '56', '163', '57', '159'], ['F', '50', '166', '50', '161'], ['F', '50', '160', '55', '150'], ['F', '63', '160', '64', '158'], ['M', '69', '182', '70', '180'], ['M', '69', '183', '70', '183'], ['F', '61', '165', '60', '163'], ['M', '55', '168', '56', '170'], ['F', '53', '169', '52', '175'], ['F', '60', '167', '55', '163'], ['F', '56', '170', '56', '170'], ['M', '59', '182', '61', '183'], ['M', '62', '178', '66', '175'], ['F', '53', '165', '53', '165'], ['F', '57', '163', '59', '160'], ['F', '57', '162', '56', '160'], ['M', '70', '173', '68', '170'], ['F', '56', '161', '56', '161'], ['M', '84', '184', '86', '183'], ['M', '69', '180', '71', '180'], ['M', '88', '189', '87', '185'], ['F', '56', '165', '57', '160'], ['M', '103', '185', '101', '182'], ['F', '50', '169', '50', '165'], ['F', '52', '159', '52', '153'], ['F', '55', '164', '55', '163'], ['M', '63', '178', '63', '175'], ['F', '47', '163', '47', '160'], ['F', '45', '163', '45', '160'], ['F', '62', '175', '63', '173'], ['F', '53', '164', '51', '160'], ['F', '52', '152', '51', '150'], ['F', '57', '167', '55', '164'], ['F', '64', '166', '64', '165'], ['F', '59', '166', '55', '163'], ['M', '84', '183', '90', '183'], ['M', '79', '179', '79', '171'], ['F', '55', '174', '57', '171'], ['M', '67', '179', '67', '179'], ['F', '76', '167', '77', '165'], ['F', '62', '168', '62', '163'], ['M', '83', '184', '83', '181'], ['M', '96', '184', '94', '183'], ['M', '75', '169', '76', '165'], ['M', '65', '178', '66', '178'], ['M', '78', '178', '77', '175'], ['M', '69', '167', '73', '165'], ['F', '68', '178', '68', '175'], ['F', '55', '165', '55', '163'], ['F', '52', '169', '56', 'NA'], ['F', '45', '157', '45', '153'], ['F', '68', '171', '68', '169'], ['F', '44', '157', '44', '155'], ['F', '62', '166', '61', '163'], ['M', '87', '185', '89', '185'], ['F', '56', '160', '53', '158'], ['F', '50', '148', '47', '148'], ['M', '83', '177', '84', '175'], ['F', '53', '162', '53', '160'], ['F', '64', '172', '62', '168'], ['M', '90', '188', '91', '185'], ['M', '85', '191', '83', '188'], ['M', '66', '175', '68', '175'], ['F', '52', '163', '53', '160'], ['F', '53', '165', '55', '163'], ['F', '54', '176', '55', '176'], ['F', '64', '171', '66', '171'], ['F', '55', '160', '55', '155'], ['F', '55', '165', '55', '165'], ['F', '59', '157', '55', '158'], ['F', '70', '173', '67', '170'], ['M', '88', '184', '86', '183'], ['F', '57', '168', '58', '165'], ['F', '47', '162', '47', '160'], ['F', '47', '150', '45', '152'], ['F', '48', '163', '44', '160'], ['M', '54', '169', '58', '165'], ['M', '69', '172', '68', '174'], ['F', '57', '167', '56', '165'], ['F', '51', '163', '50', '160'], ['F', '54', '161', '54', '160'], ['F', '53', '162', '52', '158'], ['F', '59', '172', '58', '171'], ['M', '56', '163', '58', '161'], ['F', '59', '159', '59', '155'], ['F', '63', '170', '62', '168'], ['F', '66', '166', '66', '165'], ['M', '96', '191', '95', '188'], ['F', '53', '158', '50', '155'], ['M', '76', '169', '75', '165'], ['M', '61', '170', '61', '170'], ['M', '62', '168', '64', '168'], ['M', '71', '178', '68', '178'], ['M', '66', '170', '67', '165'], ['M', '81', '178', '82', '175'], ['M', '68', '174', '68', '173'], ['M', '80', '176', '78', '175'], ['F', '63', '165', '59', '160'], ['M', '70', '173', '70', '173'], ['F', '56', '162', '56', '160'], ['F', '60', '172', '55', '168'], ['F', '58', '169', '54', '166'], ['M', '76', '183', '75', '180'], ['F', '50', '158', '49', '155'], ['M', '88', '185', '93', '188'], ['M', '89', '173', '86', '173'], ['F', '59', '164', '59', '165'], ['F', '51', '156', '51', '158'], ['F', '62', '164', '61', '161'], ['M', '74', '175', '71', '175'], ['M', '83', '180', '80', '180'], ['M', '90', '181', '91', '178'], ['M', '79', '177', '81', '178']]\n" + ] + } + ], + "source": [ + "print(data)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'M'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1][0]" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "heights = []\n", + "\n", + "for person in data[1:]:\n", + " height = int(person[2])\n", + " heights.append(height)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "170.14835164835165\n" + ] + } + ], + "source": [ + "avg_height = sum(heights)/len(heights)\n", + "print(avg_height)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Avg male height: 178.0121951219512\n", + "Avg female height 163.7\n" + ] + } + ], + "source": [ + "male_heights = []\n", + "female_heights = []\n", + "\n", + "for person in data[1:]:\n", + " height = int(person[2])\n", + " if person[0] == 'M':\n", + " male_heights.append(height)\n", + " elif person[0] == 'F':\n", + " female_heights.append(height)\n", + "\n", + "avg_male_height = sum(male_heights)/len(male_heights)\n", + "avg_female_height = sum(female_heights)/len(female_heights)\n", + "\n", + "print(\"Avg male height:\", avg_male_height)\n", + "print(\"Avg female height\", avg_female_height)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Lessons/new_folder/example.txt b/Lessons/new_folder/example.txt new file mode 100644 index 0000000..21a056b --- /dev/null +++ b/Lessons/new_folder/example.txt @@ -0,0 +1,3 @@ +Hello World! +How are you? +I'm fine. \ No newline at end of file diff --git a/Lessons/new_folder/weight_height.csv b/Lessons/new_folder/weight_height.csv new file mode 100644 index 0000000..c025681 --- /dev/null +++ b/Lessons/new_folder/weight_height.csv @@ -0,0 +1,183 @@ +gender,actual_weight,actual_height,reported_weight,reported_height +M,77,182,77,180 +F,58,161,51,159 +F,53,161,54,158 +M,68,177,70,175 +F,59,157,59,155 +M,76,170,76,165 +M,76,167,77,165 +M,69,186,73,180 +M,71,178,71,175 +M,65,171,64,170 +M,70,175,75,174 +F,166,57,56,163 +F,51,161,52,158 +F,64,168,64,165 +F,52,163,57,160 +F,65,166,66,165 +M,92,187,101,185 +F,62,168,62,165 +M,76,197,75,200 +F,61,175,61,171 +M,119,180,124,178 +F,61,170,61,170 +M,65,175,66,173 +M,66,173,70,170 +F,54,171,59,168 +F,50,166,50,165 +F,63,169,61,168 +F,58,166,60,160 +F,39,157,41,153 +M,101,183,100,180 +F,71,166,71,165 +M,75,178,73,175 +M,79,173,76,173 +F,52,164,52,161 +F,68,169,63,170 +M,64,176,65,175 +F,56,166,54,165 +M,69,174,69,171 +M,88,178,86,175 +M,65,187,67,188 +F,54,164,53,160 +M,80,178,80,178 +F,63,163,59,159 +M,78,183,80,180 +M,85,179,82,175 +F,54,160,55,158 +F,54,174,56,173 +F,75,162,75,158 +M,82,182,85,183 +F,56,165,57,163 +M,74,169,73,170 +M,102,185,107,185 +M,65,176,64,172 +M,73,183,74,180 +M,75,172,70,169 +M,57,173,58,170 +M,68,165,69,165 +M,71,177,71,170 +M,71,180,76,175 +F,78,173,75,169 +M,97,189,98,185 +F,60,162,59,160 +F,64,165,63,163 +F,64,164,62,161 +F,52,158,51,155 +M,80,178,76,175 +F,62,175,61,171 +M,66,173,66,175 +F,55,165,54,163 +F,56,163,57,159 +F,50,166,50,161 +F,50,160,55,150 +F,63,160,64,158 +M,69,182,70,180 +M,69,183,70,183 +F,61,165,60,163 +M,55,168,56,170 +F,53,169,52,175 +F,60,167,55,163 +F,56,170,56,170 +M,59,182,61,183 +M,62,178,66,175 +F,53,165,53,165 +F,57,163,59,160 +F,57,162,56,160 +M,70,173,68,170 +F,56,161,56,161 +M,84,184,86,183 +M,69,180,71,180 +M,88,189,87,185 +F,56,165,57,160 +M,103,185,101,182 +F,50,169,50,165 +F,52,159,52,153 +F,55,164,55,163 +M,63,178,63,175 +F,47,163,47,160 +F,45,163,45,160 +F,62,175,63,173 +F,53,164,51,160 +F,52,152,51,150 +F,57,167,55,164 +F,64,166,64,165 +F,59,166,55,163 +M,84,183,90,183 +M,79,179,79,171 +F,55,174,57,171 +M,67,179,67,179 +F,76,167,77,165 +F,62,168,62,163 +M,83,184,83,181 +M,96,184,94,183 +M,75,169,76,165 +M,65,178,66,178 +M,78,178,77,175 +M,69,167,73,165 +F,68,178,68,175 +F,55,165,55,163 +F,52,169,56,NA +F,45,157,45,153 +F,68,171,68,169 +F,44,157,44,155 +F,62,166,61,163 +M,87,185,89,185 +F,56,160,53,158 +F,50,148,47,148 +M,83,177,84,175 +F,53,162,53,160 +F,64,172,62,168 +M,90,188,91,185 +M,85,191,83,188 +M,66,175,68,175 +F,52,163,53,160 +F,53,165,55,163 +F,54,176,55,176 +F,64,171,66,171 +F,55,160,55,155 +F,55,165,55,165 +F,59,157,55,158 +F,70,173,67,170 +M,88,184,86,183 +F,57,168,58,165 +F,47,162,47,160 +F,47,150,45,152 +F,48,163,44,160 +M,54,169,58,165 +M,69,172,68,174 +F,57,167,56,165 +F,51,163,50,160 +F,54,161,54,160 +F,53,162,52,158 +F,59,172,58,171 +M,56,163,58,161 +F,59,159,59,155 +F,63,170,62,168 +F,66,166,66,165 +M,96,191,95,188 +F,53,158,50,155 +M,76,169,75,165 +M,61,170,61,170 +M,62,168,64,168 +M,71,178,68,178 +M,66,170,67,165 +M,81,178,82,175 +M,68,174,68,173 +M,80,176,78,175 +F,63,165,59,160 +M,70,173,70,173 +F,56,162,56,160 +F,60,172,55,168 +F,58,169,54,166 +M,76,183,75,180 +F,50,158,49,155 +M,88,185,93,188 +M,89,173,86,173 +F,59,164,59,165 +F,51,156,51,158 +F,62,164,61,161 +M,74,175,71,175 +M,83,180,80,180 +M,90,181,91,178 +M,79,177,81,178 \ No newline at end of file diff --git a/robot.md b/robot.md new file mode 100644 index 0000000..e69de29