{"id":308,"date":"2013-09-17T21:05:00","date_gmt":"2013-09-17T21:05:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-3\/"},"modified":"2013-09-17T21:05:00","modified_gmt":"2013-09-17T21:05:00","slug":"the-python-challenge-3","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-3\/","title":{"rendered":"The Python Challenge &#8211; 3"},"content":{"rendered":"<p>This one was not so hard.\u00a0 (Or so I thought).\u00a0 I could see that it was a regular expression problem right away.\u00a0 The hardest bit was getting the regex right.<\/p>\n<h3>Python 2.7 Solution<\/h3>\n<pre>import re<br>data = ''.join([line.rstrip() for line in open('3_message.txt')])<br># Looking for the pattern lUUUlUUUl (U = UPPER, l = lower)<br>regex = r'(?<![A-Z])[A-Z]{3}[a-z][A-Z]{3}(?![A-Z])' <br>the_answer = re.findall(regex, data)<br>if len(the_answer) == 1:<br>\u00a0\u00a0\u00a0 print the_answer[0]<br>else:<br>\u00a0\u00a0\u00a0 print \"Sorry, your regex is crap.\u00a0 There were %i matches \" % len(the_answer)<br>\u00a0\u00a0\u00a0 from pprint import pprint<br>\u00a0\u00a0\u00a0 pprint(the_answer)<\/pre>\n<p>But the evil setters were much cleverer than that.\u00a0 I got:<\/p>\n<blockquote><p><span>Sorry, your regex is crap.\u00a0 There were 10 matches <br \/>[&#8216;IQNlQSL&#8217;,<br \/>\u00a0&#8216;OEKiVEY&#8217;,<br \/>\u00a0&#8216;ZADnMCZ&#8217;,<br \/>\u00a0&#8216;ZUTkLYN&#8217;,<br \/>\u00a0&#8216;CNDeHSB&#8217;,<br \/>\u00a0&#8216;OIXdKBF&#8217;,<br \/>\u00a0&#8216;XJVlGZV&#8217;,<br \/>\u00a0&#8216;ZAGiLQZ&#8217;,<br \/>\u00a0&#8216;CJAsACF&#8217;,<br \/>\u00a0&#8216;KWGtIDC&#8217;]<\/span><\/p><\/blockquote>\n<p>This made me sad, even though it was a message to myself.\u00a0 I pondered the problem and then looked at the middle letters.\u00a0 They said &#8220;linked list&#8221;.\u00a0 wtf?!<\/p>\n<p>I looked up linked lists and was none the wiser.\u00a0 I then thought I would just try typing linkedlist in to the url, and it worked.\u00a0 I wonder if Linked Lists are key to the next one.\u00a0 I rewrote the code so it outputted this.<\/p>\n<h3>Better Python 2.7 Solution<\/h3>\n<pre>import re<br>data = ''.join([line.rstrip() for line in open('3_message.txt')])<br>regex = r'(?<![A-Z])[A-Z]{3}[a-z][A-Z]{3}(?![A-Z])'\u00a0 # perfect - 10 matches?<br>the_answer = re.findall(regex, data)<br>print 'You are regex GOD. The answer is %s' % ''.join(word[3] for word in the_answer)<br><\/pre>\n<p><\/p>\n<h3>Python 3.2 Solution<\/h3>\n<pre>import re<br>data = ''.join([line.rstrip() for line in open('3_message.txt')])<br>regex = r'(?<![A-Z])[A-Z]{3}[a-z][A-Z]{3}(?![A-Z])'  # perfect - 10 matches?<br>the_answer = re.findall(regex, data)<br>print ('You are regex GOD. The answer is %s' % ''.join(word[3] for word in the_answer))<br><\/pre>\n<p>I think I am learning slowly.\u00a0 But there were about 50 ways of doing this in the solutions and my regex was very basic.\u00a0 The next one looks pretty basic given my experience with php.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This one was not so hard.\u00a0 (Or so I thought).\u00a0 I could see that it was a regular expression problem right away.\u00a0 The hardest bit was getting the regex right. Python 2.7 Solution import redata = &#8221;.join([line.rstrip() for line in open(&#8216;3_message.txt&#8217;)])# Looking for the pattern lUUUlUUUl (U = UPPER, l = lower)regex = r'(?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[39],"class_list":["post-308","post","type-post","status-publish","format-standard","hentry","tag-python"],"_links":{"self":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/308","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=308"}],"version-history":[{"count":0,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}