{"id":309,"date":"2013-09-17T19:21:00","date_gmt":"2013-09-17T19:21:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-2\/"},"modified":"2020-08-23T08:50:11","modified_gmt":"2020-08-23T08:50:11","slug":"the-python-challenge-2","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-2\/","title":{"rendered":"The Python Challenge &#8211; 2"},"content":{"rendered":"<p>This challenge is interesting in that it is confirming to me how inexperienced my programming actually is.  I can get to the solution but the answer shows how limited I am.  Here is my solution to 2 (I&#8217;d copied and pasted the text from the source to a document 2_message.txt.)<\/p>\n<h3>My Python 2.7 Solution<\/h3>\n<pre># find rare characters in the mess below:<br><br>maxoccurence = 1 #how many do I think is rare?<br><br># load up the file<br>s = open(r\"2_message.txt\", \"r\").read()<br>#find the unique characters<br>uniqueones = set(s)<br><br>#find the rare characters (<= maxoccurence) and their position<br>rareones = []<br>for one in uniqueones:<br>    position = []<br>    if s.count(one) <=1:<br>        position.append(one)<br>        position.append(s.index(one))<br>        rareones.append(position)<br><br><br>#rareones is now a multi dimensional list with the character and its position<br>from operator import itemgetter<br>solution = \"\"<br>for final in sorted(rareones, key=itemgetter(1)):<br>    solution += final[0]<br>print solution  <\/pre>\n<p>But here is the first of  the suggested solutions <\/p>\n<pre>import collections<br>data = ''.join([line.rstrip() for line in open('2_message.txt')])    <br>OCCURRENCES = collections.OrderedDict()<br>for c in data: OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1<br>avgOC = len(data) \/\/ len(OCCURRENCES)<br>print ''.join([c for c in OCCURRENCES if OCCURRENCES[c] < avgOC]) <\/pre>\n<p>I want to understand this!  So I spend as much time working it out as I do solving the first problem.\u00a0 This is a great way of learning. I have had to learn what <a href=\"http:\/\/docs.python.org\/2\/library\/stdtypes.html#typesmapping\">a Dictionary is in python<\/a> (a sort of associative 2 dimensional array).\u00a0 Link this to <a href=\"http:\/\/docs.python.org\/2\/library\/collections.html\">the collections library<\/a> and you have got something pretty powerful.<br \/>The code appears to<\/p>\n<ol>\n<li>Load the data in to a massive string, removing the carriage returns.<\/li>\n<li>Make a container \"Dictionary\" for the characters <\/li>\n<li>Whiz through the massive string using the character as a key and simply adding 1 to the number attached to the key.\u00a0\u00a0<\/li>\n<li>Getting the integer (floor) average (\/\/) for each letter if they were distributed evenly.<\/li>\n<li>Concatenating all the items in the Dictionary that are less frequent than the average<\/li>\n<\/ol>\n<p>This is actually not that different to what I did!!!\u00a0 Just much more efficient. <\/p>\n<pre>[c for c in OCCURRENCES if OCCURRENCES[c] < avgOC]<\/pre>\n<p>Is very clever.\u00a0 It is one line that only outputs a string if the occurrences are less than average.\u00a0 I am still wrapping my head around it.<\/p>\n<h3>My Python 3.2.3 Solution<\/h3>\n<pre># find rare characters in the mess below:<br><br># official version<br>import collections<br>data = ''.join([line.rstrip() for line in open('2_message.txt')])    <br>OCCURRENCES = collections.OrderedDict()<br>for c in data: OCCURRENCES[c] = OCCURRENCES.get(c, 0) + 1<br># avgOC = len(data) \/\/ len(OCCURRENCES)<br>avgOC = 1<br>print (''.join([c for c in OCCURRENCES if OCCURRENCES[c] == avgOC]) )<\/pre>\n<p>This just needed the modification to \"Print\" though I did find on python 3 it didn't run because there was no collections library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This challenge is interesting in that it is confirming to me how inexperienced my programming actually is. I can get to the solution but the answer shows how limited I am. Here is my solution to 2 (I&#8217;d copied and pasted the text from the source to a document 2_message.txt.) My Python 2.7 Solution # &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Python Challenge &#8211; 2<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[39],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-general","tag-python"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/comments?post=309"}],"version-history":[{"count":1,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":1010,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/309\/revisions\/1010"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}