Solutions for Python Challenge

You may prefer tackling the challenge by yourself first before referring to my solutions 😀. Off you go: Python Challenge

Challenge 0

Just get 2^38 and substitute the result into the URL.

Challenge 1

Code:

s = "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."
#s = "map"
translated = ""
for c in s:
   if c.isalpha() and c < 'x':
     translated += chr(ord(c)+2)
   elif c.isalpha():
     translated += chr(ord(c)-24)
   else:
     translated += c
print(translated)

It's a simple mapping - every letter in the encrypted text should be moved by 2 (hence we get 'm' for 'k', 'o' for 'm' etc). Notice the special case: when the letter is y and z, you can't just refer to the ascii table as it will return you non-letter. Basically 'y' is for 'a' and 'z' is for 'b', so a special handling is required. Once you decode the helper text, the next part is just applying it to the URL.

Challenge 2

Code:

import string
s = """ 
(the text you get from the source)
"""
translated = ""

for c in s:
  if c not in string.punctuation and c != '\n':
    translated += c
print(translated)

I have to admit that this is not the MOST elegant solution (maybe regex could handle it better, but I'm not a fan of regex afterall). When inspecting the page source you should be able to find a big chunck of text that's mostly composed of punctuation marks. You can then parse it using string.punctuation.

Challenge 3

Code: (not using regex here)

s = """(the big string in the source code)"""
s = s.replace('\n', '').replace('\r', '')

res = ""
i = 0
for i in range(len(s)-8):
  current = s[i:i+9]
  if current[0].islower() and current[1:4].isupper() and current[5:8].isupper() and current[-1].islower() and current[4].islower():
    res += current[4]
print(res)

I know, I know very well that the solution here is ugly. BUT IT WORKS! Basically you should be looking for patterns like xXXXaXXXx, where a is the desirable letter. It's a bit elusive to read the hint this time, but thanks to this post I finally got it to work (not in a nice way).

Challenge 4

import urllib3

http = urllib3.PoolManager()

path = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
num = "12345"
res = [num]

for i in range(400):
    r = http.request('GET', path+num)
    if r.status == 200:
        num = r.data.split()[-1]
        res.append(num)

print(res)

The hint is buried in the back again: don't try to loop it for over 400 times. It will manifest itself at index 357 (am I disclosing too much?)

Challenge 5

import pickle
from urllib2 import urlopen

src = "http://www.pythonchallenge.com/pc/def/banner.p"

test = pickle.load(urlopen(src))
for line in test:
    print("".join([k * v for k, v in line]))

This is clever hahahaha. I didn't think of the solution myself (thanks for the reference!), but with an extension like .p I should have realized that it's related to pickle. Once you unpickle it, the way to put it back also amuses me (list of tuples, not fun to deal with huh).

Challenge 6