Skip to content

Commit 2c55790

Browse files
solved 27
1 parent 1e0a2e3 commit 2c55790

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

Diff for: 27.py

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import download_file
22

33
from PIL import Image
4+
import bz2
5+
import py2keywords
46

57

68
def main():
@@ -10,9 +12,12 @@ def main():
1012
user = "butter"
1113
password = "fly"
1214
# first_step(url, file_path, user, password)
13-
raw, result = second_step(file_path)
15+
im, raw, result = second_step(file_path)
1416
raw_ary, result_ary = third_step(raw, result)
15-
fourth_step(raw_ary, result_ary)
17+
raw_str, result, index = fourth_step(raw_ary, result_ary)
18+
hint_path = "zigzag/{}".format("hint.png")
19+
five_step(im, index, hint_path)
20+
six_step(raw_str)
1621

1722

1823
def first_step(url, file_path, user, password):
@@ -27,7 +32,7 @@ def second_step(file_path):
2732
value = "".join([chr(palette[i]) for i in range(len(palette))])
2833
table = str.maketrans(index, value)
2934
result = raw.decode("latin1").translate(table).encode("latin1")
30-
return raw, result
35+
return im, raw, result
3136

3237

3338
def third_step(raw, result):
@@ -42,7 +47,44 @@ def third_step(raw, result):
4247

4348

4449
def fourth_step(raw_ary, result_ary):
45-
pass
50+
zero = raw_ary.pop(0)
51+
raw_ary.append(zero)
52+
assert len(raw_ary) == len(result_ary)
53+
raw = []
54+
result = []
55+
index = []
56+
for i in range(len(raw_ary)):
57+
raw_i = raw_ary[i]
58+
result_i = result_ary[i]
59+
if raw_i != result_i:
60+
raw.append(raw_i)
61+
result.append(result_i)
62+
index.append(i)
63+
raw_str = b""
64+
for i in range(len(raw)):
65+
raw_str += chr(raw[i]).encode("latin1")
66+
return raw_str, result, index
67+
68+
69+
def five_step(im, index, hint_path):
70+
im2 = Image.new("RGB", im.size)
71+
colors = [(255, 255, 255)] * (im2.size[0] * im2.size[1])
72+
for i in index:
73+
colors[i] = (0, 0, 255)
74+
im2.putdata(colors)
75+
im2.save(hint_path)
76+
print("Hint in {}".format(hint_path))
77+
78+
79+
def six_step(raw_str):
80+
raw_dec = bz2.decompress(raw_str)
81+
strs = raw_dec.decode("latin1").split(" ")
82+
results = set()
83+
for i in range(len(strs)):
84+
s = strs[i]
85+
if not s in py2keywords.kwlist:
86+
results.add(s)
87+
print(results)
4688

4789

4890
if __name__ == "__main__":

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# pythonchallenge
22
Solve Problems in http://www.pythonchallenge.com
33

4+
#### 27
5+
result: http://www.pythonchallenge.com/pc/ring/bell.html auth=(repeat, switch)
6+
- author use python2 keyword, but there is some different with python3
7+
- use assert to do assert
8+
- tobytes() method for Image in PIL: Return image as a bytes object.
9+
- getpalette() method for Image in PIL: Returns the image palette as a list.
10+
11+
412
#### 26
513
result: http://www.pythonchallenge.com/pc/hex/speedboat.html
614
```python

Diff for: py2keywords.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

Diff for: zigzag/hint.png

4.57 KB
Loading

0 commit comments

Comments
 (0)