the python challenge_The Python Challenge 谜题全解(持续更新)
Python Challenge(0-2)
是個很有意思的網(wǎng)站,可以磨練使用python的技巧,每一關(guān)都有挑戰(zhàn),要編寫相應(yīng)的代碼算出關(guān)鍵詞,才可以獲取下一關(guān)的url,還是很好玩的QAQ
LEVEL 0
顯然是計(jì)算圖片中的\(2^{38}\),結(jié)果為274877906944,所以url為http://www.pythonchallenge.com/pc/def/274877906944.html
print(2**38) #輸出274877906944
LEVEL 1
仔細(xì)觀察\(K→M,O→Q,E→G\)有什么規(guī)律,結(jié)論是后面的字母在字母表中都是前一個的索引加二,比如#\(K\)是第11個,\(M\)是第13個,所以我們也可以得出轉(zhuǎn)換字符串的方法:
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:#如果當(dāng)前處理的字符是字母
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. "
#翻譯過來就是:
我希望你不是徒手進(jìn)行這個字符轉(zhuǎn)換,這是計(jì)算機(jī)擅長的事情,徒手做是很低效的,所以我才把這個文本弄得這么長,將剛才你寫的處理字符串的函數(shù)用在url上試試
顯然,調(diào)用一下就把url中的map換成了ocr,得到了下一關(guān)的url:www.pythonchallenge.com/pc/def/ocr.html
LEVEL 2
顯然第三關(guān)要看清楚圖片上的文字是不可能的,下面提示的文字里面有page source,于是馬上想到查看網(wǎng)頁源代碼,于是看到了如下內(nèi)容:
然后就是一長串亂碼
任務(wù)很顯然,就是要找到下面很長一串代碼中單獨(dú)的元素,復(fù)制這么長的代碼到ide里也不方便,所以可以用爬蟲
import requests
from collections import Counter
text = requests.get("http://www.pythonchallenge.com/pc/def/ocr.html").text#獲取HTML文檔
final_text = text.split(""
q = Counter(final_text)#對里面的所有字符計(jì)數(shù)
t = [i for i in q if q[i]==1]#找出只出現(xiàn)一次的字符
print("".join(t))
#輸出了equality
根據(jù)輸出的內(nèi)容很顯然下一個網(wǎng)頁的url為http://www.pythonchallenge.com/pc/def/equality.html
LEVEL 3
有了上一道題的經(jīng)驗(yàn),看了下圖片和文字說明,題目的意思應(yīng)該是:查找類似\(AAAbCCC\)這種格式的字符串,我果斷查看了一下頁面源代碼,果然有一大堆文本在等著我去處理...
想起了正則表達(dá)式!,處理起來輕松多了
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
LEVEL 4
點(diǎn)擊圖片之后發(fā)現(xiàn)url變成了http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
提示說下一個nothing的值,抱著試試看的態(tài)度接連輸入了幾個,發(fā)現(xiàn)這個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)
#運(yùn)行了一會之后終于出了結(jié)果peak.html
下一關(guān)的url為http://www.pythonchallenge.com/pc/def/peak.html
LEVEL 5
略坑,peakhell念快點(diǎn)就是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]))
輸出結(jié)果為channel,所以下一關(guān)的url為http://www.pythonchallenge.com/pc/def/channel.html
LEVEL 6
習(xí)慣性地打開頁面源代碼,看到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
所以應(yīng)該是要從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))
it's in the air. look at the letters.
總結(jié)
以上是生活随笔為你收集整理的the python challenge_The Python Challenge 谜题全解(持续更新)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQLite Studio软件的安装及基
- 下一篇: 瑞芯微rk3368(px5)编译环境搭建