{"id":307,"date":"2013-09-18T21:31:00","date_gmt":"2013-09-18T21:31:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-4\/"},"modified":"2013-09-18T21:31:00","modified_gmt":"2013-09-18T21:31:00","slug":"the-python-challenge-4","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-4\/","title":{"rendered":"The Python Challenge &#8211; 4"},"content":{"rendered":"<p>I thought I would write my first python function for this one.\u00a0 They appear to need to be at the top of the script though with a little casual searching I couldn&#8217;t confirm that.<\/p>\n<p><\/p>\n<h3>Python 2.7.2 Solution<\/h3>\n<p>This got me there but is pretty flaky and needed manually fixing to get to the answer.<\/p>\n<pre>import urllib, re<br>count = 1<br><br>def getNextCode(lastCode):<br>\u00a0\u00a0\u00a0 while True:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 page = urllib.urlopen('http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s' % lastCode).read()<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 code = re.findall('(?<=and the next nothing is )\\w*',page)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Code %i is %s.\u00a0 \\nPage message was '%s'.\\nPage tried was http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s'.\\n\" % (count, code[0], page, lastCode)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return code<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 except (IndexError, NameError):<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print \"Error: Page message was '%s'\\nPage was: http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s'.\" % (page,lastCode)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return False<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <br>nextCode = getNextCode('12345')<br><br>count = 1<br><br>for count in range (1,400):<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if count == 84:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nextCode[0] = int(nextCode[0])\/2 <br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nextCode = getNextCode(nextCode[0])<\/pre>\n<p><\/p>\n<h3>Python 3.2.3 Solution<\/h3>\n<p>This deals with the message that you must divide by two when you get 16044 as a code. It also loads the page.\u00a0 The 3.2.3 has a very different urllib approach.\u00a0 3.2.3 seems to do crazy stuff with variable types.<\/p>\n<pre>import urllib.request, re, webbrowser<br>count = 1<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <br>def getNextCode(lastCode):<br>\u00a0\u00a0\u00a0 while True:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if lastCode == '16044': lastCode = str(16044 \/ 2)<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 page = urllib.request.urlopen('http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s' % lastCode).read()\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 code = re.findall(r'(?<=and the next nothing is )\\w*',page.decode(\"utf-8\"))<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print (\"\\nCode %i is %s.\\nPage tried was http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s'.\\nPage message was '%s'.\" % (count, code[0], lastCode, page.decode(\"utf-8\")))<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return code<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 except (IndexError, NameError):<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print (\"Error: Page message was '%s'\\nPage was: http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s'.\" % (page,lastCode))<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return False<br>\u00a0\u00a0\u00a0\u00a0\u00a0 <br>nextCode = getNextCode('12345')<br><br>count = 1<br><br>for count in range (1,400):<br>\u00a0\u00a0\u00a0 if nextCode != False:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 answer = nextCode[0]<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nextCode = getNextCode(nextCode[0])<br>\u00a0\u00a0\u00a0 else:<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 new = 2 # open in a new tab, if possible<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 test4url = \"http:\/\/www.pythonchallenge.com\/pc\/def\/\" + urllib.request.urlopen('http:\/\/www.pythonchallenge.com\/pc\/def\/linkedlist.php?nothing=%s' % answer).read().decode(\"utf-8\")<br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 webbrowser.open(test4url,new=new)\u00a0 <br>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break<br>print (\"Done.\")<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I thought I would write my first python function for this one.\u00a0 They appear to need to be at the top of the script though with a little casual searching I couldn&#8217;t confirm that. Python 2.7.2 Solution This got me there but is pretty flaky and needed manually fixing to get to the answer. import &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-4\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Python Challenge &#8211; 4<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/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-307","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\/307","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=307"}],"version-history":[{"count":0,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}