Python Challenge is a fun website and a good material for beginners to practice. I have bookmarked this website for a long time, but I have never tried it. Recently, on a whim, this article records the clearance strategies and codes for levels 0 to 5.
Level 0: warming up
The original url is: http://www.pythonchallenge.com/pc/def/0.html
0.1 Raiders
The interface is 2 to the 38th power, use Python to calculate the result, replace the url suffix, and modify the url to http://www.pythonchallenge.com/pc/def/274877906944.html
0.2 code
print(2 ** 38)
"""
Output: 274877906944
"""
Level 1: What about making trans?
url:http://www.pythonchallenge.com/pc/def/274877906944.html
1.1 Raiders
On the interface, the letter K is shifted 2 digits back to become the letter M, the letter O is shifted 2 digits backward to become the letter Q, and the letter E is shifted 2 digits backward to become the letter G, so examine the shift password and move the url suffix backward 2 digits and replace, modify the url to http://www.pythonchallenge.com/pc/def/ocr.html
1.2 Code
def decrypt(s):
res = ""
for c in s:
if c not in [" ", ".", "(", ")", "'"]:
res += chr(ord('a') + (ord(c) + 2 - ord('a')) % 26)
else:
res += c
return res
# 解密提示
print(decrypt("g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."))
# Output:
# i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.
# 解密答案
print(decrypt("map"))
"""
Output: ocr
"""
Level 2: ocr
url:http://www.pythonchallenge.com/pc/def/ocr.html
2.1 Raiders
View the source code according to the interface prompts, find puzzles in the source code comments, and find out the characters with the least number of occurrences: a, l, q, i, y, u, t, e, and the word that forms is equality, so modify the url as http://www.pythonchallenge.com/pc/def/equality.html
2.2 Code
from collections import Counter
def getRareChar(s):
cnt = Counter(s)
sorted_cnt = sorted(cnt.items(), key=lambda x:x[1])
return sorted_cnt
# 字符串过长,用文件存储
with open('2-input.txt') as f:
s = f.read()
print(getRareChar(s))
"""
Output:
[('a', 1),
('l', 1),
('q', 1),
('i', 1),
('y', 1),
('u', 1),
('t', 1),
('e', 1),
('\n', 1219),
('^', 6030),
('*', 6034),
('&', 6043),
('$', 6046),
('{', 6046),
('+', 6066),
('!', 6079),
('%', 6104),
('}', 6105),
('[', 6108),
('_', 6112),
('#', 6115),
(']', 6152),
('(', 6154),
('@', 6157),
(')', 6186)]
"""
Level 3: re
url:http://www.pythonchallenge.com/pc/def/equality.html
3.1 Raiders
Same as the previous level, the string data is hidden in the source code, use regular expressions to match the pattern of "1 lowercase letter with 3 uppercase letters on each side", and finally
get linkedlist, so modify the url to http://www. pythonchallenge.com/pc/def/linkedlist.html , further prompts to jump to http://www.pythonchallenge.com/pc/def/linkedlist.php
3.2 code
import re
with open('3-input.txt') as f:
s = f.read()
iteritems = re.finditer(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', s, re.M)
res = []
for m in iteritems:
res.append(m.group(1))
print(''.join(res))
"""
Output: linkedlist
"""
Level 4: follow the chain
url:http://www.pythonchallenge.com/pc/def/linkedlist.php
4.1 Raiders
- There is no obvious prompt on the interface, so continue the previous few levels of thinking, view the source code, and find a hidden prompt "urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough."
- Found that the image link address is http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
- Click the picture to jump to http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345 , the interface displays "and the next nothing is 44827", so it is necessary to constantly modify the "nothing=" suffix through the interface content , using urlib to simulate a webpage request loop 400 times (loop, that is, the meaning of the title follow the chain), get "peak.html" in the loop, and the final answer is http://www.pythonchallenge.com/pc/def/peak .html
4.2 Code
from urllib.request import urlopen
response = urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345")
for i in range(400):
suffix = response.read().decode('utf-8').split()[-1]
if suffix.find('.html') != -1:
print(suffix)
break
url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" + suffix
response = urlopen(url)
"""
Output: peak.html
"""
Level 5: peak hell
url:http://www.pythonchallenge.com/pc/def/peak.html
5.1 Strategy
-
The interface prompts "pronounce it", check the source code and find the prompt "peak hell sounds familiar?"
-
Continue to read the source code, found a strange source file "banner.p", click to open in a new tab to find hidden data
-
At this time, combined with the above findings, the pronunciation of "peak hell" is similar to "pickle". Use pickle to deserialize banner.p to get "channel", so finally modify the url to http://www.pythonchallenge.com/pc/def/ channel.htmlEnter level 6.
5.2 Code
import pickle
with open('banner.p', 'rb') as f:
s = f.read()
data = pickle.loads(s)
for item in data:
for ch, cnt in item:
print(cnt * ch, end="")
print()
output: