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.
hi, how about the instruction set ? like I for what (lp0 for what.
[...] Python Challenge level 5: “peak hell” | UnixWars Python ? Pickle [...]
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.
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
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.
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.
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