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..37707e1 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": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "well_height = 125\n", + "daily_distance = 30\n", + "nightly_distance = -20\n", + "snail_position = 0" + ] }, { "cell_type": "markdown", @@ -44,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 0" + ] }, { "cell_type": "markdown", @@ -58,10 +65,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while snail_position < 125:\n", + " snail_position = snail_position + daily_distance + nightly_distance\n", + " days += 1" + ] }, { "cell_type": "markdown", @@ -72,10 +83,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13\n" + ] + } + ], + "source": [ + "print(days)" + ] }, { "cell_type": "markdown", @@ -96,10 +117,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n" + ] + } + ], + "source": [ + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "snail_position = 0\n", + "well_height = 125\n", + "days = 0\n", + "night_advance = -20\n", + "while snail_position < well_height and days < len(advance_cm):\n", + " snail_position = snail_position + advance_cm[days] + night_advance\n", + " days += 1\n", + "print(days)" + ] }, { "cell_type": "markdown", @@ -111,10 +150,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "77\n", + "12\n" + ] + } + ], + "source": [ + "maximum = max(advance_cm)\n", + "minimum = min(advance_cm)\n", + "print(maximum)\n", + "print(minimum)" + ] }, { "cell_type": "markdown", @@ -125,10 +178,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18.09090909090909\n" + ] + } + ], + "source": [ + "average = (sum(advance_cm)-20*len(advance_cm))/len(advance_cm)\n", + "print(average)" + ] }, { "cell_type": "markdown", @@ -137,6 +201,34 @@ "#### 4. What is the standard deviation of its displacement? Take into account the snail slides at night." ] }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "17.1594370826008\n" + ] + } + ], + "source": [ + "deviation = 0\n", + "for i in advance_cm:\n", + " deviation = deviation + (i - 20 - average)**2/len(advance_cm)\n", + "deviation = deviation**(1/2)\n", + "print(deviation)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -161,7 +253,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4, 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 b4a5f6d..c7ab860 100644 --- a/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb +++ b/1.-Python/2.-Duel-of-Sorcerers/duel-of-sorcerers.ipynb @@ -49,10 +49,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "spells = 10" + ] }, { "cell_type": "markdown", @@ -64,10 +68,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -78,10 +85,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "i = 0\n", + "while i < spells:\n", + " if gandalf[i] < saruman[i]:\n", + " saruman_wins += 1\n", + " elif gandalf[i] > saruman[i]:\n", + " gandalf_wins += 1\n", + " i += 1" + ] }, { "cell_type": "markdown", @@ -93,10 +108,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "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 +158,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "POWER = {\n", + " 'Fireball': 50, \n", + " 'Lightning bolt': 40, \n", + " 'Magic arrow': 10, \n", + " 'Black Tentacles': 25, \n", + " 'Contagion': 45\n", + "}\n", + "gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball', \n", + " 'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "spells = 10" + ] }, { "cell_type": "markdown", @@ -142,10 +185,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_wins = 0\n", + "saruman_wins = 0" + ] }, { "cell_type": "markdown", @@ -156,10 +202,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = []\n", + "saruman_power = []" + ] }, { "cell_type": "markdown", @@ -171,10 +220,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], + "source": [ + "i = 0\n", + "while i < spells: \n", + " if POWER[gandalf[i]] > POWER[saruman[i]]:\n", + " gandalf_wins += 1\n", + " saruman_wins = 0\n", + " if gandalf_wins == 3:\n", + " print(\"Gandalf wins\")\n", + " elif POWER[gandalf[i]] < POWER[saruman[i]]:\n", + " gandalf_wins = 0\n", + " saruman_wins += 1\n", + " if saruman_wins == 3:\n", + " print(\"Saruman wins\")\n", + " gandalf_power.append(POWER[gandalf[i]])\n", + " saruman_power.append(POWER[saruman[i]])\n", + " i += 1\n" + ] }, { "cell_type": "markdown", @@ -185,10 +258,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf's average = 39.0\n", + "Saruman's average = 30.5\n" + ] + } + ], + "source": [ + "average_gandalf = sum(gandalf_power)/spells\n", + "average_saruman = sum(saruman_power)/spells\n", + "print(\"Gandalf's average = \", average_gandalf)\n", + "print(\"Saruman's average = \", average_saruman)" + ] }, { "cell_type": "markdown", @@ -197,6 +284,43 @@ "#### 6. Find the standard deviation of the spell power of Gandalf and Saruman. " ] }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n", + "40\n", + "40\n", + "10\n", + "50\n", + "10\n", + "40\n", + "50\n", + "50\n", + "50\n", + "15.132745950421555\n", + "15.564382416273382\n" + ] + } + ], + "source": [ + "gandalf_deviation = 0\n", + "saruman_deviation = 0\n", + "for i in gandalf_power:\n", + " gandalf_deviation += (i - average_gandalf)**2/len(gandalf_power)\n", + "gandalf_deviation = gandalf_deviation**(1/2)\n", + "print(gandalf_deviation)\n", + "for i in saruman_power: \n", + " saruman_deviation += (i - average_saruman)**2/len(saruman_power)\n", + "saruman_deviation = saruman_deviation**(1/2)\n", + "print(saruman_deviation)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -221,7 +345,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8..7a50396 100755 --- a/1.-Python/3.-Bus/bus.ipynb +++ b/1.-Python/3.-Bus/bus.ipynb @@ -35,7 +35,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "number_of_stops = len(stops)\n", + "print(number_of_stops)" + ] }, { "cell_type": "markdown", @@ -67,10 +78,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 10, 13, 11, 10, 14, 10, 7, 5, 4]\n" + ] + } + ], + "source": [ + "passengers = [0]\n", + "i = 0\n", + "while i < number_of_stops:\n", + " passengers.append(passengers[i]+stops[i][0]-stops[i][1])\n", + " i += 1\n", + "print(passengers)\n", + " " + ] }, { "cell_type": "markdown", @@ -81,10 +108,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14\n" + ] + } + ], + "source": [ + "max_occupation = max(passengers)\n", + "print(max_occupation)" + ] }, { "cell_type": "markdown", @@ -93,6 +131,24 @@ "#### 4. Calculate the average occupation. And the standard deviation." ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8.4\n" + ] + } + ], + "source": [ + "average_occupation = sum(passengers)/len(passengers)\n", + "print(average_occupation)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -117,7 +173,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/1.-Python/4.-Robin-Hood/robin-hood.ipynb b/1.-Python/4.-Robin-Hood/robin-hood.ipynb index 01de29d..0c5c8ae 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ @@ -55,10 +55,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(4, 5), (5, 7), (2, 2), (-3, 2)]\n" + ] + } + ], + "source": [ + "repeated = []\n", + "points_copy = points.copy()\n", + "for i in points_copy:\n", + " j = points_copy.index(i) + 1\n", + " while j < len(points_copy):\n", + " if i == points_copy[j]:\n", + " repeated.append(points_copy[j])\n", + " points_copy.pop(j)\n", + " points_copy.remove(i)\n", + " j = len(points_copy)\n", + " else:\n", + " j += 1\n", + "print(repeated) " + ] }, { "cell_type": "markdown", @@ -70,10 +92,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + " First quadrant has 10 arrows \n", + " Secont quadrant has 6 arrows \n", + " Third quadrant has 2 arrows \n", + " Fourth quadrant has 2 arrows\n" + ] + } + ], + "source": [ + "first = 0\n", + "second = 0\n", + "third = 0\n", + "fourth = 0\n", + "for i in points:\n", + " if i[0] > 0 and i[1] > 0:\n", + " first += 1\n", + " elif i[0] < 0 and i[1] > 0:\n", + " second += 1\n", + " elif i[0] < 0 and i[1] < 0:\n", + " third += 1\n", + " elif i[0] > 0 and i[1] < 0:\n", + " fourth += 1\n", + "print('\\n First quadrant has ', first, 'arrows \\n Secont quadrant has ', second, 'arrows \\n Third quadrant has', third, 'arrows \\n Fourth quadrant has', fourth, 'arrows')" + ] }, { "cell_type": "markdown", @@ -88,10 +137,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.0\n" + ] + } + ], + "source": [ + "minimum = (points[0][0]**2 + points[0][1]**2)**(1/2)\n", + "i = 1\n", + "while i < len(points):\n", + " distance = (points[i][0]**2 + points[i][1]**2)**(1/2)\n", + " if distance < minimum:\n", + " minimum = distance\n", + " i += 1\n", + "print(minimum)" + ] }, { "cell_type": "markdown", @@ -101,6 +167,34 @@ "**Hint**: Use the function created in step 3. " ] }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "failures = 0\n", + "for i in points:\n", + " if (i[0]**2 + i[1]**2)**(1/2) > 9:\n", + " failures += 1\n", + "print(failures)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -125,7 +219,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa..1722346 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": 48, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "min_temp = min(temperatures_C)\n", + "print(min_temp)" + ] }, { "cell_type": "markdown", @@ -67,10 +78,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 50, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "90\n" + ] + } + ], + "source": [ + "max_temp = max(temperatures_C)\n", + "print(max_temp)" + ] }, { "cell_type": "markdown", @@ -81,10 +103,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[70, 76, 80, 81, 80, 83, 90, 79]\n" + ] + } + ], + "source": [ + "greater_70 = []\n", + "for i in temperatures_C:\n", + " if i >= 70:\n", + " greater_70.append(i)\n", + "print(greater_70)" + ] }, { "cell_type": "markdown", @@ -95,10 +131,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "average = sum(temperatures_C)/len(temperatures_C)\n", + "print(average)" + ] }, { "cell_type": "markdown", @@ -109,10 +156,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[33, 66, 65, 0, 59, 60, 62, 64, 70, 76, 80, 81, 80, 83, 90, 79, 61, 53, 50, 49, 53, 48, 45, 39, 60.25]\n" + ] + } + ], + "source": [ + "temperatures_C.append(average)\n", + "print(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -128,10 +186,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 54, "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, 140.45]\n" + ] + } + ], + "source": [ + "temperatures_F = []\n", + "for i in temperatures_C:\n", + " temperatures_F.append(1.8*i + 32)\n", + "print(temperatures_F)" + ] }, { "cell_type": "markdown", @@ -150,10 +221,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 3\n" + ] + } + ], + "source": [ + "more_than_70 = 0\n", + "average = 0\n", + "index = 0\n", + "for i in temperatures_C:\n", + " average += i\n", + " index += 1\n", + " if i > 80:\n", + " print(\"Change the cooling system, reason 2\")\n", + " if i >= 70:\n", + " more_than_70 += 1\n", + " if more_than_70 > 4:\n", + " print(\"Change the cooling system, reason 1\")\n", + " elif average/index > 65:\n", + " print(\"Change the cooling system, reason 3\")" + ] }, { "cell_type": "markdown", @@ -175,10 +276,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 9, 10, 11, 12, 13, 14, 15]\n" + ] + } + ], + "source": [ + "more_than_70 = []\n", + "j = 0\n", + "for i in temperatures_C:\n", + " if i >= 70:\n", + " more_than_70.append(j)\n", + " j += 1\n", + "print(more_than_70)" + ] }, { "cell_type": "markdown", @@ -189,10 +306,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are more than 4 consecutive hours with ++70\n", + "There are more than 4 consecutive hours with ++70\n", + "There are more than 4 consecutive hours with ++70\n" + ] + } + ], + "source": [ + "counter = 0\n", + "i = 0\n", + "while i < len(more_than_70) - 1:\n", + " if more_than_70[i] == more_than_70[i+1] - 1:\n", + " counter += 1\n", + " if counter > 4:\n", + " print('There are more than 4 consecutive hours with ++70')\n", + " else:\n", + " counter = 0\n", + " i += 1" + ] }, { "cell_type": "markdown", @@ -204,10 +342,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 2\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 1\n", + "Change the cooling system, reason 3\n" + ] + } + ], + "source": [ + "more_than_70 = 0\n", + "average = 0\n", + "index = 0\n", + "for i in temperatures_C:\n", + " average += i\n", + " index += 1\n", + " if i > 80:\n", + " print(\"Change the cooling system, reason 2\")\n", + " if i >= 70:\n", + " more_than_70 += 1\n", + " if more_than_70 > 4:\n", + " print(\"Change the cooling system, reason 1\")\n", + " elif average/index > 65:\n", + " print(\"Change the cooling system, reason 3\")\n", + " else:\n", + " more_than_70 = 0" + ] }, { "cell_type": "markdown", @@ -218,10 +388,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 59, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average value in Celsius 60.25\n", + "Average value in Fahrenheit 140.45\n", + "Relation between both average values 0.42897828408686367\n" + ] + } + ], + "source": [ + "average_C = sum(temperatures_C)/len(temperatures_C)\n", + "average_F = sum(temperatures_F)/len(temperatures_F)\n", + "relation = average_C/average_F\n", + "print(\"Average value in Celsius\", average_C)\n", + "print(\"Average value in Fahrenheit\", average_F)\n", + "print(\"Relation between both average values\", relation)" + ] }, { "cell_type": "markdown", @@ -230,6 +417,33 @@ "#### 5. Find the standard deviation of the temperature lists (ºC and ºF). What is the relation between both standard deviations?" ] }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "18.89603132935591\n", + "34.01285639284064\n" + ] + } + ], + "source": [ + "deviation_C = 0\n", + "deviation_F = 0\n", + "for i in temperatures_C:\n", + " deviation_C += (i - average_C)**2/len(temperatures_C)\n", + "for i in temperatures_F:\n", + " deviation_F += (i - average_F)**2/len(temperatures_F)\n", + "deviation_C **= 1/2\n", + "deviation_F **= 1/2\n", + "print(deviation_C)\n", + "print(deviation_F)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -254,7 +468,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4, 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 9e551df..653aea4 100644 --- "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" @@ -32,10 +32,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 129, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import random\n", + "import math" + ] }, { "cell_type": "markdown", @@ -46,10 +49,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 130, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gestures = [\"rock\",\"paper\",\"scissors\"]" + ] }, { "cell_type": "markdown", @@ -61,10 +66,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 131, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n_rounds = 3" + ] }, { "cell_type": "markdown", @@ -76,10 +83,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 132, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "rounds_to_win = math.ceil(n_rounds/2)" + ] }, { "cell_type": "markdown", @@ -90,10 +99,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "cpu_score = 0\n", + "player_score = 0" + ] }, { "cell_type": "markdown", @@ -105,10 +117,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 151, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def cpu_response(gestures, n):\n", + " return gestures[random.randint(0, n)]" + ] }, { "cell_type": "markdown", @@ -120,10 +135,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 144, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def human_response(gestures):\n", + " valid = False\n", + " while valid == False:\n", + " human_response = input(\"What gesture do you want to show?:\")\n", + " if human_response in gestures:\n", + " valid = True\n", + " return human_response" + ] }, { "cell_type": "markdown", @@ -135,10 +158,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def game(cpu, human):\n", + " global player_score \n", + " global cpu_score\n", + " if cpu == human:\n", + " return 0\n", + " elif cpu == \"rock\":\n", + " if human == \"paper\" or human == \"spock\":\n", + " player_score += 1\n", + " return 2\n", + " elif human == \"scissors\" or human == \"lizzard\":\n", + " cpu_score += 1\n", + " return 1\n", + " elif cpu == \"paper\":\n", + " if human == \"rock\" or human == \"spock\":\n", + " cpu_score += 1\n", + " return 1\n", + " elif human == \"scissors\" or human == \"lizzard\":\n", + " player_score += 1\n", + " return 2\n", + " elif cpu == \"scissors\":\n", + " if human == \"rock\" or human == \"spock\":\n", + " player_score += 1\n", + " return 2\n", + " elif human == \"paper\" or human == \"lizzard\": \n", + " cpu_score += 1\n", + " return 1\n", + " elif cpu == \"lizzard\":\n", + " if human == \"paper\" or human == \"spock\":\n", + " cpu_score += 1\n", + " return 1\n", + " else:\n", + " player_score += 1\n", + " return 2\n", + " elif cpu == \"spock\":\n", + " if human == \"rock\" or human == \"scissors\":\n", + " cpu_score += 1\n", + " return 1\n", + " else:\n", + " player_score += 1\n", + " return 2" + ] }, { "cell_type": "markdown", @@ -150,10 +214,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 149, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def round(gestures):\n", + " human = human_response(gestures)\n", + " cpu = cpu_response(gestures, len(gestures)-1)\n", + " results = game(cpu, human)\n", + " print(\"cpu: \",cpu)\n", + " print(\"Human: \",human)\n", + " if results == 1:\n", + " print(\"Computer won\")\n", + " elif results == 2:\n", + " print(\"Human won\")\n", + " else:\n", + " print(\"It's a tie\")" + ] }, { "cell_type": "markdown", @@ -168,10 +245,64 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 152, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What gesture do you want to show?:paper\n", + "cpu: paper\n", + "Human: paper\n", + "It's a tie\n", + "Cpu score: 0\n", + "Player score: 0\n", + "What gesture do you want to show?:rock\n", + "cpu: rock\n", + "Human: rock\n", + "It's a tie\n", + "Cpu score: 0\n", + "Player score: 0\n", + "What gesture do you want to show?:scissors\n", + "cpu: scissors\n", + "Human: scissors\n", + "It's a tie\n", + "Cpu score: 0\n", + "Player score: 0\n", + "What gesture do you want to show?:rock\n", + "cpu: paper\n", + "Human: rock\n", + "Computer won\n", + "Cpu score: 1\n", + "Player score: 0\n", + "What gesture do you want to show?:rock\n", + "cpu: scissors\n", + "Human: rock\n", + "Human won\n", + "Cpu score: 1\n", + "Player score: 1\n", + "What gesture do you want to show?:rock\n", + "cpu: paper\n", + "Human: rock\n", + "Computer won\n", + "Cpu score: 2\n", + "Player score: 1\n" + ] + } + ], + "source": [ + "def rps(gestures):\n", + " global player_score\n", + " global cpu_score\n", + " cpu_score = 0\n", + " player_score = 0\n", + " while cpu_score < rounds_to_win and player_score < rounds_to_win:\n", + " round(gestures)\n", + " print(\"Cpu score: \", cpu_score)\n", + " print(\"Player score: \", player_score)\n", + "rps(gestures)" + ] }, { "cell_type": "markdown", @@ -183,10 +314,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 153, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Computer wins\n" + ] + } + ], + "source": [ + "if cpu_score > player_score:\n", + " print(\"Computer wins\")\n", + "else:\n", + " print(\"Human wins\")" + ] }, { "cell_type": "markdown", @@ -202,6 +346,82 @@ "**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": 140, + "metadata": {}, + "outputs": [], + "source": [ + "gestures_bonus = [\"rock\",\"paper\",\"scissors\", \"lizzard\", \"spock\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 154, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What gesture do you want to show?:paper\n", + "cpu: lizzard\n", + "Human: paper\n", + "Computer won\n", + "Cpu score: 1\n", + "Player score: 0\n", + "What gesture do you want to show?:lizzard\n", + "cpu: paper\n", + "Human: lizzard\n", + "Human won\n", + "Cpu score: 1\n", + "Player score: 1\n", + "What gesture do you want to show?:spock\n", + "cpu: spock\n", + "Human: spock\n", + "It's a tie\n", + "Cpu score: 1\n", + "Player score: 1\n", + "What gesture do you want to show?:paper\n", + "cpu: spock\n", + "Human: paper\n", + "Human won\n", + "Cpu score: 1\n", + "Player score: 2\n" + ] + } + ], + "source": [ + "rps(gestures_bonus)" + ] + }, + { + "cell_type": "code", + "execution_count": 155, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Human wins\n" + ] + } + ], + "source": [ + "if cpu_score > player_score:\n", + " print(\"Computer wins\")\n", + "else:\n", + " print(\"Human wins\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -226,7 +446,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.1" } }, "nbformat": 4,