The Python Challenge 谜题全解(持续更新)

2023-07-29,,

Python Challenge(0-2)

The Python Challengehttp://www.pythonchallenge.com/

是个很有意思的网站,可以磨练使用python的技巧,每一关都有挑战,要编写相应的代码算出关键词,才可以获取下一关的url,还是很好玩的QAQ


LEVEL 0

显然是计算图片中的\(2^{38}\),结果为274877906944,所以url为http://www.pythonchallenge.com/pc/def/274877906944.html

print(2**38)  #输出274877906944

LEVEL 1

仔细观察\(K→M,O→Q,E→G\)有什么规律,结论是后面的字母在字母表中都是前一个的索引加二,比如#\(K\)是第11个,\(M\)是第13个,所以我们也可以得出转换字符串的方法:

import string
a = "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. " l = string.ascii_lowercase + "ab" #每次都是+2,所以后面多加了2个字母,这是小写字母表 def tran(s):
tmp =""
for i in s:
if i in l: #如果当前处理的字符是字母
id = l.index(i)
tmp += l[id+2]
else:
tmp += i
return tmp
print(tran(a)) #输出了
"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. " #翻译过来就是:
我希望你不是徒手进行这个字符转换,这是计算机擅长的事情,徒手做是很低效的,所以我才把这个文本弄得这么长,将刚才你写的处理字符串的函数用在url上试试

显然,调用一下就把url中的map换成了ocr,得到了下一关的url:www.pythonchallenge.com/pc/def/ocr.html


LEVEL 2

显然第三关要看清楚图片上的文字是不可能的,下面提示的文字里面有page source,于是马上想到查看网页源代码,于是看到了如下内容:

<!--find rare characters in the mess below:-->
然后就是一长串乱码

任务很显然,就是要找到下面很长一串代码中单独的元素,复制这么长的代码到ide里也不方便,所以可以用爬虫

import requests
from collections import Counter text = requests.get("http://www.pythonchallenge.com/pc/def/ocr.html").text #获取HTML文档
final_text = text.split("<!--")[2][:-3] #用了比较蠢的方法,用“<!--"当做分隔符,[:-3]是为了清除最后的"-->" q = Counter(final_text) #对里面的所有字符计数
t = [i for i in q if q[i]==1] #找出只出现一次的字符
print("".join(t))
#输出了equality

根据输出的内容很显然下一个网页的url为http://www.pythonchallenge.com/pc/def/equality.html


LEVEL 3

有了上一道题的经验,看了下图片和文字说明,题目的意思应该是:查找类似\(AAAbCCC\)这种格式的字符串,我果断查看了一下页面源代码,果然有一大堆文本在等着我去处理...

想起了正则表达式!,处理起来轻松多了

import requests
import re text = requests.get("http://www.pythonchallenge.com/pc/def/equality.html").text
final_text = re.findall("<!--(.*?)-->", text, re.DOTALL)[-1] #re.DOTALL表示忽略换行符 ans = "".join(re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+",final_text))
print(ans)
#输出了linkedlist

由此,我们得到了下一关的url:http://www.pythonchallenge.com/pc/def/linkedlist.php


LEVEL 4

点击图片之后发现url变成了http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

提示说下一个nothing的值,抱着试试看的态度接连输入了几个,发现这个linkedlist很长很长,所以写了如下代码

import requests
import re preurl = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
id = 12345
while True:
url = preurl + str(id)
text = requests.get(url).text
id = text.split(" ")[-1]
print(text)
#运行了一会之后终于出了结果peak.html

下一关的url为http://www.pythonchallenge.com/pc/def/peak.html


LEVEL 5

略坑,peakhell念快点就是pickle了,pickle是python的一个库,所以编写代码如下:

import pickle
from urllib.request import urlopen
import requests
text = pickle.load(urlopen("http://www.pythonchallenge.com/pc/def/banner.p")) for line in text:
print("".join([k*v for k,v in line]))

输出结果为channel,所以下一关的url为http://www.pythonchallenge.com/pc/def/channel.html


LEVEL 6

习惯性地打开页面源代码,看到zip,下载http://www.pythonchallenge.com/pc/def/channel.zip后打开readme.txt文档看到

welcome to my zipped list.

hint1: start from 90052
hint2: answer is inside the zip

所以应该是要从90052.txt开始像之前的linkedlist一个个找下去,最后的输出是

Collect the comments.

翻zipfile的文档看到zipfile.comments,写了下代码

import zipfile, re

file = zipfile.ZipFile("channel.zip")

num = "90052"
comments = []
while True:
content = file.read(num + '.txt').decode("utf-8")
comments.append(file.getinfo(num + '.txt').comment.decode("utf-8"))
print(content)
match = re.search("Next nothing is (\d+)",content)
if match == None:
break
num = match.group(1) print("".join(comments))

输出了hockey,http://www.pythonchallenge.com/pc/def/hockey.html,以为成功了,打开一看是:

it's in the air. look at the letters.

谜底是oxygen,所以正确的url应该是:http://www.pythonchallenge.com/pc/def/oxygen.html

The Python Challenge 谜题全解(持续更新)的相关教程结束。

《The Python Challenge 谜题全解(持续更新).doc》

下载本文的Word格式文档,以方便收藏与打印。