Tuesday, November 08, 2011

[Py] The Python Challenge -- Level 06

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

The main library used is the zipfile module.

Level 06 really made me frustrated but brought me fun and totally new knowledge about ``zipfile'' module.

I read and studied the hints given on the forum. To me, this challenge was little complicated. There were several time when I thought I've found the solution but quickly realized that I was wrong. Level 06 was ``multiple layers puzzle'', at least I think so.

The original version of my code is as the follows. I decided to use dict because the puzzle has to be solved by searching successive keys as a chain.

import zipfile

next_nothing = '90052'
zf = zipfile.ZipFile('channel.zip')
d = {}

# scan through the *.zip and collect the comments
for info in zf.infolist(): 
    filename = info.filename
    filename_s = filename.split('.')[0]

    msg = zf.read(filename).decode()
    next_nothing = msg.split( )[-1]

    comment = info.comment.decode()

    d.update( {filename_s:[next_nothing, comment]} )

# search in the dict d
key1 = '90052'
for i in range( len(d) - 1 ):
    key2 = d.get(key1)[0]
    value = d.get(key1)[1]
    key1 = key2
    print(value, end='')

---
Ref: zipfile – Read and write ZIP archive files

No comments:

Post a Comment