Sunday, October 30, 2011

[Py] The Python Challenge -- Level 04

Here is a note of my solution to solve Level 04 of The Python Challenge.

The main library used is the urllib.parse to parse the url and the urllib.request for retrieving the web page source.

I had been totally lost at the beginning. The page showed only the message ``follow the chain'' and the url said ``linkedlist'' so I thought that must be something relevant to linked list in Python.

I searched on the web and found no useful clue. I felt a little bit frustrated.

After investigating the web page of Level 04, I noticed the url ended with php instead conventional html. Actually I didn't know what was the difference between the php and html pages, but I guessed there must be some clues.  When the curser moved to the picture of the web page, the url changed to something ended with ``nothing=12345'', and the link led to another page showed ``and the next nothing is 44827.'' I replaced ``12345'' with ``44827'' and got another page with new digits.

So, I assumed the digits had something to do with the linked list. I was wrong.

It took me two days to tackle the puzzle but got no useful advances. Then I decided to get some hints from the forum of The Python Challenge. The hint that led me out is the one given by thesamet:
if your hands are getting tired, maybe get someone else to do it for you...
Yes. But who can do it for me? Finally I got that: do the dirty work by the script automatically for you. It is a shame not to think about such a basic principle. To do dirty or boring works by writing small script should be a institute of a hacker or of one who wants to be a hacker.

With the enlightened idea, I began to write the code and finally got the solution. :-)

The original version of the my code (in Python 3) is as follows.

from urllib.parse import urlparse
from urllib.request import urlopen

nothing = '12345'

while nothing.isdigit():
    # get modified url
    obj = urlparse('' + nothing) 
    # get the web page source
    src = urlopen(obj.geturl()).read()
    # extract the part of digits, and convert it from bytes to str
    nothing = src.split( )[-1].decode()

Notes: There are two sequence types named bytes and str. To convert one to another, use encode and decode.


  1. Anonymous7:16 AM

    This worked for me, thank you.

  2. thank you actually. i thought there's sth wrong with the link. but thats a riddle. how awesome is that.