Skip to content

Commit 5da5835

Browse files
committed
Add level_32.py and update many codes
1 parent 2844ee5 commit 5da5835

20 files changed

+468
-164
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ name = "pypi"
77
pillow = "*"
88
requests = "*"
99
networkx = "*"
10+
ortools = "*"
11+
sympy = "*"
1012

1113
[dev-packages]
1214

Pipfile.lock

Lines changed: 235 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

level_04.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313
def catch(text, pattern=r'<!--(.*?)-->', cnt=0):
1414
return re.findall(pattern, text, re.DOTALL)[cnt]
1515

16+
def analysis(text):
17+
NOTHING = catch(text, r'nothing=([0-9]+)">')
18+
MAX_RANGE = catch(text, r'([0-9]+) times is more than enough')
19+
return NOTHING, MAX_RANGE
1620

17-
def solve(url, NOTHING, MAXRANGE):
21+
def solve(url, nothing, maxrange):
1822
reo = re.compile(r'and the next nothing is ([0-9]+)')
19-
nothing = NOTHING
23+
next = nothing
24+
text = ''
2025
print("Following the nothing chain!")
21-
for i in range(int(MAXRANGE)):
22-
response = requests.get(url, params={'nothing': nothing})
26+
for i in range(int(maxrange)):
27+
response = requests.get(url, params={'nothing': next})
2328
text = response.text
2429
m = reo.search(text)
2530
print('{}: {}'.format(i, text))
2631
if m:
27-
nothing = m.group(1)
32+
next = m.group(1)
2833
elif text.find(r'Divide by two') > -1:
29-
nothing = int(nothing) / 2
34+
next = int(next) / 2
3035
else:
3136
break
3237
print("Done\n")
@@ -35,11 +40,9 @@ def solve(url, NOTHING, MAXRANGE):
3540

3641
if __name__ == "__main__":
3742
r = requests.get(url)
38-
NOTHING = catch(r.text, r'nothing=([0-9]+)">')
39-
MAXRANGE = catch(r.text, r'([0-9]+) times is more than enough')
40-
41-
# NOTHING=12345, MAXRANGE=400
42-
answer = solve(url, NOTHING, MAXRANGE)
43+
NOTHING, MAX_RANGE = analysis(r.text)
44+
# NOTHING=12345, MAX_RANGE=400
45+
answer = solve(url, NOTHING, MAX_RANGE)
4346
# peak.html
4447
print(PREFIX + answer)
4548
# http://www.pythonchallenge.com/pc/def/peak.html

level_05.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ def solve(url):
2020
print("Unpacking the message:")
2121
something = requests.get(url).content
2222
banner = pickle.load(BytesIO(something))
23+
result_lines = []
2324
for line in banner:
24-
print("".join(ch * count for ch, count in line))
25+
result_lines.append("".join(ch * count for ch, count in line))
26+
return "\n".join(result_lines)
2527

2628

2729
if __name__ == "__main__":
2830
r = requests.get(url)
2931
src = catch(r.text, r'<peakhell src="(.*?)"/>')
3032
answer = solve(PREFIX + src)
33+
print(answer)
3134
# channel
3235

3336
# http://www.pythonchallenge.com/pc/def/channel.html

level_06.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ def analysis(something):
2020
with zipfile.ZipFile(BytesIO(something), 'r') as channel:
2121
readme = channel.read('readme.txt').decode()
2222
NOTHING = catch(readme, r'start from ([0-9]+)')
23-
MAXRANGE = len(channel.infolist())
24-
return NOTHING, MAXRANGE
23+
MAX_RANGE = len(channel.infolist())
24+
return NOTHING, MAX_RANGE
2525

2626

27-
def solve(something, NOTHING, MAXRANGE):
27+
def solve(something, nothing, MAXRANGE):
2828
reo = re.compile(r'nothing is ([0-9]+)')
29-
nothing = NOTHING
29+
nothing = nothing
3030
print("Following the nothing chain and picking up the crumbs!")
3131
with zipfile.ZipFile(BytesIO(something), 'r') as channel:
32-
comments = ""
32+
comments = []
3333
for i in range(int(MAXRANGE)):
3434
name = '{}.txt'.format(nothing)
3535
text = channel.read(name).decode()
36-
comments += channel.getinfo(name).comment.decode()
36+
comments.append(channel.getinfo(name).comment.decode())
3737
m = reo.search(text)
3838
print('{}:{}'.format(i, text))
3939
if m:
4040
nothing = m.group(1)
4141
else:
4242
break
4343
print("Done\n")
44-
print(comments)
44+
return "".join(comments)
4545

4646

4747
if __name__ == "__main__":
@@ -50,6 +50,7 @@ def solve(something, NOTHING, MAXRANGE):
5050
NOTHING, MAXRANGE = analysis(something)
5151
# NOTHING=90052, MAXRANGE = 910
5252
answer = solve(something, NOTHING, MAXRANGE)
53+
print(answer)
5354
# oxygen
5455

5556
# http://www.pythonchallenge.com/pc/def/oxygen.html

level_09.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ def catch(text, pattern=r'<!--(.*?)-->', cnt=0):
1919
def solve(something):
2020
first, second = re.findall(
2121
r'first:(.*?)second:(.*)', something.replace('\n', ''))[0]
22-
L1 = list(map(int, first.split(',')))
23-
L2 = list(map(int, second.split(',')))
22+
l1 = list(map(int, first.split(',')))
23+
l2 = list(map(int, second.split(',')))
2424

25+
return l1, l2
26+
27+
28+
def plot(l1, l2):
2529
im = Image.new("RGB", (640, 480))
2630
draw = ImageDraw.Draw(im)
27-
draw.polygon(L1)
28-
draw.polygon(L2)
31+
draw.polygon(l1)
32+
draw.polygon(l2)
2933

3034
im.show()
3135

@@ -34,6 +38,7 @@ def solve(something):
3438
r = requests.get(url)
3539
something = catch(r.text, cnt=1)
3640
answer = solve(something)
41+
plot(*answer)
3742
# bull graph
3843

3944
# http://huge:file@www.pythonchallenge.com/pc/return/bull.html

level_11.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ def solve(something):
1515
im = Image.open(BytesIO(something))
1616
data = list(im.getdata())
1717
im.putdata(data[::2] + data[1::2])
18+
return im
19+
20+
21+
def plot(im):
1822
im.show()
1923

2024

2125
if __name__ == "__main__":
2226
r = requests.get(url)
2327
something = r.content
2428
answer = solve(something)
25-
# evil
29+
plot(answer)
30+
# 'evil' in the graph
2631

2732
# http://huge:file@www.pythonchallenge.com/pc/return/evil.html

level_12.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ def solve(something):
2121
im = Image.open(BytesIO(piece)).resize((640 // 5, 96))
2222
background.paste(im, (cur_width, 0))
2323
cur_width += im.size[0]
24-
background.show()
24+
return background
25+
26+
27+
def plot(im):
28+
im.show()
2529

2630

2731
if __name__ == "__main__":
2832
r = requests.get(url)
2933
something = r.content
3034
answer = solve(something)
35+
plot(answer)
3136
# disproportional
3237

3338
# http://huge:file@www.pythonchallenge.com/pc/return/disproportional.html

level_14.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ def solve(something):
3232
i += 1
3333
N += 4 * L
3434
L -= 2
35-
unknown.show()
35+
return unknown
36+
37+
38+
def plot(im):
39+
im.show()
3640

3741

3842
if __name__ == "__main__":
3943
r = requests.get(url)
4044
something = r.content
4145
answer = solve(something)
46+
plot(answer)
4247
# cat
4348

4449
# http://huge:file@www.pythonchallenge.com/pc/return/cat.html

level_16.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ def solve(something):
3333
index = line.index(separator)
3434
data += line[index:] + line[:index]
3535
unknown.putdata(data)
36-
unknown.show()
36+
return unknown
37+
38+
def plot(im):
39+
im.show()
3740

3841

3942
if __name__ == "__main__":
4043
r = requests.get(url)
4144
something = r.content
4245
answer = solve(something)
46+
plot(answer)
4347
# romance
4448

4549
# http://huge:file@www.pythonchallenge.com/pc/return/romance.html

0 commit comments

Comments
 (0)