diff --git a/bus/bus.ipynb b/bus/bus.ipynb index 81779ce..dfffd63 100644 --- a/bus/bus.ipynb +++ b/bus/bus.ipynb @@ -32,53 +32,136 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 47, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter num of onboarded riders, *Enter 'end' as finished: 40\n", + "Enter number of passengers offboarded:0\n", + "Enter num of onboarded riders, *Enter 'end' as finished: 12\n", + "Enter number of passengers offboarded:15\n", + "Enter num of onboarded riders, *Enter 'end' as finished: 9\n", + "Enter number of passengers offboarded:34\n", + "Enter num of onboarded riders, *Enter 'end' as finished: 34\n", + "Enter number of passengers offboarded:46\n", + "Enter num of onboarded riders, *Enter 'end' as finished: end\n", + "[(40, 0), (12, 15), (9, 34), (34, 46)]\n" + ] + } + ], "source": [ "# variables\n", - "\n" + "stops=[]\n", + "in_pass=0\n", + "\n", + "while in_pass != 'end':\n", + " in_pass=input(\"Enter num of onboarded riders, *Enter 'end' as finished: \")\n", + " if in_pass != 'end':\n", + " in_pass=int(in_pass)\n", + " out_pass=int(input(\"Enter number of passengers offboarded:\"))\n", + " bus_stop = (in_pass,out_pass)\n", + " stops.append(bus_stop)\n", + " else:\n", + " break\n", + "\n", + "print(stops)\n" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 48, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# 1. Calculate the number of stops.\n", - "\n" + "# 1. Calculate the number of stops\n", + "len(stops)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 52, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Number of passengers in each stops: [40, 37, 12, 0]\n" + ] + } + ], "source": [ "# 2. Assign a variable a list whose elements are the number of passengers in each stop: \n", "# Each item depends on the previous item in the list + in - out.\n", - "\n" + "occupation=[]\n", + "num_pass=0\n", + "\n", + "for i in range(0,(len(stops))):\n", + " num_pass += stops[i][0]- stops[i][1]\n", + " occupation.append(num_pass)\n", + "\n", + "print(\"Number of passengers in each stops:\", occupation)\n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max occupation is 40 passengers.\n" + ] + } + ], "source": [ "# 3. Find the maximum occupation of the bus.\n", + "\n", + "\n", + "print(\"Max occupation is\", max(occupation),\"passengers.\")\n", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 57, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average occupation: 22.25\n", + "Standard deviation: 16.82817577754642\n" + ] + } + ], "source": [ "# 4. Calculate the average occupation. And the standard deviation.\n", - "\n" + "\n", + "\n", + "mean_o=(sum(occupation)/len(occupation))\n", + "stdv_o=(sum(((i-mean_o)**2) for i in occupation)/len(occupation))**0.5\n", + "\n", + "print(\"Average occupation: \",mean_o)\n", + "print(\"Standard deviation: \",stdv_o)" ] }, { @@ -105,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/crusine.md b/crusine.md new file mode 100644 index 0000000..e69de29 diff --git a/duel/duel.ipynb b/duel/duel.ipynb index 4398d88..fac3957 100644 --- a/duel/duel.ipynb +++ b/duel/duel.ipynb @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -45,31 +45,107 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ - "# Assign 0 to each variable that stores the victories\n" + "# Assign 0 to each variable that stores the victories\n", + "gandalf_win=0\n", + "saruman_win=0\n", + "tie=0" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 23, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Saruman', 'Saruman', 'Gandalf', 'Saruman', 'Gandalf', 'Gandalf', 'Saruman', 'Gandalf', 'Gandalf']\n" + ] + } + ], "source": [ - "# Execution of spell clashes\n" + "# Execution of spell clashes\n", + "for i in range(0,(len(gandalf)-1)):\n", + " if gandalf[i]>saruman[i]:\n", + " gandalf_win+=1\n", + " elif saruman[i]>gandalf[i]:\n", + " saruman_win+=1\n", + " else :\n", + " tie+=1\n", + " " ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], + "execution_count": 8, + "metadata": { + "scrolled": false + }, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'winner' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m# Print the result based on the winner.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwinner\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\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[0m", + "\u001b[0;31mNameError\u001b[0m: name 'winner' is not defined" + ] + } + ], "source": [ "# We check who has won, do not forget the possibility of a draw.\n", "# Print the result based on the winner.\n", - "\n" + "\n", + "if gandalf_win>saruman_win:\n", + " print(\"Gandalf wins!\")\n", + "elif saruman_win>gandalf_win:\n", + " print(\"Saruman wins!\")\n", + "else:\n", + " print(\"Tie!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins!\n" + ] + } + ], + "source": [ + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "\n", + "gandalf_win=0\n", + "saruman_win=0\n", + "tie=0\n", + "\n", + "for i in range(0,(len(gandalf)-1)):\n", + " if gandalf[i]>saruman[i]:\n", + " gandalf_win+=1\n", + " elif saruman[i]>gandalf[i]:\n", + " saruman_win+=1\n", + " else :\n", + " tie+=1\n", + " \n", + "if gandalf_win>saruman_win:\n", + " print(\"Gandalf wins!\")\n", + "elif saruman_win>gandalf_win:\n", + " print(\"Saruman wins!\")\n", + "else:\n", + " print(\"Tie!\")\n" ] }, { @@ -116,7 +192,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 70, "metadata": {}, "outputs": [], "source": [ @@ -139,31 +215,74 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "# Assign spell power lists to variables\n", - "\n" + "\n", + "gandalf_sp = []\n", + "saruman_sp = []\n", + "\n", + "for i in range(0,(len(gandalf)-1)):\n", + " gandalf_sp.append(POWER[gandalf[i]])\n", + " \n", + "for i in range(0,(len(saruman)-1)):\n", + " saruman_sp.append(POWER[saruman[i]])" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 73, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins!\n" + ] + } + ], "source": [ "# 2. A sorcerer wins if he succeeds in winning 3 spell clashes in a row.\n", "\n", "\n", "# Execution of spell clashes\n", + "winner = []\n", "\n", - "\n", + "for i in range(0,(len(gandalf)-1)):\n", + " if gandalf_sp[i]>saruman_sp[i]:\n", + " winner.append('Gandalf')\n", + " elif saruman_sp[i]>gandalf_sp[i]:\n", + " winner.append('Saruman')\n", + " else :\n", + " winner.append('Tie')\n", "\n", "# check for 3 wins in a row\n", "\n", + "win=0\n", + "g_wincount=0\n", + "s_wincount=0\n", + "\n", + "while g_wincount <3 or s_wincount <3 and win!=battle-1:\n", + " if winner[win]=='Gandalf':\n", + " g_wincount +=1\n", + " elif winner[win] == 'Saruman':\n", + " s_wincount +=1\n", + " else :\n", + " pass\n", + " win+=1\n", + "\n", + "\n", + "# check the winner\n", "\n", - "# check the winner\n" + "if g_wincount==3:\n", + " print(\"Gandalf wins!\")\n", + "elif s_wincount==3:\n", + " print(\"Saruman wins!\")\n", + "else:\n", + " print(\"It's a Tie!\")" ] }, { @@ -173,7 +292,12 @@ "outputs": [], "source": [ "# 3. Average of each of the spell lists.\n", - "\n" + "\n", + "mean_g = sum(gandalf_sp)/len(gandalf_sp)\n", + "mean_s = sum(saruman_sp)/len(saruman_sp)\n", + "\n", + "print(\"Average of Gandalf's spell list:\", mean_g)\n", + "print(\"Average of Saruman's spell list:\", mean_s)\n" ] }, { @@ -183,9 +307,109 @@ "outputs": [], "source": [ "# 4. Standard deviation of each of the spell lists.\n", - "\n" + "\n", + "stdv_g = (sum(((i-mean_g) **2) for i in gandalf_sp)/len(gandalf_sp)) ** 0.5\n", + "stdv_s = (sum(((i-mean_s) **2) for i in saruman_sp)/len(saruman_sp)) ** 0.5\n", + "\n", + "print(\"Standard deviation of Gandalf's spell list:\", stdv_g)\n", + "print(\"Standard deviation of Saruman's spell list:\", stdv_s)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gandalf wins!\n", + "Average of Gandalf's spell list: 33.333333333333336\n", + "Average of Gandalf's spell list: 32.77777777777778\n", + "Standard deviation of Gandalf's spell list: 16.99673171197595\n", + "Standard deviation of Saruman's spell list: 14.740554623801778\n" + ] + } + ], + "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', 'Magic arrow', 'Fireball']\n", + "saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles', \n", + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "\n", + "gandalf_sp = []\n", + "saruman_sp = []\n", + "\n", + "for i in range(0,(len(gandalf)-1)):\n", + " gandalf_sp.append(POWER[gandalf[i]])\n", + " \n", + "for i in range(0,(len(saruman)-1)):\n", + " saruman_sp.append(POWER[saruman[i]])\n", + "\n", + "winner = []\n", + "battle = len(gandalf)\n", + "\n", + "for i in range(0,(battle-1)):\n", + " if gandalf_sp[i]>saruman_sp[i]:\n", + " winner.append('Gandalf')\n", + " elif saruman_sp[i]>gandalf_sp[i]:\n", + " winner.append('Saruman')\n", + " else :\n", + " winner.append('Tie')\n", + "\n", + "\n", + "win=0\n", + "g_wincount=0\n", + "s_wincount=0\n", + "\n", + "while g_wincount <3 or s_wincount <3 and win!=battle-1:\n", + " if winner[win]=='Gandalf':\n", + " g_wincount +=1\n", + " elif winner[win] == 'Saruman':\n", + " s_wincount +=1\n", + " else :\n", + " pass\n", + " win+=1\n", + " \n", + "if g_wincount==3:\n", + " print(\"Gandalf wins!\")\n", + "elif s_wincount==3:\n", + " print(\"Saruman wins!\")\n", + "else:\n", + " print(\"It's a Tie!\")\n", + " \n", + "\n", + "mean_g = sum(gandalf_sp)/len(gandalf_sp)\n", + "mean_s = sum(saruman_sp)/len(saruman_sp)\n", + "\n", + "print(\"Average of Gandalf's spell list:\", mean_g)\n", + "print(\"Average of Gandalf's spell list:\", mean_s)\n", + " \n", + "\n", + " \n", + "stdv_g = (sum(((i-mean_g) **2) for i in gandalf_sp)/len(gandalf_sp)) ** 0.5\n", + "stdv_s = (sum(((i-mean_s) **2) for i in saruman_sp)/len(saruman_sp)) ** 0.5\n", + "\n", + "print(\"Standard deviation of Gandalf's spell list:\", stdv_g)\n", + "print(\"Standard deviation of Saruman's spell list:\", stdv_s)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -210,7 +434,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/ingradient.md b/ingradient.md new file mode 100644 index 0000000..e69de29 diff --git a/recipe.md b/recipe.md new file mode 100644 index 0000000..2b7b6a5 --- /dev/null +++ b/recipe.md @@ -0,0 +1,7 @@ +Recipe Nashville hot chicken ** Danger! Extra Spicy + +Ingradient + +Preparation + +Cooking diff --git a/robin-hood/robin-hood.ipynb b/robin-hood/robin-hood.ipynb index b1af06b..1cb430c 100644 --- a/robin-hood/robin-hood.ipynb +++ b/robin-hood/robin-hood.ipynb @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ @@ -52,49 +52,154 @@ "points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5),\n", " (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),\n", " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2),\n", - " (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]" + " (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]\n", + "\n" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 35, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Robin Hood gets [(4, 5), (-3, 2), (5, 7), (5, 7), (2, 2)]\n" + ] + } + ], "source": [ "# 1. Robin Hood is famous for hitting an arrow with another arrow. Did you get it?\n", - "\n" + "unique=set()\n", + "robin=[]\n", + "\n", + "for (x,y) in points:\n", + " if (x,y) not in unique:\n", + " unique.add((x,y))\n", + " else:\n", + " robin.append((x,y))\n", + " \n", + "print(\"Robin Hood gets\",robin)\n" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 36, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Q1: [(4, 5), (4, 7), (4, 5), (3, 2), (5, 7), (2, 2), (5, 7), (5, 7), (2, 2), (9, 9)]\n", + "Q2: [(-5, 7), (-4, 5), (-4, 7), (-1, 3), (-3, 2), (-3, 2)]\n", + "Q3: [(-4, -5), (-8, -9)]\n", + "Q4: [(1, -3), (3, -2)]\n", + "[(0, 2), (0, -2)] lay between quarants.\n" + ] + } + ], "source": [ "# 2. Calculate how many arrows have fallen in each quadrant.\n", - "\n" + "\n", + "q1=[]\n", + "q2=[]\n", + "q3=[]\n", + "q4=[]\n", + "between_quarant=[]\n", + "\n", + "for (x,y) in points:\n", + " if x > 0 and y > 0:\n", + " q1.append((x,y))\n", + " elif x <0 and y > 0:\n", + " q2.append((x,y))\n", + " elif x <0 and y < 0:\n", + " q3.append((x,y))\n", + " elif x >0 and y < 0:\n", + " q4.append((x,y))\n", + " else :\n", + " between_quarant.append((x,y))\n", + "\n", + "\n", + "print(\"Q1:\",q1)\n", + "print(\"Q2:\",q2)\n", + "print(\"Q3:\",q3)\n", + "print(\"Q4:\",q4)\n", + "print(between_quarant, \"lay between quarants.\")\n", + " \n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 140, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Closest to centre: (0, 2)\n", + "Distance: 2.0\n", + "Closest to centre: (0, -2)\n", + "Distance: 2.0\n" + ] + } + ], "source": [ "# 3. Find the point closest to the center. Calculate its distance to the center\n", "# Defining a function that calculates the distance to the center can help.\n", + "\n", + "points = [(4, 5), (-0, 2), (4, 7), (1, -3), (3, -2), (4, 5),\n", + " (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2),\n", + " (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2),\n", + " (5, 7), (5, 7), (2, 2), (9, 9), (-8, -9)]\n", + "\n", + "\n", + "def find_d(x,y):\n", + " distance=(x**2+y**2)**0.5\n", + " return round(distance,2)\n", + "\n", + "for x,y in points:\n", + " if x==0 or y==0:\n", + " print(\"Closest to centre:\",(x,y)) \n", + " print(\"Distance:\",find_d(x,y))\n", + "\n", + " \n", + "\n", + "\n", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 155, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Arrows hit target: [(4, 5), (0, 2), (4, 7), (1, -3), (3, -2), (4, 5), (3, 2), (5, 7), (-5, 7), (2, 2), (-4, 5), (0, -2), (-4, 7), (-1, 3), (-3, 2), (-4, -5), (-3, 2), (5, 7), (5, 7), (2, 2)] \n", + "\n", + "20 out of 22 arrows must be picked up in the forest.\n" + ] + } + ], "source": [ "# 4. If the target has a radius of 9, calculate the number of arrows that \n", "# must be picked up in the forest.\n", - "\n" + "\n", + "target=[]\n", + "\n", + "for x,y in points:\n", + " all_d.append(find_d(x,y))\n", + " if find_d(x,y) < 9:\n", + " target.append((x,y))\n", + " \n", + "print(\"Arrows hit target:\", target,\"\\n\")\n", + "print(len(target),'out of',len(points), \"arrows must be picked up in the forest.\")\n" ] }, { @@ -121,7 +226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/robot.md b/robot.md new file mode 100644 index 0000000..e69de29 diff --git "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" index f13735d..c829ca5 100644 --- "a/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" +++ "b/rock\342\200\223paper\342\200\223scissors/rock-paper-scissors.ipynb" @@ -25,51 +25,173 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 234, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Win 3 games out of 5 to win the game!\n", + "Paper, Scissors, Stone? paper, you asshole\n", + "Paper, Scissors, Stone? paper\n", + "Master gets a point.\n", + "You: paper VS Master: Scissors\n", + "Your points: 0 | Master's points: 1\n", + "Paper, Scissors, Stone? paper\n", + "You gets a point.\n", + "You: paper VS Master: Paper\n", + "Your points: 1 | Master's points: 1\n", + "Paper, Scissors, Stone? scissors\n", + "You gets a point.\n", + "You: scissors VS Master: Paper\n", + "Your points: 2 | Master's points: 1\n", + "Paper, Scissors, Stone? stone\n", + "You gets a point.\n", + "You: stone VS Master: Scissors\n", + "Your points: 3 | Master's points: 1\n", + "Paper, Scissors, Stone? stone\n", + "You gets a point.\n", + "You: stone VS Master: Scissors\n", + "Your points: 4 | Master's points: 1\n", + "You win!\n" + ] + } + ], "source": [ "# Import the choice function of the random module\n", "# https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list\n", + "import random\n", "\n", "# Assign to a list the 3 possible options: 'stone', 'paper' or 'scissors'.\n", + "options=[\"Stone\",\"Paper\",\"Scissors\"]\n", "\n", "# Assign a variable to the maximum number of games: 1, 3, 5, etc ...\n", + "max_game = 5\n", "\n", "# Assign a variable to the number of games a player must win to win.\n", "# Preferably the value will be based on the number of maximum games\n", + "win = 3\n", "\n", "# Define a function that randomly returns one of the 3 options.\n", "# This will correspond to the play of the machine. Totally random.\n", "\n", + "def random_options(options):\n", + " return random.choice(options)\n", "\n", "# Define a function that asks your choice: 'stone', 'paper' or 'scissors'\n", + " \n", + "def your_choice():\n", + " Enter=\"\"\n", + " while Enter==False or Enter.upper()!=\"PAPER\" and Enter.upper()!=\"SCISSORS\" and Enter.upper()!=\"STONE\":\n", + " Enter=input(\"Paper, Scissors, Stone? \")\n", + " return Enter\n", + " \n", + " \n", "# you should only allow one of the 3 options. This is defensive programming.\n", "# If it is not stone, paper or scissors keep asking until it is.\n", "\n", "\n", + "\n", "# Define a function that resolves a combat.\n", "# Returns 0 if there is a tie, 1 if the machine wins, 2 if the human player wins\n", "\n", + "def combat(computer,player):\n", + " if computer==player:\n", + " return 0\n", + " elif (computer.upper()==\"PAPER\" and player.upper()==\"STONE\"):\n", + " return 1\n", + " elif (computer.upper()==\"SCISSORS\" and player.upper()==\"PAPER\"):\n", + " return 1 \n", + " elif (computer.upper()==\"STONE\" and player.upper()==\"SCISSORS\"):\n", + " return 1\n", + " else:\n", + " return 2\n", " \n", "# Define a function that shows the choice of each player and the state of the game\n", "# This function should be used every time accumulated points are updated\n", "\n", + "def state(player,computer,result,player_win,computer_win):\n", + " print(\"You:\",player,\"VS Master:\",computer)\n", + " if result==1 or result==2:\n", + " print(\"Your points:\",player_win,\"| Master's points: \",computer_win)\n", + "\n", " \n", "# Create two variables that accumulate the wins of each participant\n", - "\n", + "computer_win=0\n", + "player_win=0\n", "\n", "# Create a loop that iterates while no player reaches the minimum of wins\n", "# necessary to win. Inside the loop solves the play of the\n", "# machine and ask the player's. Compare them and update the value of the variables\n", "# that accumulate the wins of each participant.\n", "\n", + "count=0\n", + "\n", + "print(\"Win 3 games out of 5 to win the game!\")\n", + "\n", + "for i in range(max_game):\n", + " computer=random_options(options)\n", + " player=your_choice()\n", + " result=combat(computer,player)\n", + " if result==1:\n", + " computer_win+=1\n", + " print(\"Master gets a point.\")\n", + " elif result==2:\n", + " player_win+=1\n", + " print(\"You get a point.\")\n", + " else:\n", + " print(\"Tie!\")\n", + " state(player,computer,result,player_win,computer_win)\n", "\n", " \n", "# Print by console the winner of the game based on who has more accumulated wins\n", - " " + " \n", + "if player_win>=win:\n", + " print(\"You win! :-)\")\n", + "else:\n", + " print(\"You lose!\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -93,60 +215,176 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 277, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the number of games (Must be ODD number):asdfjk\n", + "Enter the number of games (Must be ODD number):5\n", + "Get 3 points to win the game!\n", + "Paper, Scissors, Stone, Lizard, Spock? spock\n", + "Tie!\n", + "You: spock VS Master: Spock\n", + "Your points: 0 | Master's points: 0\n", + "Paper, Scissors, Stone, Lizard, Spock? spock\n", + "Tie!\n", + "You: spock VS Master: Spock\n", + "Your points: 0 | Master's points: 0\n", + "Paper, Scissors, Stone, Lizard, Spock? spock\n", + "Master gets a point.\n", + "You: spock VS Master: Paper\n", + "Your points: 0 | Master's points: 1\n", + "Paper, Scissors, Stone, Lizard, Spock? lizard\n", + "Master gets a point.\n", + "You: lizard VS Master: Scissors\n", + "Your points: 0 | Master's points: 2\n", + "Paper, Scissors, Stone, Lizard, Spock? stone\n", + "Tie!\n", + "You: stone VS Master: Stone\n", + "Your points: 0 | Master's points: 2\n", + "You lose!\n" + ] + } + ], "source": [ "# Import the choice function of the random module\n", - "\n", + "import random\n", "\n", "# Define a function that asks for an odd number on the keyboard, until it is not valid\n", "# will keep asking\n", "\n", + "def max_games():\n", + " odd=\"\"\n", + " check_int=False\n", + " while odd==False or check_int==False or int(odd)%2!=1:\n", + " odd=input(\"Enter the number of games (Must be ODD number):\")\n", + " try:\n", + " val = int(odd)\n", + " check_int=True\n", + " except ValueError:\n", + " check_int=False\n", + " \n", + " return odd\n", "\n", "# Assign a list of 5 possible options.\n", "\n", + "options=[\"Stone\",\"Paper\",\"Scissors\",\"Lizard\",\"Spock\"]\n", "\n", "# Assign a variable to the maximum number of games: 1, 3, 5, etc ...\n", "# This time the previously defined function is used\n", "\n", + "num_games=max_games()\n", "\n", "# Assign a variable to the number of games a player must win to win.\n", "# Preferably the value will be based on the number of maximum games\n", "\n", + "win=round(int(num_games)*2/3)\n", "\n", "# Define a function that randomly returns one of the 5 options.\n", "# This will correspond to the play of the machine. Totally random.\n", "\n", + "def random_options(options):\n", + " return random.choice(options)\n", "\n", "# Define a function that asks your choice between 5\n", "# you should only allow one of the 5 options. This is defensive programming.\n", "# If it is not valid, keep asking until it is valid.\n", "\n", "\n", + "def your_choice():\n", + " Enter=\"\"\n", + " while Enter==False or Enter.upper()!=\"STONE\" and Enter.upper()!=\"PAPER\" and Enter.upper()!=\"SCISSORS\" and Enter.upper()!=\"LIZARD\" and Enter.upper()!=\"SPOCK\":\n", + " Enter=input(\"Paper, Scissors, Stone, Lizard, Spock? \")\n", + " return Enter\n", + "\n", "# Define a function that resolves a combat.\n", "# Returns 0 if there is a tie, 1 if the machine wins, 2 if the human player wins\n", "# Now there are more options\n", " \n", - "\n", + "def combat(computer,player):\n", + " if computer.upper()==player.upper():\n", + " return 0\n", + " elif (computer.upper()==\"PAPER\" or \"SPORK\" and player.upper()==\"STONE\"):\n", + " return 1\n", + " elif (computer.upper()==\"SCISSORS\" or \"LIZARD\" and player.upper()==\"PAPER\"):\n", + " return 1 \n", + " elif (computer.upper()==\"STONE\" or \"SPORK\" and player.upper()==\"SCISSORS\"):\n", + " return 1\n", + " elif (computer.upper()==\"SCISSORS\" or \"STONE\" and player.upper()==\"LIZARD\"):\n", + " return 1 \n", + " elif (computer.upper()==\"PAPER\" or \"LIZARD\" and player.upper()==\"SPORK\"):\n", + " return 1\n", + " else:\n", + " return 2\n", + " \n", " \n", "# Define a function that shows the choice of each player and the state of the game\n", "# This function should be used every time accumulated points are updated\n", "\n", + "def state(player,computer,result,player_win,computer_win):\n", + " print(\"You:\",player,\"VS Master:\",computer)\n", + " print(\"Your points:\",player_win,\"| Master's points: \",computer_win)\n", + "\n", " \n", "# Create two variables that accumulate the wins of each participant\n", "\n", + "computer_win=0\n", + "player_win=0\n", + "\n", "# Create a loop that iterates while no player reaches the minimum of wins\n", "# necessary to win. Inside the loop solves the play of the\n", "# machine and ask the player's. Compare them and update the value of the variables\n", "# that accumulate the wins of each participant.\n", "\n", + "print(\"Get \",win,\" points to win the game!\")\n", + "\n", + "for i in range(int(num_games)):\n", + " computer=random_options(options)\n", + " player=your_choice()\n", + " result=combat(computer,player)\n", + " if result==1:\n", + " computer_win+=1\n", + " print(\"Master gets a point.\")\n", + " elif result==2:\n", + " player_win+=1\n", + " print(\"You gets a point.\")\n", + " else:\n", + " print(\"Tie!\")\n", + " state(player,computer,result,player_win,computer_win)\n", " \n", " \n", "# Print by console the winner of the game based on who has more accumulated wins\n", - "\n" + "\n", + "if player_win>=win:\n", + " print(\"You win!\")\n", + "else:\n", + " print(\"You lose!\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -171,7 +409,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/snail-and-well/snail-and-well.ipynb b/snail-and-well/snail-and-well.ipynb index c8055f7..7625966 100644 --- a/snail-and-well/snail-and-well.ipynb +++ b/snail-and-well/snail-and-well.ipynb @@ -20,21 +20,67 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Days = 13\n" + ] + } + ], "source": [ "# Assign problem data to variables with representative names\n", "# well height, daily advance, night retreat, accumulated distance\n", + "well_height=125\n", + "daily_advance=30\n", + "night_retreat=20\n", + "accumulated_distance=[]\n", "\n", "\n", "# Assign 0 to the variable that represents the solution\n", - "\n", + "days=0\n", "\n", "# Write the code that solves the problem\n", + "while sum(accumulated_distance)<= well_height:\n", + " accumulated_distance.append((daily_advance-night_retreat))\n", + " days+=1\n", + " \n", "\n", - "\n", - "# Print the result with print('Days =', days)\n" + "# Print the result with print('Days =', days)\n", + "print(\"Days =\", days)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Days = 13\n" + ] + } + ], + "source": [ + "well_height=125\n", + "daily_advance=30\n", + "night_retreat=20\n", + "accumulated_distance=0\n", + "days=0\n", + "\n", + "while accumulated_distance <= well_height:\n", + " if True:\n", + " accumulated_distance+=(daily_advance-night_retreat)\n", + " days+=1\n", + " else:\n", + " break\n", + " \n", + "print(\"Days =\",days)" ] }, { @@ -69,35 +115,73 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 37, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Days = 6\n", + "Max : 77\n", + "Min : 12\n", + "Average progress = 70\n", + "Standard deviation: 18.037839015903106\n" + ] + } + ], "source": [ "# Assign problem data to variables with representative names\n", "# well height, daily advance, night retreat, accumulated distance\n", + "well_height=125\n", + "daily_advance=[30,20,33,77,44,45,23,45,12,34,55]\n", + "night_retreat=20\n", + "accumulated_distance=0\n", "\n", "\n", "# Assign 0 to the variable that represents the solution\n", - "\n", + "days=0\n", "\n", "# Write the code that solves the problem\n", - "\n", + "while accumulated_distance<=well_height:\n", + " if True:\n", + " accumulated_distance+=(daily_advance[days]-night_retreat)\n", + " days+=1\n", + " else:\n", + " break\n", "\n", "\n", "# Print the result with print('Days =', days)\n", - "\n", + "print(\"Days =\", days)\n", "\n", "# What is its maximum displacement in a day? And its minimum?\n", "\n", - "\n", + "print(\"Max :\", max(daily_advance))\n", + "print(\"Min :\", min(daily_advance))\n", "\n", "# What is its average progress?\n", "\n", + "print(\"Average progress =\", round(sum(daily_advance)/days))\n", "\n", "# What is the standard deviation of your displacement during the day?\n", + "\n", + "\n", + "mean=sum(daily_advance)/len(daily_advance)\n", + "varience=sum((((i-night_retreat)-mean)**2)for i in daily_advance[0:days])/len(daily_advance) \n", + "\n", + "st_dev=varience ** 0.5\n", + "\n", + "print(\"Standard deviation:\",st_dev)\n", "\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -122,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/temperature/temperature.ipynb b/temperature/temperature.ipynb index 048d15a..fc9202b 100644 --- a/temperature/temperature.ipynb +++ b/temperature/temperature.ipynb @@ -101,33 +101,60 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Minimum temperature: 0\n", + "Maximum temperature: 83\n", + "Temperatures higher than 70C: 76 C.\n", + "Temperatures higher than 70C: 80 C.\n", + "Temperatures higher than 70C: 80 C.\n", + "Temperatures higher than 70C: 83 C.\n", + "Temperatures higher than 70C: 79 C.\n", + "Mean temperature: 58.833333333333336\n", + "Temperature in F: [91.4, 150.8, 149.0, 137.9, 138.2, 140.0, 143.60000000000002, 147.2, 158.0, 168.8, 176.0, 156.2, 176.0, 181.4, 154.4, 174.20000000000002, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n" + ] + } + ], "source": [ "# assign a variable to the list of temperatures\n", + "temp_C = [33,66,65,0,59,60,62,64,70,76,80,69,80,83,68,79,61,53,50,49,53,48,45,39]\n", "\n", "# 1. Calculate the minimum of the list and print the value using print()\n", - "\n", + "min_temp=(min(temp_C))\n", + "print(\"Minimum temperature:\", min_temp)\n", "\n", "# 2. Calculate the maximum of the list and print the value using print()\n", - "\n", + "max_temp=(max(temp_C))\n", + "print(\"Maximum temperature:\",max_temp)\n", "\n", "# 3. Items in the list that are greater than 70ºC and print the result\n", - "\n", + "for t in temp_C:\n", + " if t>70:\n", + " print(\"Temperatures higher than 70C:\", t,\"C.\")\n", "\n", "# 4. Calculate the mean temperature throughout the day and print the result\n", - "\n", + "mean_temp=sum(temp_C)/len(temp_C)\n", + "print(\"Mean temperature:\",mean_temp)\n", "\n", "# 5.1 Solve the fault in the sensor by estimating a value\n", + "del temp_C[3]\n", "\n", "\n", "# 5.2 Update of the estimated value at 03:00 on the list\n", - "\n", + "temp_C.insert(3,mean_temp)\n", "\n", "\n", "# Bonus: convert the list of ºC to ºFarenheit\n", - "\n" + "temp_F=[]\n", + "for t in temp_C:\n", + " temp_F.append(t*1.8+32)\n", + " \n", + "print(\"Temperature in F:\",temp_F)" ] }, { @@ -144,12 +171,37 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 26, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "True\n" + ] + } + ], "source": [ "# Print True or False depending on whether you would change the cooling system or not\n", - "\n" + "\n", + "if mean_temp>65:\n", + " print(True)\n", + "else:\n", + " count=0\n", + " for t in temp_C:\n", + " if t >=70:\n", + " count+=1\n", + " elif t>80:\n", + " print(True)\n", + "\n", + "if count>4:\n", + " print(True)\n", + "else:\n", + " print(False)\n", + " \n", + " " ] }, { @@ -165,41 +217,109 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hours whose temperature >70: [9, 10, 12, 13, 15]\n" + ] + } + ], "source": [ "# 1. We want the hours (not the temperatures) whose temperature exceeds 70ºC\n", - "\n" + "temp_C = [33,66,65,0,59,60,62,64,70,76,80,69,80,83,68,79,61,53,50,49,53,48,45,39]\n", + "\n", + "hours=[]\n", + "\n", + "i=0\n", + "while i!=len(temp_C):\n", + " if temp_C[i]>70:\n", + " hours.append(i)\n", + " else:\n", + " pass\n", + " i+=1\n", + "\n", + "print(\"Hours whose temperature >70:\",hours)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 24, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "# 2. Condition that those hours are more than 4 consecutive and consecutive, not simply the sum of the whole set. Is this condition met?\n", + "\n", + "count=1\n", + "\n", + "for i in range(0,len(hours)-1):\n", + " if hours[i+1]==hours[i]+1:\n", + " count+=1\n", + " else:\n", + " count=1\n", + " \n", + "if count>=4:\n", + " print(True)\n", + "else:\n", + " print(False)\n", + " \n", + "\n", "\n" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average of list F: average of list C: 2.322152974504249 :1\n" + ] + } + ], "source": [ "# 3. Average of each of the lists (ºC and ºF). How they relate?\n", + "\n", + "print(\"Average of list F: average of list C:\",(sum(temp_F)/len(temp_F))/(sum(temp_C)/len(temp_C)),\":1\")\n", "\n" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 44, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SD of list F: SD of list C: 1.7999999999999998 :1\n" + ] + } + ], "source": [ "# 4. Standard deviation of each of the lists. How they relate?\n", + "mean_C=sum(temp_C)/len(temp_C)\n", + "mean_F=sum(temp_F)/len(temp_F)\n", + "\n", + "stdv_C = (sum(((i-mean_C)**2) for i in temp_C)/len(temp_C))**0.5\n", + "stdv_F = (sum(((i-mean_F)**2) for i in temp_F)/len(temp_F))**0.5\n", + "\n", + "print(\"SD of list F: SD of list C:\", stdv_F/stdv_C ,\":1\")\n", "\n" ] }, @@ -227,7 +347,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.4" } }, "nbformat": 4,