Skip to content

Commit a13c62f

Browse files
18
1 parent f9b0ca6 commit a13c62f

File tree

7 files changed

+43
-33
lines changed

7 files changed

+43
-33
lines changed

18-1.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

18.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
def main():
55
left, right = first_step()
6-
second_step(left, right)
6+
diff = second_step(left, right)
7+
third_step(diff)
78

89

910
def first_step():
@@ -22,7 +23,34 @@ def first_step():
2223

2324

2425
def second_step(left, right):
25-
pass
26+
diff = difflib.ndiff(left.splitlines(keepends=True), right.splitlines(keepends=True))
27+
result = "".join(diff)
28+
return result
29+
30+
31+
def third_step(diff):
32+
plus = open("swan/plus.png", "wb")
33+
subtraction = open("swan/subtraction.png", "wb")
34+
space = open("swan/space.png", "wb")
35+
lines = diff.split("\n")
36+
for line in lines:
37+
if len(line) > 0:
38+
first_byte = line[0]
39+
if first_byte == "+":
40+
write_file(plus, line)
41+
elif first_byte == "-":
42+
write_file(subtraction, line)
43+
elif first_byte == " ":
44+
write_file(space, line)
45+
46+
47+
def write_file(file, strings):
48+
sub_strings = strings[2:]
49+
ary = sub_strings.split(" ")
50+
for point in ary:
51+
if point.isalnum():
52+
b = bytes.fromhex(point)
53+
file.write(b)
2654

2755

2856
if __name__ == "__main__":

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,21 @@
22
Solve Problems in http://www.pythonchallenge.com
33

44
#### 18
5+
```python
6+
import difflib
7+
```
58
- the obvious different between two picture is brightness.
69

710
```shell
811
gunzip deltas.gz
912
```
13+
- png file start with: 0x89 0x50 0x4E 0x47 0x0D 0x0A 0x1A 0x0A
14+
- 0x89 is png's magic number,all jpg file start with 0xFF
15+
- 0x50 decode in utf-8 is P, 0x4E for N, 0x47 for G
16+
- use three tag "+", "-", " " to describe the difference between two files
17+
> when you say a php file, there is some function in it
18+
- Loop: http://www.pythonchallenge.com/pc/def/linkedlist.php
19+
- RPC: http://www.pythonchallenge.com/pc/phonebook.php
1020

1121
#### 17
1222
result: http://www.pythonchallenge.com/pc/return/balloons.html

18-2.py renamed to read-bin-file.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
def main():
2-
file = open("swan/plus.png", "rb")
1+
def read_bin_file():
2+
file = open("evil/0.jpg", "rb")
33
bs = []
44
while True:
55
b = file.read()
@@ -11,4 +11,4 @@ def main():
1111

1212

1313
if __name__ == "__main__":
14-
main()
14+
read_bin_file()

swan/plus.png

12.1 KB
Loading

swan/space.png

28.1 KB
Loading

swan/subtraction.png

10.8 KB
Loading

0 commit comments

Comments
 (0)