本篇内容主要讲解“Python趣味挑战题目有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python趣味挑战题目有哪些”吧!
第一题
这道题只能算是一道热身题,下面有一行提示,”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趣味挑战题目有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/219637.html