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..de9b7ad 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": 1, "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": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "days = 1" + ] }, { "cell_type": "markdown", @@ -58,10 +65,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while snail_position < well_height:\n", + " snail_position += daily_distance\n", + " if snail_position <= well_height:\n", + " snail_position -= nightly_distance\n", + " days += 1" + ] }, { "cell_type": "markdown", @@ -72,10 +85,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snail takes 11 days to escape the well\n" + ] + } + ], + "source": [ + "print(\"The snail takes\", days, \"days to escape the well\")" + ] }, { "cell_type": "markdown", @@ -96,10 +119,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BONUS: The snail takes 6 days to escape the well\n" + ] + } + ], + "source": [ + "days = 1\n", + "snail_position = 0\n", + "advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]\n", + "while snail_position < well_height:\n", + " snail_position += advance_cm[days-1]\n", + " if snail_position <= well_height:\n", + " snail_position -= nightly_distance\n", + " days += 1\n", + "print(\"BONUS: The snail takes\", days, \"days to escape the well\")" + ] }, { "cell_type": "markdown", @@ -111,10 +152,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The maximum displacement in one day is 57\n", + "The minimum displacement in one day is 1\n" + ] + } + ], + "source": [ + "# Accumulated displacements before the last day\n", + "accumDisp = sum(advance_cm[:days-1]) - nightly_distance*(days-1)\n", + "# Snail's displacement on the last day\n", + "lastDayDisp = well_height - accumDisp\n", + "# Compute the maximum and minimum (no slide for the last day)\n", + "maxDailyDisp = max([max(advance_cm[:days-1])-nightly_distance,lastDayDisp])\n", + "minDailyDisp = min([min(advance_cm[:days-1])-nightly_distance,lastDayDisp])\n", + "print(\"The maximum displacement in one day is\", maxDailyDisp)\n", + "print(\"The minimum displacement in one day is\", minDailyDisp)" + ] }, { "cell_type": "markdown", @@ -125,10 +185,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The average displacement is 20.83\n" + ] + } + ], + "source": [ + "# Sum of displacements is well height\n", + "meanDisp = well_height/days\n", + "print(\"The average displacement is\", round(meanDisp,2))" + ] }, { "cell_type": "markdown", @@ -139,10 +211,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The standard deviation of displacements is 17.83\n" + ] + } + ], + "source": [ + "# Store the accumulated square distance to mean\n", + "accumDist2Mean = 0\n", + "for day in range(days):\n", + " if day < days:\n", + " accumDist2Mean += (advance_cm[day]-nightly_distance-meanDisp)**2\n", + " else:\n", + " accumDist2Mean += (lastDayDisp-meanDisp)**2\n", + "stdDev = (accumDist2Mean/days)**(0.5)\n", + "print(\"The standard deviation of displacements is\", round(stdDev,2))" + ] } ], "metadata": { @@ -161,7 +251,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "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..b882741 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,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# It is assumed gandalf and saruman have same length\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", + "# I don't see the point of creating the spells variable" + ] }, { "cell_type": "markdown", @@ -64,10 +69,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 +86,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for clash in range(len(gandalf)):\n", + " if gandalf[clash] > saruman[clash]:\n", + " gandalf_wins += 1\n", + " elif gandalf[clash] < saruman[clash]:\n", + " saruman_wins += 1" + ] }, { "cell_type": "markdown", @@ -93,10 +107,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 +157,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# It is assumed gandalf and saruman have same length\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", + "POWER = {'Fireball': 50, 'Lightning bolt': 40, 'Magic arrow': 10, 'Black Tentacles': 25, 'Contagion': 45}\n", + "# I don't see the point of creating the spells variable" + ] }, { "cell_type": "markdown", @@ -142,10 +179,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 +196,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gandalf_power = []\n", + "saruman_power = []" + ] }, { "cell_type": "markdown", @@ -171,10 +214,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins\n" + ] + } + ], + "source": [ + "clash = 0\n", + "while ((gandalf_wins < 3) & (saruman_wins < 3)) & (clash < len(gandalf)):\n", + " gandalfPow = POWER[gandalf[clash]]\n", + " sarumanPow = POWER[saruman[clash]]\n", + " if gandalfPow > sarumanPow:\n", + " gandalf_wins += 1\n", + " saruman_wins = 0\n", + " elif gandalfPow < sarumanPow:\n", + " saruman_wins += 1\n", + " gandalf_wins = 0\n", + " clash += 1\n", + " gandalf_power.append(gandalfPow)\n", + " saruman_power.append(sarumanPow)\n", + "if gandalf_wins == 3:\n", + " print(\"Gandalf wins\")\n", + "elif saruman_wins == 3:\n", + " print(\"Saruman wins\")\n", + "else:\n", + " print(\"Tie\")" + ] }, { "cell_type": "markdown", @@ -185,10 +256,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average spell power is 37.78 for Gandalf and 32.78 for Saruman\n" + ] + } + ], + "source": [ + "gandalfMeanPow = sum(gandalf_power)/clash\n", + "sarumanMeanPow = sum(saruman_power)/clash\n", + "print(\"Average spell power is\", round(gandalfMeanPow,2), \"for Gandalf and\"\\\n", + " , round(sarumanMeanPow,2), \"for Saruman\")" + ] }, { "cell_type": "markdown", @@ -199,10 +283,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Standard deviation of spell power is 15.48 for Gandalf and 14.74 for Saruman\n" + ] + } + ], + "source": [ + "# Accumulated square distance of power to mean\n", + "gandalf_AccPow2Mean = 0\n", + "saruman_AccPow2Mean = 0\n", + "for i in range(clash):\n", + " gandalf_AccPow2Mean += (gandalf_power[i] - gandalfMeanPow)**2\n", + " saruman_AccPow2Mean += (saruman_power[i] - sarumanMeanPow)**2\n", + "gandalf_StdPow = (gandalf_AccPow2Mean/clash)**(0.5)\n", + "saruman_StdPow = (saruman_AccPow2Mean/clash)**(0.5)\n", + "print(\"Standard deviation of spell power is\", round(gandalf_StdPow,2), \"for Gandalf and\"\\\n", + " , round(saruman_StdPow,2), \"for Saruman\")" + ] } ], "metadata": { @@ -221,7 +324,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/1.-Python/3.-Bus/bus.ipynb b/1.-Python/3.-Bus/bus.ipynb index 31f09b8..123ebae 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,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "nStops = len(stops)" + ] }, { "cell_type": "markdown", @@ -67,10 +69,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "passangerHist = []\n", + "passangerHist.append(stops[0][0]-stops[0][1])\n", + "for iStop in range(nStops-1):\n", + " passangerHist.append(passangerHist[iStop] + stops[iStop+1][0] - stops[iStop+1][1])" + ] }, { "cell_type": "markdown", @@ -81,10 +88,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "maxOccupation = max(passangerHist)" + ] }, { "cell_type": "markdown", @@ -95,10 +104,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "meanOccupation = sum(passangerHist)/nStops\n", + "accDist2Mean = 0\n", + "for iStop in range(nStops):\n", + " accDist2Mean += (passangerHist[iStop] - meanOccupation)**2\n", + "stdOccupation = (accDist2Mean/nStops)**(0.5)" + ] } ], "metadata": { @@ -117,7 +132,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/1.-Python/4.-Robin-Hood/robin-hood.ipynb b/1.-Python/4.-Robin-Hood/robin-hood.ipynb index 01de29d..9aa23bc 100644 --- a/1.-Python/4.-Robin-Hood/robin-hood.ipynb +++ b/1.-Python/4.-Robin-Hood/robin-hood.ipynb @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -55,10 +55,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robin Hood has hitted an arrow with another arrow at point (4, 5)\n", + "Robin Hood has hitted an arrow with another arrow at point (5, 7)\n", + "Robin Hood has hitted an arrow with another arrow at point (-3, 2)\n", + "Robin Hood has hitted an arrow with another arrow at point (2, 2)\n" + ] + } + ], + "source": [ + "pointSet = set(points)\n", + "pointSetLen = len(pointSet)\n", + "pointLen = len(points)\n", + "if pointSetLen < pointLen:\n", + " pointSetHit = []\n", + " iSet = 0\n", + " for ptSet in pointSet:\n", + " pointSetHit.append(0)\n", + " for j in range(pointLen):\n", + " if (ptSet == points[j]):\n", + " pointSetHit[iSet] += 1\n", + " if pointSetHit[iSet] >= 2:\n", + " print(\"Robin Hood has hitted an arrow with another arrow at point\", ptSet)\n", + " iSet += 1\n", + "# An improvement could be made by breaking the loop in j (once I know the position has been hitted twice at least)\n", + "# To do that break doesn't work because it kills both nested loops (a function with a proper return would do it?)\n", + "# Anyway, being pointSetHit computed as it is above, it also serves the purpose for calculating issue 2 below" + ] }, { "cell_type": "markdown", @@ -70,10 +99,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Q1': 10, 'Q2': 6, 'Q3': 2, 'Q4': 2}\n" + ] + } + ], + "source": [ + "quadrantCount = {'Q1':0,'Q2':0,'Q3':0,'Q4':0}\n", + "iSet = 0\n", + "for ptSet in pointSet:\n", + " if (ptSet[0] != 0) & (ptSet[1] != 0):\n", + " if (ptSet[0] > 0) & (ptSet[1] > 0):\n", + " quadrantCount['Q1'] += pointSetHit[iSet]\n", + " elif (ptSet[0] > 0) & (ptSet[1] < 0):\n", + " quadrantCount['Q4'] += pointSetHit[iSet]\n", + " elif (ptSet[1] > 0):\n", + " quadrantCount['Q2'] += pointSetHit[iSet]\n", + " else:\n", + " quadrantCount['Q3'] += pointSetHit[iSet]\n", + " iSet += 1\n", + "print(quadrantCount)" + ] }, { "cell_type": "markdown", @@ -88,10 +140,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Best arrow hit/hits is/are found at:\n", + "(0, -2)\n", + "(0, 2)\n" + ] + } + ], + "source": [ + "def centerDist(x,y):\n", + " return (x**2 + y**2)**(0.5)\n", + "arrowDist = []\n", + "for ptSet in pointSet:\n", + " arrowDist.append(centerDist(ptSet[0],ptSet[1]))\n", + "minDist = min(arrowDist)\n", + "# I found the solution below online but would like to discuss it to understand the syntax completely\n", + "indexMin = [i for i, x in enumerate(arrowDist) if x == minDist]\n", + "print(\"Best arrow hit/hits is/are found at:\")\n", + "pointSetList = list(pointSet)\n", + "for i in range(len(indexMin)):\n", + " print(pointSetList[indexMin[i]])" + ] }, { "cell_type": "markdown", @@ -103,10 +178,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "There are 2 arrows don't hitting target\n" + ] + } + ], + "source": [ + "nArrowOut = 0\n", + "iSet = 0\n", + "for ptSet in pointSet:\n", + " if centerDist(ptSet[0],ptSet[1]) > 9:\n", + " nArrowOut += pointSetHit[iSet]\n", + " iSet += 1\n", + "print(\"There are\", nArrowOut, \"arrows don't hitting target\")" + ] } ], "metadata": { @@ -125,7 +216,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa..5543cf3 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": 1, "metadata": {}, "outputs": [], "source": [ @@ -53,10 +53,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# So far minimum temperature is not a requirement\n", + "minTemp = min(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -67,10 +70,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "maxTemp = max(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -81,10 +86,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "tempAbove = []\n", + "for elem in temperatures_C:\n", + " if elem >= 70:\n", + " tempAbove.append(elem)" + ] }, { "cell_type": "markdown", @@ -95,10 +105,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "nMeasures = len(temperatures_C)\n", + "meanTemp = sum(temperatures_C)/nMeasures\n", + "# print(meanTemp)" + ] }, { "cell_type": "markdown", @@ -109,10 +123,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "# Estimation based on mean value of neighbor meassurements\n", + "failIndex = 3\n", + "temperatures_C[failIndex] = (temperatures_C[failIndex-1] + temperatures_C[failIndex+1])*0.5" + ] }, { "cell_type": "markdown", @@ -128,10 +146,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temperatures_F = []\n", + "# Question about speed --> isn't better to allocate the size of variable temperatures_F? How do you do it?\n", + "for elem in temperatures_C:\n", + " temperatures_F.append(1.8*elem+32)" + ] }, { "cell_type": "markdown", @@ -150,10 +173,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cooling system needs to be replaced\n" + ] + } + ], + "source": [ + "# Inspecting values, I observe that the failed measurement won't affect tempAbove nor maxTemp variables\n", + "# However, average has to be computed again\n", + "meanTemp = sum(temperatures_C)/nMeasures\n", + "if ((len(tempAbove) > 4) | (maxTemp > 80)) | (meanTemp > 65):\n", + " print(\"Cooling system needs to be replaced\")\n", + "else:\n", + " print(\"Cooling system is OK\")" + ] }, { "cell_type": "markdown", @@ -175,10 +214,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "hoursAbove = []\n", + "for i in range(nMeasures):\n", + " if temperatures_C[i] >= 70:\n", + " hoursAbove.append(i)\n", + "#print(hoursAbove)" + ] }, { "cell_type": "markdown", @@ -189,10 +234,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Consecutive count reached\n" + ] + } + ], + "source": [ + "consecCount = 0\n", + "for i in range(len(hoursAbove)-1):\n", + " if consecCount <= 4:\n", + " if (hoursAbove[i+1] - hoursAbove[i]) < 1.5:\n", + " consecCount += 1\n", + " else:\n", + " consecCount = 0\n", + " else:\n", + " print(\"Consecutive count reached\")\n", + " break\n", + "#print(consecCount)" + ] }, { "cell_type": "markdown", @@ -204,10 +269,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cooling system needs to be replaced\n" + ] + } + ], + "source": [ + "# Already know that the maximum temperature has been reached so decision won't change\n", + "# Anyway, the criteria is modified according to new computed variable consecCount\n", + "if ((consecCount > 4) | (maxTemp > 80)) | (meanTemp > 65):\n", + " print(\"Cooling system needs to be replaced\")\n", + "else:\n", + " print(\"Cooling system is OK\")" + ] }, { "cell_type": "markdown", @@ -218,10 +298,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Mean values are related the same as temperature conversion\n" + ] + } + ], + "source": [ + "meanTempF = sum(temperatures_F)/nMeasures\n", + "if abs((meanTempF-32)/1.8 - meanTemp) < 1e-6:\n", + " print(\"Mean values are related the same as temperature conversion\")" + ] }, { "cell_type": "markdown", @@ -232,10 +324,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The relation between standard deviations is the tangent of the\n", + "linear relation between temperatures, that is 1.8\n" + ] + } + ], + "source": [ + "accumTempC = 0\n", + "accumTempF = 0\n", + "for iMeasure in range(nMeasures):\n", + " accumTempC += (temperatures_C[iMeasure] - meanTemp)**2\n", + " accumTempF += (temperatures_F[iMeasure] - meanTempF)**2\n", + "stdTempC = (accumTempC/nMeasures)**(0.5)\n", + "stdTempF = (accumTempF/nMeasures)**(0.5)\n", + "print(\"The relation between standard deviations is the tangent of the\\n\"\\\n", + " \"linear relation between temperatures, that is\", round(stdTempF/stdTempC,2))" + ] } ], "metadata": { @@ -254,7 +365,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.6" } }, "nbformat": 4, diff --git a/1.-Python/6.-Rock-Paper-Scissors/images/rpsls.jpg b/1.-Python/6.-Rock-Paper-Scissors/images/rpsls.jpg new file mode 100644 index 0000000..82490bc Binary files /dev/null and b/1.-Python/6.-Rock-Paper-Scissors/images/rpsls.jpg differ diff --git a/1.-Python/6.-Rock-Paper-Scissors/rock-paper-scissors.ipynb b/1.-Python/6.-Rock-Paper-Scissors/rock-paper-scissors.ipynb new file mode 100644 index 0000000..1f96dc3 --- /dev/null +++ b/1.-Python/6.-Rock-Paper-Scissors/rock-paper-scissors.ipynb @@ -0,0 +1,408 @@ +{ + "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": [ + "from random import choice" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 2. Create a list that includes the 3 possible gesture options of the game: 'rock', 'paper' or 'scissors'. Store the list in a variable called `gestures`." + ] + }, + { + "cell_type": "code", + "execution_count": 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": [ + "Insert an odd number of rounds (best-of-series format) 3\n" + ] + } + ], + "source": [ + "def inputOddNumber(message):\n", + " while True:\n", + " try:\n", + " userInput = int(input(message))\n", + " except ValueError:\n", + " print(\"Please give me an integer!\")\n", + " else:\n", + " if userInput % 2 == 0:\n", + " print(\"Try again with an odd number!\")\n", + " else:\n", + " return userInput\n", + "\n", + "n_rounds = inputOddNumber(\"Insert an odd number of rounds (best-of-series format) \")" + ] + }, + { + "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 = 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": [ + "# I use \"gestures\" list global variable\n", + "def randGest():\n", + " return gestures[choice(range(len(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": 7, + "metadata": {}, + "outputs": [], + "source": [ + "def inputGesture(message):\n", + " while True:\n", + " userGesture = input(message)\n", + " if (userGesture == 'rock') | (userGesture == 'paper') | (userGesture == 'scissors'):\n", + " return userGesture\n", + " else:\n", + " print(\"Your option must be either rock or paper or scissors\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 8. Define a function that checks who won a round. \n", + "The function should return 0 if there is a tie, 1 if the computer wins and 2 if the player wins." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def rockPaperScissors(player,cpu):\n", + " if player == cpu:\n", + " return 0\n", + " else:\n", + " if ((player == 'rock') & (cpu == 'scissors')) | ((player == 'scissors') & (cpu == 'paper')) |\\\n", + " ((player == 'paper') & (cpu == 'rock')):\n", + " return 2\n", + " else:\n", + " return 1" + ] + }, + { + "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": 9, + "metadata": {}, + "outputs": [], + "source": [ + "# I use cpu_score and player_score global variables\n", + "def scoreRound(player,cpu,status):\n", + " scorePyr = player_score\n", + " scoreCpu = cpu_score\n", + " if status == 2:\n", + " scorePyr += 1\n", + " print(\"Cpu choice is '\", cpu, \"' while Player is '\", player, \"' --> Player wins round\")\n", + " return (scorePyr,scoreCpu)\n", + " elif status == 1:\n", + " scoreCpu += 1\n", + " print(\"Cpu choice is '\", cpu, \"' while Player is '\", player, \"' --> Cpu wins round\")\n", + " return (scorePyr,scoreCpu)\n", + " else:\n", + " print(\"Cpu choice is '\", cpu, \"' while Player is '\", player, \"' --> Tied round\")\n", + " return (scorePyr,scoreCpu)" + ] + }, + { + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Type your choice: rock, paper or scissors? rock\n", + "Cpu choice is ' rock ' while Player is ' rock ' --> Tied round\n", + "Type your choice: rock, paper or scissors? rock\n", + "Cpu choice is ' scissors ' while Player is ' rock ' --> Player wins round\n", + "Type your choice: rock, paper or scissors? paper\n", + "Cpu choice is ' scissors ' while Player is ' paper ' --> Cpu wins round\n" + ] + } + ], + "source": [ + "roundCounter = 1\n", + "while (roundCounter <= n_rounds) & ((player_score < rounds_to_win) & (cpu_score < rounds_to_win)):\n", + " cpu_choice = randGest()\n", + " player_choice = inputGesture(\"Type your choice: rock, paper or scissors? \")\n", + " # call the game function and collect the status!\n", + " roundStatus = rockPaperScissors(player_choice,cpu_choice)\n", + " # show the round results and update scores!\n", + " (player_score,cpu_score) = scoreRound(player_choice,cpu_choice,roundStatus)\n", + " roundCounter += 1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### 11. Print the winner of the game based on who won more rounds.\n", + "Remember that the game might be tied. " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Game tied, let's play again!\n" + ] + } + ], + "source": [ + "if player_score == cpu_score:\n", + " print(\"Game tied, let's play again!\")\n", + "elif player_score > cpu_score:\n", + " print(\"You won the game :) !\")\n", + "else:\n", + " print(\"Computer wins :( !\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Bonus: Rock, Paper, Scissors, Lizard & Spock\n", + "![](images/rpsls.jpg)\n", + "\n", + "In this challenge, you need to improve the previous game by adding two new options. To know more about the rules of the improved version of rock, paper, scissors, check this [link](http://www.samkass.com/theories/RPSSL.html). \n", + "\n", + "In addition, you will also need to improve how the game interacts with the player: the number of rounds to play, which must be an odd number, will be requested to the user until a valid number is entered. Define a new function to make that request.\n", + "\n", + "**Hint**: Try to reuse the code that you already coded in the previous challenge. If your code is efficient, this bonus will only consist of simple modifications to the original game." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Type your choice: rock, paper, scissors, lizard or spock? spock\n", + "Cpu choice is ' paper ' while Player is ' spock ' --> Cpu wins round\n", + "Type your choice: rock, paper, scissors, lizard or spock? spock\n", + "Cpu choice is ' spock ' while Player is ' spock ' --> Tied round\n", + "Type your choice: rock, paper, scissors, lizard or spock? spock\n", + "Cpu choice is ' scissors ' while Player is ' spock ' --> Player wins round\n", + "Game tied, let's play again!\n" + ] + } + ], + "source": [ + "# From original game, update gestures\n", + "gestures = ['rock','paper','scissors','lizard','spock']\n", + "\n", + "# The function \"inputOddNumber\" already considers the improved interaction\n", + "\n", + "# Restart counters: cpu and player round scores\n", + "cpu_score = 0\n", + "player_score = 0\n", + "\n", + "# Recall \"randGest()\" function will work given \"gestures\" update\n", + "\n", + "# Redefine \"inputGesture\" below to consider lizard and spock valid\n", + "def inputGesture(message):\n", + " while True:\n", + " userGesture = input(message)\n", + " if (userGesture == 'rock') | (userGesture == 'paper') | (userGesture == 'scissors') |\\\n", + " (userGesture == 'lizard') | (userGesture == 'spock'):\n", + " return userGesture\n", + " else:\n", + " print(\"Your option must be either rock or paper or scissors\")\n", + "\n", + "# Redefine the game logic and hail Sam Kass 8)\n", + "def rockPaperScissorsLizardSpock(player,cpu):\n", + " if player == cpu:\n", + " return 0\n", + " else:\n", + " if ((player == 'scissors') & (cpu == 'paper')) | ((player == 'paper') & (cpu == 'rock')) | \\\n", + " ((player == 'rock') & (cpu == 'lizard')) | ((player == 'lizard') & (cpu == 'spock')) | \\\n", + " ((player == 'spock') & (cpu == 'scissors')) | ((player == 'scissors') & (cpu == 'lizard')) | \\\n", + " ((player == 'lizard') & (cpu == 'paper')) | ((player == 'paper') & (cpu == 'spock')) | \\\n", + " ((player == 'spock') & (cpu == 'rock')) | ((player == 'rock') & (cpu == 'scissors')):\n", + " return 2\n", + " else:\n", + " return 1\n", + " \n", + "# \"scoreRound()\" function works the same. Initialize roundCounter and play!\n", + "roundCounter = 1\n", + "while (roundCounter <= n_rounds) & ((player_score < rounds_to_win) & (cpu_score < rounds_to_win)):\n", + " cpu_choice = randGest()\n", + " player_choice = inputGesture(\"Type your choice: rock, paper, scissors, lizard or spock? \")\n", + " # call the new game function and collect the status!\n", + " roundStatus = rockPaperScissorsLizardSpock(player_choice,cpu_choice)\n", + " # show the round results and update scores!\n", + " (player_score,cpu_score) = scoreRound(player_choice,cpu_choice,roundStatus)\n", + " roundCounter += 1\n", + " \n", + "if player_score == cpu_score:\n", + " print(\"Game tied, let's play again!\")\n", + "elif player_score > cpu_score:\n", + " print(\"You won the game :) !\")\n", + "else:\n", + " print(\"Computer wins :( !\")" + ] + } + ], + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/2.-Statistics/week1_gradQuizSetNumSum.JPG b/2.-Statistics/week1_gradQuizSetNumSum.JPG new file mode 100644 index 0000000..9637205 Binary files /dev/null and b/2.-Statistics/week1_gradQuizSetNumSum.JPG differ diff --git a/2.-Statistics/week1_pracQuizSum.JPG b/2.-Statistics/week1_pracQuizSum.JPG new file mode 100644 index 0000000..95a8665 Binary files /dev/null and b/2.-Statistics/week1_pracQuizSum.JPG differ diff --git a/2.-Statistics/week1_practQuizNum.JPG b/2.-Statistics/week1_practQuizNum.JPG new file mode 100644 index 0000000..055ebdf Binary files /dev/null and b/2.-Statistics/week1_practQuizNum.JPG differ diff --git a/2.-Statistics/week1_practQuizSets.jpg b/2.-Statistics/week1_practQuizSets.jpg new file mode 100644 index 0000000..da5be26 Binary files /dev/null and b/2.-Statistics/week1_practQuizSets.jpg differ diff --git a/2.-Statistics/week2_gradQuizCart&Fun.JPG b/2.-Statistics/week2_gradQuizCart&Fun.JPG new file mode 100644 index 0000000..4089cc4 Binary files /dev/null and b/2.-Statistics/week2_gradQuizCart&Fun.JPG differ diff --git a/2.-Statistics/week2_practQuizCartPlane.JPG b/2.-Statistics/week2_practQuizCartPlane.JPG new file mode 100644 index 0000000..80d7a8b Binary files /dev/null and b/2.-Statistics/week2_practQuizCartPlane.JPG differ diff --git a/2.-Statistics/week2_practQuizFun.JPG b/2.-Statistics/week2_practQuizFun.JPG new file mode 100644 index 0000000..2da63d2 Binary files /dev/null and b/2.-Statistics/week2_practQuizFun.JPG differ diff --git a/2.-Statistics/week3_gradQuizFunExpLog.JPG b/2.-Statistics/week3_gradQuizFunExpLog.JPG new file mode 100644 index 0000000..5e673e7 Binary files /dev/null and b/2.-Statistics/week3_gradQuizFunExpLog.JPG differ diff --git a/2.-Statistics/week3_practQuizExpLog.JPG b/2.-Statistics/week3_practQuizExpLog.JPG new file mode 100644 index 0000000..bea69da Binary files /dev/null and b/2.-Statistics/week3_practQuizExpLog.JPG differ diff --git a/2.-Statistics/week3_practQuizTangLin.JPG b/2.-Statistics/week3_practQuizTangLin.JPG new file mode 100644 index 0000000..01d68c5 Binary files /dev/null and b/2.-Statistics/week3_practQuizTangLin.JPG differ diff --git a/2.-Statistics/week4_practQuizBayesBinomial.JPG b/2.-Statistics/week4_practQuizBayesBinomial.JPG new file mode 100644 index 0000000..90a1dfb Binary files /dev/null and b/2.-Statistics/week4_practQuizBayesBinomial.JPG differ diff --git a/2.-Statistics/week4_practQuizProbConc.JPG b/2.-Statistics/week4_practQuizProbConc.JPG new file mode 100644 index 0000000..f943d26 Binary files /dev/null and b/2.-Statistics/week4_practQuizProbConc.JPG differ diff --git a/2.-Statistics/week4_practQuizProbSolv.JPG b/2.-Statistics/week4_practQuizProbSolv.JPG new file mode 100644 index 0000000..bf24a1b Binary files /dev/null and b/2.-Statistics/week4_practQuizProbSolv.JPG differ diff --git a/robot.md b/robot.md new file mode 100644 index 0000000..e69de29