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.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 Comments »

 
  • Jhonny says:

    hi, how about the instruction set ? like I for what (lp0 for what.

  • [...] Python Challenge level 5: “peak hell” | UnixWars Python ? Pickle [...]

  • Demoli says:

    Thanks for this, I had no difficulty with the unpickling but just couldn’t figure out what the data meant.

    I totally forgot that you can repeat strings with *, Python continues to impress me as I learn more.

  • g0rg0d says:

    could not get the meaning of the last line.
    “print ”.join(map(lambda pair: pair[0]*pair[1], line))”
    plz explain in little detail. Thanks

  • Ian Durkan says:

    in case anyone else finds this post while wondering WTF the pickled data in Challenge 5 means, it’s a list of lists where each sub-list represents a line of console output. The sublists consist of pairs of characters and numbers of times to repeat the character.

    print ”.join(map(lambda pair: pair[0]*pair[1], line))

    map(F, line) means loop over each element (a pair here) in the list ‘line’, applying F to the element. pair[0] is a character (length-1 string), and pair[1] is a long, so pair[0] * pair[1] produces the character in pair[0] repeated pair[1] times.

  • Thropian says:

    Even after I ran this I couldn’t figure out what all the “#” were for. It took me quite a while to realize I had to stretch the window, which annoyed me.

  • shlomi says:

    my sulution less prety but eazy to understand:
    for line in obj:
    print (”)
    for li in line:
    i = 0
    while i < li[1]:
    sys.stdout.write(li[0])
    i +=1

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>