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..cd1710c 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=0 #snail starts at the bottom on day 0" + ] }, { "cell_type": "markdown", @@ -58,10 +65,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "while snail_positionsaruman[spell]:\n", + " gandalf_wins+=1 \n", + " elif gandalf[spell]saruman_wins:\n", + " print('Gandalf wins')\n", + "elif gandalf_winssaruman_power[spell]:\n", + " if previous_winner=='gandalf':\n", + " gandalf_wins+=1 # if it´s a streak increment count\n", + " else:\n", + " gandalf_wins=1 # if other winner in previous throw reset count\n", + " previous_winner='gandalf'\n", + " elif gandalf_power[spell]1:\n", + " print('Robin Hodd has it one arrow with {} other arrow(s) at point {}'.format(counts[item]-1,item))" + ] }, { "cell_type": "markdown", @@ -70,10 +89,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " Q1: 10 \n", + " Q2: 6 \n", + " Q3: 2 \n", + " Q4: 2\n" + ] + } + ], + "source": [ + "quadrant_counts=[0]*4\n", + "\n", + "for item in points:\n", + " if item[0]>0 and item[1]>0:\n", + " quadrant_counts[0]+=1\n", + " elif item[0]<0 and item[1]>0:\n", + " quadrant_counts[1]+=1\n", + " elif item[0]<0 and item[1]<0:\n", + " quadrant_counts[2]+=1\n", + " elif item[0]>0 and item[1]<0:\n", + " quadrant_counts[3]+=1\n", + "print(' Q1:',quadrant_counts[0],'\\n','Q2:',quadrant_counts[1],'\\n','Q3:',quadrant_counts[2],'\\n','Q4:',quadrant_counts[3])" + ] }, { "cell_type": "markdown", @@ -88,10 +131,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(0, 2), (0, -2)] is(are) the closest point(s) to the center at a distance of 2.0\n" + ] + } + ], + "source": [ + "def distance(point):\n", + " dist=(point[0]**2+point[1]**2)**0.5\n", + " return dist\n", + "\n", + "min_points=[]\n", + "min_distance=1000000 #initialize a very large value to ensure all distances in set are smaller\n", + "\n", + "for item in points:\n", + " point_distance=distance(item)\n", + " if point_distance9]) \n", + "\n", + "print('Number of arrows that missed the target:',missed_target_counts)" + ] } ], "metadata": { @@ -125,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git a/1.-Python/5.-Temperature-Processor/temperature.ipynb b/1.-Python/5.-Temperature-Processor/temperature.ipynb index 4b597aa..4f7057f 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,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "min_temp=min(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -67,10 +69,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "max_temp=max(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -81,10 +85,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temp_greater_70=[temp for temp in temperatures_C if temp>=70]" + ] }, { "cell_type": "markdown", @@ -95,10 +101,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60.25\n" + ] + } + ], + "source": [ + "from statistics import mean\n", + "\n", + "avg_temp=mean(temperatures_C)" + ] }, { "cell_type": "markdown", @@ -112,7 +130,10 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temp_3am_est=(temperatures_C[2]+temperatures_C[4])/2 # estimated it as the average values of the two closest hours\n", + "temperatures_C[3]=temp_3am_est" + ] }, { "cell_type": "markdown", @@ -128,10 +149,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "temp_fah=[1.8*temp+32 for temp in temperatures_C]" + ] }, { "cell_type": "markdown", @@ -150,10 +173,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Replace Cooling System ASAP!!!\n" + ] + } + ], + "source": [ + "if (len(temp_greater_70)>4 or max_temp>80 or avg_temp>65) :\n", + " print('Replace Cooling System ASAP!!!')\n", + "else:\n", + " print ('Cooling System is OK')" + ] }, { "cell_type": "markdown", @@ -175,10 +211,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[9, 10, 11, 12, 13, 14, 15]\n" + ] + } + ], + "source": [ + "hours_above_70=[hour for hour in range(len(temperatures_C)) if temperatures_C[hour]>70]" + ] }, { "cell_type": "markdown", @@ -189,10 +235,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The temperature is above 70ºC for 6 consecutive hours\n" + ] + } + ], + "source": [ + "consecutive_hrs=0\n", + "\n", + "for i in range(1,len(hours_above_70)):\n", + " if hours_above_70[i]-hours_above_70[i-1]==1:\n", + " consecutive_hrs+=1\n", + " else:\n", + " consecutive_hrs==0\n", + "\n", + "print('The temperature is above 70ºC for {} consecutive hours'.format(consecutive_hrs))" + ] }, { "cell_type": "markdown", @@ -204,10 +268,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Replace Cooling System ASAP!!!\n" + ] + } + ], + "source": [ + "if (consecutive_hrs>4 or max_temp>80 or avg_temp>65) :\n", + " print('Replace Cooling System ASAP!!!')\n", + "else:\n", + " print ('Cooling System is OK')" + ] }, { "cell_type": "markdown", @@ -218,10 +295,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average Temperature in ºC: 60.25\n", + "Average Temperature in Fahrenheit: 140.45000000000002\n", + "Average Temperature in Fahrenheit calculated from formula: 140.45\n" + ] + } + ], + "source": [ + "from statistics import mean, stdev\n", + "\n", + "average_C=mean(temperatures_C)\n", + "average_F=mean(temp_fah)\n", + "\n", + "print('Average Temperature in ºC: ',average_C)\n", + "print('Average Temperature in Fahrenheit: ',average_F)\n", + "\n", + "average_F_2=average_C*1.8+32\n", + "print('Average Temperature in Fahrenheit calculated from formula:',average_F_2) # to show that the formula holds with averages\n" + ] }, { "cell_type": "markdown", @@ -232,10 +330,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "St. Dev. Temperature in ºC: 19.700474503223873\n", + "St. Dev. Temperature in Fahrenheit: 35.46085410580297\n", + "St. Dev. Temperature in Fahrenheit calculated from formula: 35.46085410580297\n" + ] + } + ], + "source": [ + "stdev_C=stdev(temperatures_C)\n", + "stdev_F=stdev(temp_fah)\n", + "\n", + "print('St. Dev. Temperature in ºC: ',stdev_C)\n", + "print('St. Dev. Temperature in Fahrenheit: ',stdev_F)\n", + "\n", + "stdev_F_2=stdev_C*1.8\n", + "print('St. Dev. Temperature in Fahrenheit calculated from formula:',stdev_F_2) # to show that stdevF=1.8*stdevC\n", + "\n", + "#from statistics stdev(cX)=abs(c)stdev(X) and stdev(X+c)=stdev(X) so stdev(X*1.8+32)=1.8*stdev(X)" + ] } ], "metadata": { @@ -254,7 +373,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.4" } }, "nbformat": 4, diff --git "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" index 9e551df..adacfc0 100644 --- "a/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" +++ "b/1.-Python/6.-Rock\342\200\223Paper\342\200\223Scissors/rock-paper-scissors.ipynb" @@ -32,10 +32,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from random import choice" + ] }, { "cell_type": "markdown", @@ -46,10 +48,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "gestures=['rock','paper','scissors']" + ] }, { "cell_type": "markdown", @@ -61,10 +65,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "n_rounds=7" + ] }, { "cell_type": "markdown", @@ -76,10 +82,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "rounds_to_win=int(n_rounds/2+1)" + ] }, { "cell_type": "markdown", @@ -90,10 +98,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "cpu_score=0\n", + "player_score=0" + ] }, { "cell_type": "markdown", @@ -105,10 +116,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def computer_play(gesture_list):\n", + " return choice(gesture_list) " + ] }, { "cell_type": "markdown", @@ -120,10 +134,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def human_play():\n", + " human_choice=''\n", + " while human_choice not in gestures:\n", + " human_choice=input('What is your option (rock/paper/scissors)?')\n", + " return human_choice" + ] }, { "cell_type": "markdown", @@ -135,10 +155,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def who_wins(computer_pl,human_pl):\n", + "\n", + " play_to_number={'rock':0,'paper':1,'scissors':2}\n", + " \n", + " nb_comp_play=play_to_number[computer_pl]\n", + " nb_human_play=play_to_number[human_pl]\n", + " \n", + " result_matrix = [[0 for x in range(3)] for y in range(3)] # sets a matrix size 3x3 to fill with the possible outcomes of a round (a list of 3 lists containing 3 results each)\n", + " \n", + " #Ties already at 0 so no need to configure\n", + " #Computer Rock vs. Human\n", + " result_matrix[0][1]=2\n", + " result_matrix[0][2]=1\n", + " \n", + " #Computer Paper vs. Human\n", + " result_matrix[1][0]=1\n", + " result_matrix[1][2]=2\n", + " \n", + " #Computer Scissors vs. Human\n", + " result_matrix[2][0]=2\n", + " result_matrix[2][1]=1\n", + " \n", + " return result_matrix[nb_comp_play][nb_human_play]" + ] }, { "cell_type": "markdown", @@ -150,10 +194,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def print_outcome(computer_pl,human_pl):\n", + " print('Computer played:',computer_pl)\n", + " print('Human played:', human_pl)\n", + " result=who_wins(computer_pl,human_pl)\n", + " if result==0:\n", + " print('It´s a tie for this round')\n", + " elif result==1:\n", + " print('Computer wins this round')\n", + " else:\n", + " print('Human wins this round')\n", + " return result" + ] }, { "cell_type": "markdown", @@ -168,10 +224,59 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "What is your option (rock/paper/scissors)?rock\n", + "Computer played: rock\n", + "Human played: rock\n", + "It´s a tie for this round\n", + "What is your option (rock/paper/scissors)?paper\n", + "Computer played: paper\n", + "Human played: paper\n", + "It´s a tie for this round\n", + "What is your option (rock/paper/scissors)?scissors\n", + "Computer played: rock\n", + "Human played: scissors\n", + "Computer wins this round\n", + "What is your option (rock/paper/scissors)?erro\n", + "What is your option (rock/paper/scissors)?344\n", + "What is your option (rock/paper/scissors)?rock\n", + "Computer played: scissors\n", + "Human played: rock\n", + "Human wins this round\n", + "What is your option (rock/paper/scissors)?paper\n", + "Computer played: paper\n", + "Human played: paper\n", + "It´s a tie for this round\n", + "What is your option (rock/paper/scissors)?scissors\n", + "Computer played: scissors\n", + "Human played: scissors\n", + "It´s a tie for this round\n", + "What is your option (rock/paper/scissors)?scissors\n", + "Computer played: rock\n", + "Human played: scissors\n", + "Computer wins this round\n" + ] + } + ], + "source": [ + "n=0\n", + "while (ncpu_score:\n", + " print('Human Wins the Game')\n", + "elif player_scorecpu_score:\n", + " print('Human Wins the Game')\n", + "elif player_score