Archive for the ‘Python’ Category

Python Challenge level 8: “working hard?”

Level 8 is easy.

Hint 1: Where is the missing link?
Hint 2: un and pn are provided
Fact 1: the bee links to a page in a protected directory
Fact 2: BZh91AY seems to be a bzip2 header (says Google)

import bz2
un= 'BZh91AY&SYAx....' #complete the string here
pw= 'BZh91AY&SYx9....' #likewise
print "user:",bz2.decompress(un)
print "pass:",bz2.decompress(pw)

Which gives:

user: huge
pass: file

And on to Level 9.

Note that the values of un and pn aren’t complete since they had HEX characters that do not seem to render correctly in the plugin I use for code in WordPress. Have it in mind if you are just copying and pasting your way through the Python Challenge. I advise strongly against that though. The beauty is in the journey itself ;-) If you don’t think highly of the game by now, just wait until we get to the levels for grown-ups.

Read the rest of this entry »

Python Challenge level 7: “smarty”

Level 7 needs the Image module.

Fact 1: The PNG file has a weird grey area, so the information must be encoded there.

import Image
im = Image.open("oxygen.png")
print "Image info:",im.format, im.size, im.mode

#we'll limit the grey zone
y_begin = 0
while True:
    p = im.getpixel((0, y_begin))
    if p[0] == p[1] == p[2]:
        break
    y_begin += 1
x_end = 0
while True:
    p = im.getpixel((x_end, y_begin))
    if not p[0] == p[1] == p[2]:
        break
    x_end += 1
print "Y first coordinate:", y_begin,"nX last coordinate:",x_end

message=[]
for i in range(0,x_end,7):
    p = im.getpixel((i, y_begin))
    message.append(chr(p[0]))
print ''.join(message),
#First run gives: [105, 110, 116, 101, 103, 114, 105, 116, 121]
message=[105, 110, 116, 101, 103, 114, 105, 116, 121]
print '(',''.join([chr(x) for x in message]),')'

After running the script:

smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121] ( integrity )

And that sends us to Level 8

Read the rest of this entry »

Python Challenge level 6: “now there are pairs”

Level 6 deals again with linked lists.

Hint 1: Image is a zipper and there is a comment in the page (<!– <– zip –>). Once we open channel.zip, there are some more hints: “welcome to my zipped list“.
Hint 2: start from 90052
Hint 3: answer is inside the zip

import zipfile,re
idx="90052"
file = zipfile.ZipFile("channel.zip", "r")
history = []
while True:
    history.append(idx)
    data = file.read(idx+".txt")
    print "File",idx+":\t"+ data
    idx="".join(re.findall('[0-9.]',data))
    if len(idx)==1:
        break
#Collect the comments
print ''.join([file.getinfo(i+'.txt').comment for i in history])
File 90052:     Next nothing is 94191
File 94191:     Next nothing is 85503
[...]
File 67824:     Next nothing is 46145
File 46145:     Collect the comments.
****************************************************************
****************************************************************
**                                                            **
**   OO    OO    XX      YYYY    GG    GG  EEEEEE NN      NN  **
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE  NN    NN   **
**   OO    OO XXX  XXX YYY   YY  GG GG     EE       NN  NN    **
**   OOOOOOOO XX    XX YY        GGG       EEEEE     NNNN     **
**   OOOOOOOO XX    XX YY        GGG       EEEEE      NN      **
**   OO    OO XXX  XXX YYY   YY  GG GG     EE         NN      **
**   OO    OO  XXXXXX   YYYYYY   GG   GG   EEEEEE     NN      **
**   OO    OO    XX      YYYY    GG    GG  EEEEEE     NN      **
**                                                            **
****************************************************************
 **************************************************************

… and in hockey.html

it’s in the air. look at the letters.

Hm… the letters in “hockey” are made out of “o, x, y, g, e, n”. Level 7, here we go!

UPDATE: A ‘\’ was missing in the code. Thanks to PSquid for reporting.

Read the rest of this entry »

Python Challenge level 5: “peak hell”

Level 5 is the first challenge specific to Python since it involves the pickle module in charge of Python object serialization.

Hint 1: pronounce it
Fact 1: there is a refference to a file named banner.p in the html source, so we’ll download it and see
Fact 2: once depickled, it looks like a run-length encoding

import urllib,pickle
url='http://www.pythonchallenge.com/pc/def/banner.p'
obj=pickle.load(urllib.urlopen(url))
for line in obj:
    print ''.join(map(lambda pair: pair[0]*pair[1], line))

The output, in a pretty *nix banner command fashion, is:

channel

And that is Level 6.

Read the rest of this entry »

Python Challenge level 4: “follow the chain”

Level 4 requires some web scraping. Nice :)

Hint 1: urllib may help. DON’T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough
Hint 2: linkedlist.php?nothing=12345

import urllib,re
url='http://www.pythonchallenge.com/pc/def/
     linkedlist.php?nothing='
seed="12345"
for i in range(400):
    text=urllib.urlopen(url+seed).read()
    seed="".join(re.findall(r"nothing is (\d+)",text))
    try :
        int(seed)
        print "   Next:",seed
    except :
        print "   Last:",text
        break

When we get to 92118 the message is:

Yes. Divide by two and keep going.

So we’ll reset the seed to 46059

peak

Great. To Level 5.

Read the rest of this entry »

Python Challenge level 3: “re”

Level 3 is the last of these initial text processing problems. Everything quite easy for now.

Hint 1: One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

#!/usr/bin/env python
import re
mess = open("03.txt").read()
print ''.join(re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]',
 mess))

linkedlist

From linkedlist.html we’re sent to linkedlist.php, and up to Level 4.

Read the rest of this entry »

Python Challenge level 2: “ocr”

Hint 1: recognize the characters. maybe they are in the book, but MAYBE they are in the page source.
Hint 2: find rare characters in the mess below (this hint is found looking at the source, and I stored it in file 02.txt for convinience). Looking for clues in whatever information you get will become a must from now on. By rare I understand ‘few appearences’, not neccesarily strange characters:

mess = open("02.txt").read()
dict = {}
for ch in mess:
    dict[ch] = dict.get(ch, 0) + 1
print "".join(ch for ch in mess if dict[ch] == 1)
taher@borg:~/src/python_challenge$ python 02.py
equality

Well. Up to Level 3.

Read the rest of this entry »

Python Challenge level 1: “what about making trans?”

Level 1 gets a bit more interesting.
Hint 1: k->m, o->q, e->g

import string
src = string.lowercase[:26]
dst = string.lowercase[2:26] + 'ab'
trans = string.maketrans(src, dst)
text = "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."
print text.translate(trans)

That gives:

i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that’s why this text is so long. using string.maketrans() is recommended. now apply on the url.

Ha, funny! Ok then, an extra:

print "map".translate(trans)

ocr

That gets us to Level 2.

Read the rest of this entry »

Python Challenge level 0: “warming up!”

I recently discovered the Python Challenge web site. An interesting way to pick up a programming language. It claims to be the first programming riddle on the net, and I have been told that it’s shaped after another onlin games that predates this one by more than a year, notprOn. It turns out the Python Challenge has been around since 2005, but I had no clue about Python by then anyway. It’s really a pity because I’m addicted to the game as it is, so I’m quite sure I would have started learning a lot earlier for amusement’s sake.

Some of the puzzles are a bit obscure IMHO, but nevertheless it’s worth it. It has 33 levels and it gradually introduces you to Python specifics. Most levels could be solved with other languages, but it’s a good way of showing Python’s impressive resources.

I’ve been talking to some friends about it, and it looks like a whole new bunch of addicts is on the way ;) Since I really do love puzzles, one of them conviced me to post every solution I come up with so that we can all compare our results. I’ll try, at least until I get completely stuck. Hopefully I’ll find enough hints in the forums and among these friends. I’m allready at level 17, by the way. I’ll be posting on a more or less daily basis so that everyone can catch up.

Warning: You should only read these Python Challenge Chronicles of mine if you are really stuck on some point. Otherwise you’ll only end up spoiling your fun. As in every game, the interest is in onvercoming the challenges on your own. When you play in God-mode (prince megahit, anyone?) it get’s boring pretty fast. Besides… the challenges are really well thought. You should give it a try, resort to the forums for hints when you’re stuck, and only after that …

Level 0 doesn’t even need a script to be solved. It’s just a warm up to introduce the player to the game. The hint given is ‘try to change the url’ and an image showing 2^38, so we use our Python interpreter to find out the result:

>>> 2**38
274877906944L

And with that we’re on our way to all the fun on Level 1

Read the rest of this entry »