Solutions for the Python Challenge

Show possible solutions for: All levels; level: previous, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, next

Possible solution for level 18

URI:
http://www.pythonchallenge.com/pc/return/balloons.html
Log-in data:
Username: huge; password: file
Comment:
Possible Solution:
import gzip, difflib

# Download the GNU zip file from:
# http://www.pythonchallenge.com/pc/return/deltas.gz

h = gzip.open("deltas.gz")
d = difflib.Differ()

part_1, part_2 = [], []
file_1, file_2, file_3 = [], [], []

for line in h:
    part_1.append(line[0:53])
    part_2.append(line[56:-1])

h.close()

for line in list(d.compare(part_1, part_2)):
    if line[0] == "+":
        file_1.append(line[2:])
    elif line[0] == "-":
        file_2.append(line[2:])
    else:
        file_3.append(line[2:])

for n, data in enumerate((file_1, file_2, file_3)):
    temp = []

    for line in data:
        temp.extend([chr(int(o, 16)) for o in line.strip().split(" ") if o])

    h = open("%s.png" % (n + 1), "wb")
    h.writelines(temp)
    h.close()

    # The third file is corrupt and won't be properly displayed by most image
    # viewers. It should work in Gecko-based browsers such as Firefox, though.

(Download this source code.)

Code word for next challenge:
../hex/bin

By Holger Thölking, May 29th, 2007.