{"id":89,"date":"2018-10-26T13:27:11","date_gmt":"2018-10-26T17:27:11","guid":{"rendered":"http:\/\/sites.nd.edu\/crivaldi\/?p=89"},"modified":"2018-11-27T16:11:19","modified_gmt":"2018-11-27T20:11:19","slug":"python-in-class-challenge-answers-10-26-18","status":"publish","type":"post","link":"https:\/\/sites.nd.edu\/crivaldi\/2018\/10\/26\/python-in-class-challenge-answers-10-26-18\/","title":{"rendered":"Python In class challenge answers 10-26-18"},"content":{"rendered":"<h1>Python In class challenge answers 10-26-18<\/h1>\n<h2>Here are the answers to the python challenges this week in class. The formatting isn&#8217;t great here, so the link below will show another format if it&#8217;s which might be easier for you to understand\/read.<\/h2>\n<p><a href=\"https:\/\/hackmd.io\/s\/S1oyAag3Q#\">https:\/\/hackmd.io\/s\/S1oyAag3Q#<\/a><\/p>\n<h3># cumulative sum<\/h3>\n<h3><\/h3>\n<p>x=[3,10,4,12,55]<br \/>\ncs=[0]*5<\/p>\n<p>for i in range(0,len(x)):<br \/>\ncs[i]=sum(x[0:(i+1)])<\/p>\n<p>print(cs)<\/p>\n<p>x=[3,10,4,12,55]<br \/>\ncs=list()<\/p>\n<p>for i in range(0,len(x)):<br \/>\ncs.append(sum(x[0:(i+1)]))<\/p>\n<p>print(cs)<\/p>\n<h3>#how long to 5 5&#8217;s<\/h3>\n<p>x=[5,3,2,5,5,1,2,5,3,5,1,5,1]<\/p>\n<p>count=0<br \/>\ni=0<\/p>\n<p>while count &lt; 5:<br \/>\nif x[i]==5:<br \/>\ncount+=1<br \/>\ni+=1<\/p>\n<p>print(i)<\/p>\n<h3>Answers for Lecture 11 extra practice<\/h3>\n<p>#<\/p>\n<p>import pandas as pd<\/p>\n<p># 0 &#8211; calculate sum of female and male wages in wages.csv<br \/>\nwages=pandas.read_csv(&#8220;wages.csv&#8221;,header=0,sep=&#8221;,&#8221;)<\/p>\n<p>femaleSum=0<br \/>\nmaleSum=0<\/p>\n<p>for i in range(0,len(wages),1):<br \/>\nif wages.gender[i]==&#8221;female&#8221;:<br \/>\nfemaleSum=femaleSum+wages.wage[i]<br \/>\nelse:<br \/>\nmaleSum=maleSum+wages.wage[i]<\/p>\n<p>femaleSum<br \/>\nmaleSum<\/p>\n<p>#the two commands below won&#8217;t work &#8211; why?<\/p>\n<p>sum(wages.gender==&#8221;female&#8221;)<br \/>\nsum(wages.gender==&#8221;male&#8221;)<\/p>\n<h3><\/h3>\n<h3>\u00a0Find runs &#8211;<\/h3>\n<h3>This is the super abstract one we went over in Tutorial!<\/h3>\n<p># load file &#8211; available on Sakai<br \/>\nfindRuns=pd.read_csv(&#8220;findRuns.txt&#8221;,header=None,sep=&#8221;\\t&#8221;)<\/p>\n<p># create a variable out that is currently undefined<br \/>\nout=pd.DataFrame(columns=[&#8216;startIndex&#8217;,&#8217;runLength&#8217;])<\/p>\n<p># I will use this variable cur to hold onto the previous number in the vector;<br \/>\n# this is analagous to using findRuns[i-1]<br \/>\ncur=findRuns.iloc[0,0]<br \/>\n#cur=findRuns[i-1]<br \/>\n# this is a counter that I use to keep track of how long a run of repeated values is;<br \/>\n# if there are not repeated values than this count equals 1<br \/>\ncount=1<\/p>\n<p># loop through each entry of our vector (except the 1st one, which we set to cur above)<br \/>\nfor i in range(1,50,1):<br \/>\n# test if the ith value in the vector findRuns equals the previous (stored in cur)<br \/>\nif findRuns.iloc[i,0]==cur:<br \/>\n# test whether count is 1 (we aren&#8217;t in the middle of a run) or &gt;1 (in the middle of a run)<br \/>\nif count==1:<br \/>\n# if the ith value in the vector equals the previous (stored in cur) and count is 1, we<br \/>\n# are at the beginning of a run and we want to store this value (we temporarily store it in &#8216;start&#8217;)<br \/>\nstart=(i-1)<\/p>\n<p># we add one to count because the run continued based on the ith value of findRuns being equal to<br \/>\n# the previous (stored in cur)<br \/>\ncount=count+1<br \/>\n# if the ith value in findRuns is not the same as the previous (stored in cur) we either are not in a run<br \/>\n# or we are ending a run<br \/>\nelse:<br \/>\n# if count is greater than 1 it means we were in a run and must be exiting one<br \/>\nif count&gt;1:<br \/>\n# add a row to &#8216;out&#8217; that will hold the starting positions in the first column and the length<br \/>\n# of runs in the second column; this appends rows to out after finding and counting each run<br \/>\nout.loc[len(out)]=[start,count]<br \/>\n# reset count to 1 because we just exited a run<br \/>\ncount=1<br \/>\n# remember cur holds the previous element in findRuns, so we need to update this after each time<br \/>\n# we go through the for loop<br \/>\ncur=findRuns.iloc[i,0]<br \/>\ncur<br \/>\nout<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python In class challenge answers 10-26-18 Here are the answers to the python challenges this week in class. The formatting isn&#8217;t great here, so the link below will show another format if it&#8217;s which might be easier for you to understand\/read. https:\/\/hackmd.io\/s\/S1oyAag3Q# # cumulative sum x=[3,10,4,12,55] cs=[0]*5 for i in range(0,len(x)): cs[i]=sum(x[0:(i+1)]) print(cs) x=[3,10,4,12,55] cs=list() &hellip; <a href=\"https:\/\/sites.nd.edu\/crivaldi\/2018\/10\/26\/python-in-class-challenge-answers-10-26-18\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Python In class challenge answers 10-26-18&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2790,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[86,29879],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-announcements","category-python"],"_links":{"self":[{"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/users\/2790"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/comments?post=89"}],"version-history":[{"count":5,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/posts\/89\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/media?parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/categories?post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.nd.edu\/crivaldi\/wp-json\/wp\/v2\/tags?post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}