diff --git a/bus/bus.ipynb b/bus/bus.ipynb index 81779ce..5747163 100644 --- a/bus/bus.ipynb +++ b/bus/bus.ipynb @@ -32,53 +32,138 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# variables\n", - "\n" + "stops = [(5,0),(8,2),(5,3),(7,4)]\n" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 1. Calculate the number of stops.\n", - "\n" + "\n", + "len(stops)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 4, 3, 3, 5, 1, 5, 4, 2]\n", + "[0, 1, 5, 4, 1, 5, 8, 6, 3]\n", + "[10, 13, 11, 10, 14, 10, 7, 5, 4]\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" + "stops = [(10, 0), (4, 1), (3, 5), (3, 4), (5, 1), (1, 5), (5, 8 ), (4, 6), (2, 3)]\n", + "bus_in = []\n", + "for i in stops:\n", + " bus_in.append(i [0])\n", + "print(bus_in)\n", + "\n", + "bus_out = []\n", + "\n", + "for j in stops:\n", + " bus_out.append(j [1])\n", + "print(bus_out)\n", + "\n", + "bus_occupied=[]\n", + "total = 0\n", + "for x,y in zip(bus_in, bus_out):\n", + " total += x-y\n", + " bus_occupied.append(total)\n", + "print(bus_occupied)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "14" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 3. Find the maximum occupation of the bus.\n", - "\n" + "\n", + "max(bus_occupied)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "9.333333333333334" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# 4. Calculate the average occupation. And the standard deviation.\n", - "\n" + "\n", + "sum(bus_occupied)/len(bus_occupied)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3.391164991562634" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# standard deviation\n", + "import statistics\n", + "statistics.stdev(bus_occupied)" ] }, { @@ -105,7 +190,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/duel/duel.ipynb b/duel/duel.ipynb index 4398d88..a1a199e 100644 --- a/duel/duel.ipynb +++ b/duel/duel.ipynb @@ -33,28 +33,61 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "# Assign spell power lists to variables\n", "\n", "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", - "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]" + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 0, 1, 0, 1, 1, 0, 1, 1, 1]\n", + "[1, 1, 0, 1, 0, 0, 1, 0, 0, 0]\n", + "Gandalf wins 6 clashes\n" + ] + } + ], "source": [ - "# Assign 0 to each variable that stores the victories\n" + "# Assign 0 to each variable that stores the victories\n", + "gandalf = [10, 11, 13, 30, 22, 11, 10, 33, 22, 22]\n", + "saruman = [23, 66, 12, 43, 12, 10, 44, 23, 12, 17]\n", + "\n", + "# 1 for win, 0 for loss;\n", + "win_g = []\n", + "win_s = []\n", + "for i,j in zip(gandalf, saruman):\n", + " if i > j:\n", + " win_g.append(1)\n", + " win_s.append(0)\n", + " elif i ==j:\n", + " win_g.append(0)\n", + " win_s.append(0)\n", + " elif i < j:\n", + " win_g.append(0)\n", + " win_s.append(1)\n", + "print(win_g)\n", + "print(win_s)\n", + " \n", + "if sum(win_g) > sum(win_s):\n", + " print(\"Gandalf wins\", sum(win_g), \"clashes\")\n", + "elif sum(win_g)< sum(win_s):\n", + " print(\"Saruman wins\", sum(win_s), \"clashes\")" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -63,11 +96,14 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# We check who has won, do not forget the possibility of a draw.\n", + "\n", + "\n", + " \n", "# Print the result based on the winner.\n", "\n" ] @@ -116,9 +152,19 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 10, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Fireball', 50), ('Lightning bolt', 40), ('Magic arrow', 10), ('Black Tentacles', 25), ('Contagion', 45)]\n", + "[50, 40, 40, 10, 50, 10, 40, 50, 10, 50]\n", + "[45, 45, 25, 50, 25, 40, 10, 45, 10, 10]\n" + ] + } + ], "source": [ "# 1. Spells now have a name and there is a dictionary that relates that name to a power.\n", "# variables\n", @@ -134,12 +180,28 @@ "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']" + " 'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']\n", + "\n", + "power=[(i,j) for i,j in POWER.items()]\n", + "print(power)\n", + "g_score = []\n", + "for z in gandalf:\n", + " for i,j in power:\n", + " if z == i:\n", + " g_score.append(j)\n", + "print(g_score)\n", + " \n", + "s_score = []\n", + "for z in saruman:\n", + " for i,j in power:\n", + " if z ==i:\n", + " s_score.append(j)\n", + "print(s_score)\n" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -149,41 +211,105 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 0, 1, 0, 1, 0, 1, 1, 0, 1]\n", + "[0, 1, 0, 1, 0, 1, 0, 0, 0, 0]\n", + "[0, 2, 4, 6, 7, 9]\n", + "Gandalf looses.\n", + "Saruman looses.\n", + "nobody won\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", + "wins_g =[]\n", + "wins_s=[]\n", + "for g,s in zip(g_score, s_score):\n", + " if g>s:\n", + " wins_g.append(1)\n", + " wins_s.append(0)\n", + " elif g1]\n", + "print(double_shot)\n", + "print(\"Robin Hood hit\" , len(double_shot) , \"arrows with another arrow!\")\n", "\n" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 102, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(4, 5), (4, 7), (4, 5), (3, 2), (5, 7), (2, 2), (5, 7), (5, 7), (2, 2), (9, 9)]\n", + "[(-5, 7), (-4, 5), (-4, 7), (-1, 3), (-3, 2), (-3, 2)]\n", + "[(-4, -5), (-8, -9)]\n", + "[(1, -3), (3, -2)]\n", + "[(0, 2), (0, -2)]\n", + "In Q1 are 10 arrows. In Q2 are 6 arrows. In Q3 are 2 arrows. In Q4 are 2 arrows. But 2 arrows are on the aix\n" + ] + } + ], "source": [ "# 2. Calculate how many arrows have fallen in each quadrant.\n", - "\n" + "# x and y are positiv Q_I\n", + "# x and y are negativ Q_III\n", + "# x negative and y positiv Q_II\n", + "# x positiv and y negative Q_IV\n", + "Q_I=[]\n", + "Q_II = []\n", + "Q_III = []\n", + "Q_IV = []\n", + "aix= []\n", + "for (x,y) in points:\n", + " if (x < 0) & (y < 0):\n", + " Q_III.append((x,y))\n", + " elif (x == 0) | (y == 0):\n", + " aix.append((x,y))\n", + " elif (x > 0) & (y>0):\n", + " Q_I.append((x,y))\n", + " elif (x < 0) & (y >0):\n", + " Q_II.append((x,y))\n", + " elif (x>0) & (y<0):\n", + " Q_IV.append((x,y))\n", + "\n", + "print(Q_I)\n", + "print(Q_II) \n", + "print(Q_III)\n", + "print(Q_IV)\n", + "print(aix)\n", + "\n", + "print(\"In Q1 are\", len(Q_I), \"arrows. In Q2 are\", len(Q_II), \"arrows. In Q3 are\", len(Q_III), \"arrows. In Q4 are \", len(Q_IV), \"arrows. But \", len(aix), \"arrows are on the aix\")\n" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 135, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 6.4031242374328485, 2.0, 8.06225774829855, 3.1622776601683795, 3.605551275463989, 6.4031242374328485, 3.605551275463989, 8.602325267042627, 8.602325267042627, 2.8284271247461903, 12.727922061357855, 12.041594578792296]\n", + "2.0\n", + "The 2 . shot and the 12 . shot were the closest.\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" + "distance = []\n", + "import math\n", + "for x,y in points:\n", + " z = math.sqrt(x**2+y**2)\n", + " distance.append(z)\n", + "print(distance)\n", + "print(min(distance))\n", + "\n", + "closest_shot = [ i for i,n in enumerate(distance) if n == min(distance)]\n", + "\n", + "print(\"The\", closest_shot[0]+1 , \". shot and the\", closest_shot[1]+1, \". shot were the closest.\" )" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 143, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 arrows has to pick 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" + "arrows_forest=[]\n", + "for g in distance:\n", + " if g > 9:\n", + " arrows_forest.append(g)\n", + "print(len(arrows_forest), \"arrows has to pick up in the forest.\")\n", + "\n", + "\n", + " \n" ] }, { @@ -121,7 +213,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.3" } }, "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..8ea39f6 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,35 +25,91 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "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", + "print(\"Let´s play Stone-paper-scissors!\\n\\n\",\"*\"*100,\"\\n\")\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_to_win = round(max_game/2)+1\n", + "\n", + "\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", "\n", + "\n", + "def MachineMove(options= options):\n", + " machine_choice =random.choice(options)\n", + " return machine_choice\n", + "\n", + "\n", + "\n", "# Define a function that asks your choice: 'stone', 'paper' or 'scissors'\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", + "def HumanMove(options=options):\n", + " human_choice = input(\"It´s your turn, please choose: \").strip() \n", + " if human_choice in options:\n", + " return human_choice\n", + " else: \n", + " print(\"I don´t understand, please try again!\\n\")\n", + " return HumanMove(options=options)\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():\n", + " machine_choice = MachineMove()\n", + " human_choice = HumanMove()\n", + " print(\"\\nYou: \", human_choice, \"Machine: \", machine_choice)\n", + " if human_choice == machine_choice:\n", + " return 0\n", + " if (human_choice == \"stone\" and machine_choice == \"paper\") | (human_choice == \"scissors\" and machine_choice == \"stone\") | (human_choice == \"paper\" and machine_choice == \"scissors\"):\n", + " return 1\n", + " if (human_choice == \"paper\" and machine_choice == \"stone\") | (human_choice == \"stone\" and machine_choice == \"scissors\")| (human_choice == \"scissors\" and machine_choice == \"paper\"):\n", + " return 2\n", " \n", + " \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", + "score_h = 0\n", + "score_m = 0\n", + "scores= [score_h, score_m]\n", + "\n", + "def game_status(scores=scores):\n", + " result = combat()\n", + " if result == 1:\n", + " scores[1] = scores[1]+1\n", + " print(\"The computer won this round.\")\n", + " elif result == 2:\n", + " scores[0] = scores[0]+1\n", + " print(\"You won this round.\")\n", + " elif result == 0:\n", + " print(\"It´s a Tie.\")\n", + " print(\"\\n\\nThe Score is:\" , \"\\nYou: \", scores[0], \"Machine: \", scores[1])\n", + " return scores\n", + "\n", + "\n", + "\n", "\n", " \n", "# Create two variables that accumulate the wins of each participant\n", @@ -64,12 +120,28 @@ "# 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", - "\n", + "while (scores[0]+scores[1]= win_to_win:\n", + " print(\"Congrats! You win!\")\n", + " break\n", + " elif scores[1] >=win_to_win:\n", + " print(\"GAME OVER! \\nMachine wins!\")\n", + " break\n", + " \n", " \n", "# Print by console the winner of the game based on who has more accumulated wins\n", " " ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", "metadata": {}, @@ -93,45 +165,158 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Let´s play stone, paper, scissors, lizard, spock\n", + " **************************************************************************************************** \n", + "\n", + "\n", + "We will play a best of #. Please choose an odd integer number for the amount of maximum games:5\n", + "We will play a best of 5\n", + "Please enter your choice: lizard\n", + "You: lizard Machine: lizard\n", + "It´s a Tie!\n", + "Your score: 0 Machine score: 0 \n", + "\n", + "Please enter your choice: scissors\n", + "You: scissors Machine: spock\n", + "Machine won this round! \n", + "\n", + " You: 0 Machine: 1 \n", + "\n", + "Your score: 0 Machine score: 1 \n", + "\n", + "Please enter your choice: lizard\n", + "You: lizard Machine: stone\n", + "Machine won this round! \n", + "\n", + " You: 0 Machine: 2 \n", + "\n", + "Your score: 0 Machine score: 2 \n", + "\n", + "Please enter your choice: spock\n", + "You: spock Machine: stone\n", + "You won this round! \n", + "\n", + " You: 1 Machine: 2 \n", + "\n", + "Your score: 1 Machine score: 2 \n", + "\n", + "Please enter your choice: stone\n", + "You: stone Machine: scissors\n", + "You won this round! \n", + "\n", + " You: 2 Machine: 2 \n", + "\n", + "Your score: 2 Machine score: 2 \n", + "\n", + "Please enter your choice: paper\n", + "You: paper Machine: spock\n", + "You won this round! \n", + "\n", + " You: 3 Machine: 2 \n", + "\n", + "Your score: 3 Machine score: 2 \n", + "\n", + "\n", + " ********** \n", + " Congrats! You won!\n" + ] + } + ], "source": [ "# Import the choice function of the random module\n", - "\n", + "import random\n", + " \n", + "print(\"Let´s play stone, paper, scissors, lizard, spock\\n\", \"*\"*100, \"\\n\\n\")\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", "\n", "# Assign a list of 5 possible options.\n", + "new_options = [\"lizard\",\"spock\", \"stone\",\"paper\",\"scissors\"]\n", "\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", + "def max_games(maxgames):\n", + " #maxgames = int(input(\"We will play a best of #. Please choose an odd integer number for the amount of maximum games:\"))\n", + " if maxgames %2 != 1:\n", + " return max_games(int(input(\"Please choose an odd integer:\")))\n", + " #maxgames=max_games(int(input(\"Please choose an odd integer:\")))\n", + " else:\n", + " print(\"We will play a best of\", maxgames)\n", + " return maxgames\n", + "\n", + "maxgames=max_games(int(input(\"We will play a best of #. Please choose an odd integer number for the amount of maximum games:\")))\n", "\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_to_win = round(maxgames/2)+1\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", + "def MachineMove5(new_options=new_options):\n", + " machine_choice5 = random.choice(new_options)\n", + " return machine_choice5\n", "\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", + "def HumanMove5(new_options = new_options):\n", + " human_choice5 = input(\"Please enter your choice: \").strip()\n", + " if human_choice5 not in new_options:\n", + " print(\"Invalid input! Please try it again: \")\n", + " return HumanMove5(new_options=new_options)\n", + " else:\n", + " return human_choice5\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", "# Now there are more options\n", - " \n", + "def combat5():\n", + " machine_choice5 = MachineMove5()\n", + " human_choice5 = HumanMove5()\n", + " print(\"You: \", human_choice5, \"Machine: \", machine_choice5)\n", + " if human_choice5 == machine_choice5:\n", + " return 0\n", + " if (human_choice5 == \"stone\" and machine_choice5 == \"paper\") | (human_choice5 == \"scissors\" and machine_choice5 == \"stone\") | (human_choice5 == \"paper\" and machine_choice5 == \"scissors\")| (human_choice5 == \"lizard\" and machine_choice5 == \"stone\") | (human_choice5 == \"spock\" and machine_choice5 == \"lizard\") | (human_choice5 == \"scissors\" and machine_choice5 == \"spock\") | (human_choice5 == \"lizard\" and machine_choice5 == \"spock\") | (human_choice5 == \"paper\" and machine_choice5 == \"lizard\") | (human_choice5 == \"spock\" and machine_choice5 == \"paper\") | (human_choice5 == \"stone\" and machine_choice5 == \"spock\") | (human_choice5 == \"lizard\" and machine_choice5 == \"scissors\"):\n", + " return 1\n", + " if (human_choice5 == \"paper\" and machine_choice5 == \"stone\") | (human_choice5 == \"stone\" and machine_choice5 == \"scissors\")| (human_choice5 == \"scissors\" and machine_choice5 == \"paper\") | (human_choice5 == \"stone\" and machine_choice5 == \"lizard\") | (human_choice5 == \"lizard\" and machine_choice5 == \"spock\") | (human_choice5 == \"spock\" and machine_choice5 == \"scissors\") | (human_choice5 == \"spock\" and machine_choice5 == \"lizard\") | (human_choice5 == \"lizard\" and machine_choice5 == \"paper\") | (human_choice5 == \"paper\" and machine_choice5 == \"spock\") | (human_choice5 == \"spock\" and machine_choice5 == \"stone\")| (human_choice5 == \"scissors\" and machine_choice5 == \"lizard\"):\n", + " return 2\n", + " \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", + "score_h5 = 0\n", + "score_m5 = 0\n", + "scores5 = [score_h5, score_m5]\n", + "\n", + "def game_status5(scores5 = scores5):\n", + " result5 = combat5()\n", + " if result5 == 1:\n", + " scores5[1] = scores5[1]+1\n", + " print(\"Machine won this round!\",\"\\n\\n\",\"You: \", scores5[0], \"Machine: \", scores5[1], \"\\n\")\n", + " elif result5 == 2:\n", + " scores5[0] = scores5[0]+1\n", + " print(\"You won this round!\",\"\\n\\n\",\"You: \", scores5[0], \"Machine: \", scores5[1], \"\\n\")\n", + " elif result5 == 0:\n", + " print(\"It´s a Tie!\")\n", + " print(\"Your score: \", scores5[0], \"Machine score: \", scores5[1], \"\\n\")\n", + " return scores5\n", + " \n", "\n", " \n", "# Create two variables that accumulate the wins of each participant\n", @@ -141,12 +326,26 @@ "# 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", - " \n", + " \n", " \n", "# Print by console the winner of the game based on who has more accumulated wins\n", - "\n" + "while (scores5[0]+scores5[1]= win_to_win:\n", + " print(\"\\n\", \"*\"*10,\"\\n\",\"Congrats! You won!\")\n", + " break\n", + " elif scores5[1] >= win_to_win:\n", + " print(\"\\n\",\"*\"*10,\"\\n\",\"GAME OVER\")\n", + " break\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, @@ -171,7 +370,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/snail-and-well/snail-and-well.ipynb b/snail-and-well/snail-and-well.ipynb index c8055f7..a78a5c5 100644 --- a/snail-and-well/snail-and-well.ipynb +++ b/snail-and-well/snail-and-well.ipynb @@ -20,21 +20,43 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The snail escaped after 11 days\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", + "snailNotEscapeYet = True\n", + "distance_with_nights = well_height - daily_advance\n", "\n", "\n", "# Assign 0 to the variable that represents the solution\n", - "\n", + "days_nights = 0\n", + "accumulated_distance =0\n", "\n", "# Write the code that solves the problem\n", + "while snailNotEscapeYet:\n", + " accumulated_distance += daily_advance - night_retreat\n", + " days_nights += 1\n", + " if accumulated_distance >= distance_with_nights:\n", + " snailNotEscapeYet = False\n", "\n", + "days = days_nights +1\n", "\n", - "# Print the result with print('Days =', days)\n" + "\n", + "# Print the result with print('Days =', days)\n", + "print(\"The snail escaped after \", days , \"days\")" ] }, { @@ -69,13 +91,33 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "17.996969441850734" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Assign problem data to variables with representative names\n", "# well height, daily advance, night retreat, accumulated distance\n", + "well = 125\n", + "advance_cm = [30,21,33,77,44,45,23,45,12,34,55]\n", + "night_retreat = -20\n", + "whole_days = []\n", + "whole_day_cm = advance_cm[0] + night_retreat\n", + "whole_days.append(whole_day_cm)\n", + "whole_days\n", + "\n", "\n", + " \n", "\n", "# Assign 0 to the variable that represents the solution\n", "\n", @@ -89,13 +131,15 @@ "\n", "# What is its maximum displacement in a day? And its minimum?\n", "\n", - "\n", + "max(advance_cm)\n", + "min(advance_cm)\n", "\n", "# What is its average progress?\n", - "\n", + "sum(advance_cm)/len(advance_cm)\n", "\n", "# What is the standard deviation of your displacement during the day?\n", - "\n" + "import statistics\n", + "statistics.stdev(advance_cm)\n" ] }, { @@ -122,7 +166,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/temperature/temperature.ipynb b/temperature/temperature.ipynb index 048d15a..7dcc3a3 100644 --- a/temperature/temperature.ipynb +++ b/temperature/temperature.ipynb @@ -101,33 +101,59 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 22, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "83\n", + "[76, 80, 80, 83, 79]\n", + "58.833333333333336\n", + "[33, 66, 65, 58.833333333333336, 59, 60, 62, 64, 70, 76, 80, 69, 80, 83, 68, 79, 61, 53, 50, 49, 53, 48, 45, 39]\n", + "[91.4, 150.8, 149.0, 137.9, 138.2, 140.0, 143.6, 147.2, 158.0, 168.8, 176.0, 156.2, 176.0, 181.4, 154.4, 174.2, 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", - "\n", + "temperatures_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", "# 1. Calculate the minimum of the list and print the value using print()\n", - "\n", + "min(temperatures_C)\n", + "print(min(temperatures_C))\n", "\n", "# 2. Calculate the maximum of the list and print the value using print()\n", - "\n", + "z = max(temperatures_C)\n", + "print(z)\n", "\n", "# 3. Items in the list that are greater than 70ºC and print the result\n", - "\n", - "\n", + "more_70 = []\n", + "for i in temperatures_C:\n", + " if i > (70):\n", + " more_70.append(i)\n", + "print(more_70)\n", + " \n", + " \n", "# 4. Calculate the mean temperature throughout the day and print the result\n", - "\n", + "mean_temperature = sum(temperatures_C)/len(temperatures_C)\n", + "print(mean_temperature)\n", "\n", "# 5.1 Solve the fault in the sensor by estimating a value\n", + "temperatures_C[3] = mean_temperature\n", "\n", "\n", "# 5.2 Update of the estimated value at 03:00 on the list\n", - "\n", + "print(temperatures_C)\n", "\n", "\n", "# Bonus: convert the list of ºC to ºFarenheit\n", - "\n" + "temperatures_F =[]\n", + "for i in temperatures_C:\n", + " F = ((i*9/5)+32)\n", + " temperatures_F.append(F)\n", + "print(temperatures_F)" ] }, { @@ -144,12 +170,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Print True or False depending on whether you would change the cooling system or not\n", - "\n" + "\n", + "Change_coolingsystem = True" ] }, { @@ -165,41 +192,121 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8, 9, 10, 12, 13, 15]\n" + ] + } + ], "source": [ "# 1. We want the hours (not the temperatures) whose temperature exceeds 70ºC\n", - "\n" + "hours_more_70 = []\n", + "temperatures_C = [33,66,65,58.33,59,60,62,64,70,76,80,69,80,83,68,79,61,53,50,49,53,48, 45, 39]\n", + "hours_more_70 = [i for i,n in enumerate(temperatures_C) if n >= 70]\n", + "print(hours_more_70)\n" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We do not need to change the cooling system.\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" + "if hours_more_70[0]+1 == hours_more_70[1] and hours_more_70[1]+1 == hours_more_70[2] and hours_more_70[2]+1 == hours_more_70[3] and hours_more_70[3]+1 == hours_more_70[4]:\n", + " print(\"Please change the cooling system.\")\n", + "elif hours_more_70[1]+1 == hours_more_70[2] and hours_more_70[2]+1 == hours_more_70[3] and hours_more_70[3]+1 == hours_more_70[4] and hours_more_70[4]+1 == hours_more_70[5]:\n", + " print(\"Please change the cooling system.\")\n", + "elif hours_more_70[2]+1 == hours_more_70[3] and hours_more_70[3]+1 == hours_more_70[4] and hours_more_70[4]+1 == hours_more_70[5] and hours_more_70[5]+1 == hours_more_70[6]:\n", + " print(\"Please change the cooling system.\")\n", + "else:\n", + " print(\"We do not need to change the cooling system.\")" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 88, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "61.263749999999995\n", + "142.3125\n", + "The average of the list F is nine-fifths of the average of the list C + 32.\n" + ] + } + ], "source": [ "# 3. Average of each of the lists (ºC and ºF). How they relate?\n", - "\n" + "temperatures_F = [91.4, 150.8, 149.0, 137.9, 138.2, 140.0, 143.6, 147.2, 158.0, 168.8, 176.0, 156.2, 176.0, 181.4, 154.4, 174.2, 141.8, 127.4, 122.0, 120.2, 127.4, 118.4, 113.0, 102.2]\n", + "avrg_C = sum(temperatures_C)/len(temperatures_C)\n", + "avrg_F = sum(temperatures_F)/len(temperatures_F)\n", + "print(avrg_C)\n", + "print(avrg_F)\n", + "\n", + "if int((avrg_C*9/5)+32) == int(avrg_F):\n", + " print(\"The average of the list F is nine-fifths of the average of the list C + 32.\")" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 108, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13.345670788849388\n", + "24.014258671989385\n", + "[-58.400000000000006, -84.80000000000001, -84.0, -79.57000000000001, -79.19999999999999, -80.0, -81.6, -83.19999999999999, -88.0, -92.80000000000001, -96.0, -87.19999999999999, -96.0, -98.4, -86.4, -95.19999999999999, -80.80000000000001, -74.4, -72.0, -71.2, -74.4, -70.4, -68.0, -63.2]\n", + "10.669476704701777\n", + "Standard deviation of the difference between each element in the list temperatures_C and temperatures_F is the difference between the standard deviation of the list temperatures_C and temperatures_F So they are equivalent. \n" + ] + } + ], "source": [ "# 4. Standard deviation of each of the lists. How they relate?\n", + "import statistics\n", + "c = statistics.stdev(temperatures_C)\n", + "f = statistics.stdev(temperatures_F)\n", + "print(c)\n", + "print(f)\n", + "\n", + "difference=[]\n", + "for i,j in zip(temperatures_C,temperatures_F):\n", + " g = i-j\n", + " difference.append(g)\n", + "print(difference)\n", + "d = statistics.stdev(difference)\n", + "print(d)\n", + "\n", + "if int(c+d) == int(f):\n", + " print(\"Standard deviation of the difference between each element in the list temperatures_C and temperatures_F is the difference between the standard deviation of the list temperatures_C and temperatures_F So they are equivalent. \")\n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "\n" ] }, @@ -227,7 +334,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.7" + "version": "3.7.3" } }, "nbformat": 4,