Python Challenge – Solutions

Don’t look if you’re doing the puzzles, or plan on doing them.

I’ve compared my solutions with existing solutions, and some are very similar, sometimes even to the level of variable names, which I find amazing. It’s just a coincidence though, so no silly thoughts.

Level 0 Show

print 2**38

Level 1 Show

alphabet1="abcdefghijklmnopqrstuvwxyz";
alphabet2="cdefghijklmnopqrstuvwxyzab"
from string import maketrans
table=maketrans(alphabet1, alphabet2)

t="""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."""

translated = t.translate(table)
print translated

# the previous url is "map.html", and "map" translates to "ocr"
u = "map"

print u.translate(table)

Level 2 Show

# view source, lots of crap at the end
# solution 1: count the frequency of each
# character, looking for the rarest ones

import string

cnt={}
f=open("level2.txt")
for line in f:
    for c in line:
        cnt[c] = cnt.get(c,0) + 1

print cnt    

# {'\n': 1219, '!': 6079, '#': 6115, '%': 6104, '$': 6046,
# '&': 6043, ')': 6186, '(': 6154,
# '+': 6066, '*': 6034, '@': 6157, '[': 6108, ']': 6152, '_': 6112,
# '^': 6030,
# 'a': 1, 'e': 1, 'i': 1, 'l': 1, 'q': 1, 'u': 1, 't': 1, 'y': 1,
#'{': 6046, '}': 6105}

# the rare ones are e,q,u,a,l,i,t,y
# this is to display just what's needed

Level 3 Show

import re

junk = open("level3.txt").read()
pat=re.compile(r'[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]')
letters = re.findall(pat, junk)
# -> l,i,n,k,e,d,l,i,s,t

Level 4 Show

import urllib
import re

# start with 12345
# read the numbers at the end of the line only
# on 92118, it says to divide by two -> 46059
# on 65667 -> finds peak.html

numbers=re.compile('[\d]*$')
solution=re.compile(r'.html')
nothing="12345"
prefix = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="

while True:
    contents = urllib.urlopen(prefix + nothing).read()
    print contents

    if solution.findall(contents):
        break

    match=numbers.findall(contents)
    if len(match) == 1: # no numbers
        nothing=str(int(nothing)/2)
    else:
        nothing="".join(match)

Level 5 Show

# peak hell -> pickle : this was a stretch, had to get outside help
#

# banner.p -> http://www.pythonchallenge.com/pc/def/banner.p

import pickle, urllib

raw = urllib.urlopen('http://www.pythonchallenge.com/pc/def/banner.p')
obj = pickle.load(raw)

print(dir(obj)) # --> list

for i in range(0,len(obj),1):
    for j in range(0,len(obj[i]),1):
        print obj[i][j][0]*int(obj[i][j][1]),
    print ''

# Though I use fixed fonts, it wasn't obvious to see the banner. I had to
# manually push some characters to the right. Not sure if that's normal
# Also, now that I have access to the solutions for this puzzle, I see
# that some people use lambdas and stuff. It's definitely more elegant,
# maybe next time.

Level 6 Show

# first replace the .html extension of the url, with .zip

import zipfile, re

numbers=re.compile('[\d]*$')
number="90052"
zip = zipfile.ZipFile("channel.zip", "r")
while True:
    fn=number+".txt"
    filecontents = zip.read(fn)
    print zip.getinfo(fn).comment,
    match=numbers.findall(filecontents)
    if len(match) > 1:
        number="".join(match)
    else:
        break

# notice the letters used to spell out the banner