Py学习  »  Python

不服来战, Python趣味挑战赛

Python之禅 • 5 年前 • 322 次点击  


有个很牛的网站 www.pythonchallenge.com, 它是一个在线的 Python 技术挑战平台,一共有33道关卡,你只有正确回答了第一道题才能看到第二道题,依此类推

第一次使用这个网站估计你弄不明白怎么玩,因为每道题就是一张图,你要根据每道题的提示信息算出正确答案,正确答案就是下一道题的入口

第一题


这道题只能算是一道热身题,下面有一行提示,”Hint: try to change the URL address.”

它的答案其实就是将2的38次方的结果作为下一道题的入口链接,因为 2**38 等于 274877906944,所以第二道题的链接地址就是将这道题的链接

http://www.pythonchallenge.com/pc/def/0.html
替换成:
http://www.pythonchallenge.com/pc/def/274877906944.html

第二题

打开链接后会重定向到新的页面,出现的图是:


图中提供的信息应该是类似于凯撒加密,K->M,O->Q,E->G,也就是意味着每个字母都往右移了2位,可以猜到 a->c,c->e,… x->z, y->a,z->b,另外还有一串加密的提示信息,为了得到正确,答案,我们必须将这串字符串解密出来

先用代码实现可以得到解密算法:

def decrypt(c):
return chr(((ord(c) + 2) - ord('a')) % 26 + ord('a'))

将加密字符串进行解密得到:

>>> s = """

everybody thinks twice before solving this.

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.

"""

>>> result = ""
>>> for c in raw:
...    if c >= 'a' and c <= 'z':
...        result += decrypt(c)
...    else:
...        result += c

最后解密出result为(代码可右划):

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.

它告诉我们不要等手动的去转换,而是用一种算法来实现,因为遇到很长的字符串时手动转换的效率不高,另外它还给了我们另一种方法就是使用 string.maketrans() 进行转换,所以就有了第二种解法:

>>> t = str.maketrans("abcdefghijklmnopqrstuvwxyz", "cdefghijklmnopqrstuvwxyzab")
>>> s.translate(t)
"i hope you didnt translate it by hand. thats what computers are for. doing itin by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url."

我们将该算法应用到该url

http://www.pythonchallenge.com/pc/def/map.html

将被转换成

http://www.pythonchallenge.com/pc/def/ocr.html

该链接就是第三题的入口

第三题


这道题留给大家思考,第一个答对的同学将获得一本和Python相关的书籍。

推荐阅读:

Python小白必备的8个最常用的内置函数

如果喜欢本文,欢迎关注并置顶公众号
方便第一时间收看最新内容


今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/hmFqilU7Lf
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/28987
 
322 次点击