{"id":310,"date":"2013-09-13T16:56:00","date_gmt":"2013-09-13T16:56:00","guid":{"rendered":"http:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-1\/"},"modified":"2013-09-13T16:56:00","modified_gmt":"2013-09-13T16:56:00","slug":"the-python-challenge-1","status":"publish","type":"post","link":"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-1\/","title":{"rendered":"The Python Challenge &#8211; 1"},"content":{"rendered":"<p>So, my first effort in about 5 minutes gave me the following secret message:<\/p>\n<blockquote><p>The answer is: &#8221; fone you biblr rp{lsj{re ir zy f{lb. rf{rs wf{r comnureps {pe dop. boilg ir il zy f{lb is ileddicielr {lb rf{r&#8217;s wfy rfis revr is so jolg. usilg srpilg.m{kerp{ls() is pecommelbeb. low {nnjy ol rfe upj.&#8221;<\/p><\/blockquote>\n<p>Not so good. This was better:<\/p>\n<blockquote><p>The answer is: &#8220;i&#8221;hope&#8221;you&#8221;didnt&#8221;tr{nsl{te&#8221;it&#8221;|y&#8221;h{nd0&#8243;th{ts&#8221;wh{t&#8221;computers&#8221;{re&#8221;for0&#8243;doing&#8221;it&#8221;in&#8221;|y&#8221;h{nd&#8221;is&#8221;inefficient&#8221;{nd&#8221;th{t)s&#8221;why&#8221;this&#8221;text&#8221;is&#8221;so&#8221;long0&#8243;using&#8221;string0m{ketr{ns*+&#8221;is&#8221;recommended0&#8243;now&#8221;{pply&#8221;on&#8221;the&#8221;url&#8221;<\/p><\/blockquote>\n<p>And this was the last wrong one:<\/p>\n<blockquote><p>The answer is: &#8220;i hope you didnt tr{nsl{te it |y h{nd. th{ts wh{t computers {re for. doing it in |y h{nd is inefficient {nd th{t&#8217;s why this text is so long. using string.m{ketr{ns() is recommended. now {pply on the url&#8221;<\/p><\/blockquote>\n<p>So here was the message:<\/p>\n<blockquote><p>The answer is: &#8220;i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that&#8217;s why this text is so long. using string.maketrans() is recommended. now apply on the url&#8221;<\/p><\/blockquote>\n<p>string.maketrans()!? My code looked like this:<\/p>\n<pre>question = \"g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.\"<br>output = 'The answer is: \\\"'<br>for x in range (0 , len(question)-1):<br>    # 97 - 122<br>    if ord(question[x]) >= 97 and ord(question[x]) <= 120:<br>        output += chr(ord(question[x]) + 2)<br>    elif ord(question[x]) >= 121 and ord(question[x]) <= 122:<br>        output += chr(ord(question[x]) - 24)<br>    else:<br>        output += question[x]<br>print output + '\\\"'<\/pre>\n<p><\/p>\n<h3>Python 2.7.5 Solution <\/h3>\n<p>But maketrans sounded fun, so I quickly did this.<\/p>\n<pre>from string import maketrans   # Required to call maketrans function.<br>question = \"g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.\"<br>output = 'The answer is: \\\"'<br>intab = \"abcdefghijklmnopqrstuvwxyz\"<br>outtab = \"cdefghijklmnopqrstuvwxyzab\"<br>trantab = maketrans(intab, outtab)<br>print question.translate(trantab);<\/pre>\n<p>Which was a lot faster, cleaner and easier. But didn&#8217;t work at all with Python 3.0 which died on &#8220;TypeError: maketrans arguments must be bytes objects&#8221;.\u00a0 I found out quickly that I needed to declare the intab and outtab with a &#8220;b&#8221; onthe front so they became byte objects.\u00a0 That, and the changed &#8220;print&#8221; call and I was done.<\/p>\n<h3>Python 3.0 Solution<\/h3>\n<pre>from string import maketrans   # Required to call maketrans function.<br>question = \"g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.\"<br>output = 'The answer is: \\\"'<br>intab = b\"abcdefghijklmnopqrstuvwxyz\"<br>outtab = b\"cdefghijklmnopqrstuvwxyzab\"<br>trantab = maketrans(intab, outtab)<br>print (question.translate(trantab));\u00a0<\/pre>\n<pre>\u00a0<\/pre>\n<p>In the answers they use string.ascii_lowercase for b&#8221;abcdefghijklmnopqrstuvwxyz&#8221; and\u00a0   string.ascii_lowercase[2:]+string.ascii_lowercase[:2] for b&#8221;cdefghijklmnopqrstuvwxyzab&#8221;, which is just showing off.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, my first effort in about 5 minutes gave me the following secret message: The answer is: &#8221; fone you biblr rp{lsj{re ir zy f{lb. rf{rs wf{r comnureps {pe dop. boilg ir il zy f{lb is ileddicielr {lb rf{r&#8217;s wfy rfis revr is so jolg. usilg srpilg.m{kerp{ls() is pecommelbeb. low {nnjy ol rfe upj.&#8221; Not &hellip; <a href=\"https:\/\/thomas.w-p.me.uk\/blog\/2013\/09\/the-python-challenge-1\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Python Challenge &#8211; 1<\/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-310","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\/310","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=310"}],"version-history":[{"count":0,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas.w-p.me.uk\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}