有趣的Python Challenge编程游戏闯关攻略一(0-4关)

**有趣的Python Challenge编程游戏闯关攻略一(0-4关)**

介绍

平时自己学python,大家肯定很是无聊,推荐一个很早之前的网页版python闯关游戏——Python Challenge,虽然说这个网站很早了,但是很有意思,你会发现这些游戏一点也不简单,基本都需要通过编程来解决……

有意思的是,这是个解谜游戏,所以需要你细心去发现线索,破解谜底,并且考察的知识量也不是很小,很有意思。

游戏链接:http://www.pythonchallenge.com/

界面是这样的:
在这里插入图片描述
点击Click here to get challenged开始挑战。

关卡

第0关

链接:http://www.pythonchallenge.com/pc/def/0.html
在这里插入图片描述
可以看见左上角显示第0关,下面提示 try to change the URL address,让我们改变url地址,这是很重要的,因为后面的关卡都将通过这样的方式进入下一关。屏幕中间显示2^38,猜测需要用计算结果来改变url,于是编程:

2**38

很简单就能得到结果是274877906944,替换掉原来url中的0得到新的url:

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

第1关

在这里插入图片描述
可以看见下面有一行看不懂的字母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.,上面图片有字母提示。

当前的url为http://www.pythonchallenge.com/pc/def/map.html 其实这时候我们可以大胆猜测是把map替换掉,替换的规则就是把每个字母向后推两位。

我们编程来翻译下面的文字:

s='''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.'''
for i in range(len(s)):
    if ord(s[i])>=ord('a') and ord(s[i])<=ord('x'):
        c=chr(ord(s[i])+2)
        print(c,end='')
    elif ord(s[i])>=ord('y') and ord(s[i])<=ord('z'):
        c=chr(ord(s[i])-24)
        print(c,end='')
    else:
        print(s[i],end='')

输出结果为:

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()来进行计算,但事实上我们几乎已经可以肯定谜底了,显然就是把map替换为ocr即可,于是结果为:

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

第2关

在这里插入图片描述
可以看见提示我们查看网页源代码,右键查看源代码(后面的关卡经常用到),我们发现有一段注释<!-- find rare characters in the mess below:(在下面杂乱的字符中找出稀有字符) -->
下面有一段非常长的字符,目测大概有一两万,大概像这样:
在这里插入图片描述
什么是稀有字符呢?估计就是出现次数少的吧,我们编程处理:

from collections import Counter
strings='''长长的字符'''
c = Counter(strings)
print(c.most_common())

结果是这样,可以看见有一些字母出现了一次,

[(')', 6186), ('@', 6157), ('(', 6154), (']', 6152), ('#', 6115), ('_', 6112), ('[', 6108), ('}', 6105), ('%', 6104), ('!', 6079), ('+', 6066), ('$', 6046), ('{', 6046), ('&', 6043), ('*', 6034), ('^', 6030), ('\n', 1219), ('e', 1), ('q', 1), ('u', 1), ('a', 1), ('l', 1), ('i', 1), ('t', 1), ('y', 1)]

打印出来

from collections import Counter
strings='''长长的字符'''
c = Counter(strings)
print(c.most_common())
print(''.join([i[0] for i in c.items() if i[1]==1]))

结果是:equality
于是得到新的url:

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

第3关

在这里插入图片描述
可以看见下面的提示One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.
就是说找小写字母,这个小写字母的两边必须是3个大写字母,但是没有说去哪里找??

根据经验,我们右键查看网页源代码:
在这里插入图片描述
同样我们可以看见有一段注释,长度吓人。。。。。
此时我们注意到网页titlere,所以我们使用正则表达式来查找:
(解释一下,因为两边的大写字母只能是三个,所以我们在两边添加[a-z]

import re
strings='''长长的字符串'''
reg=re.compile('[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]')
print(' '.join(reg.findall(dat)))

结果是这样:

qIQNlQSLi eOEKiVEYj aZADnMCZq bZUTkLYNg uCNDeHSBj kOIXdKBFh dXJVlGZVm gZAGiLQZx vCJAsACFl qKWGtIDCj

中间的小写字母就是我们要找的,修改代码:

result=reg.findall(dat) 
url='' 
for res in result: 
    url+=res[4] 
print(url)

打印的结果是:linkedlist,得到新的url:
http://www.pythonchallenge.com/pc/def/linkedlist.html

打开以后,页面只有一句 linkedlist.php,我们再次替换url:

得到:http://www.pythonchallenge.com/pc/def/linkedlist.php 这才是最终结果。

第4关

在这里插入图片描述
只有一张图片。。。于是我点击图片看到一句话and the next nothing is 44827
???????????what???这是啥意思?????
别急,还是查看源代码


<html>
<head>
  <title>follow the chain</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>

我们可以看见,下面这两行文字,它给我们提供了一个库urllib,然后告诉我们不要尝试所有的nothing,因为它是无限的,400次循环足够了……

<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->

这时,我么们也许会想,nothing是啥????别忘了我们点击图片以后的url是http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

以及出现的文字是and the next nothing is 44827
这就很明了了,爬虫的用处出来了,提示我们使用urllib库,但是我用requests照样解决问题
(其实为了严谨起见,使用正则表达式查找nothing的值更为精确吧,不过数据不多,我们这样也很简单找出来)

import requests
url='http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345'
for i in range(400):
    url='http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+txt.split()[-1]
    r=requests.get(url)
    txt=r.text
    print(txt)

虽然每一次都打印出来结果很浪费,但是不要省略,因为也许有些页面不一样呢??并且打印出来的时候,我们就可以停止程序的运行,因为这个循环很费时间。我开始就是当循环第400次的时候打印结果,但是没有收获,我尝试了400前后的几次都不行,于是我打印了出来,大约是250左右的某一次打印了出来,结果是这样。

and the next nothing is 41643
and the next nothing is 23416
and the next nothing is 54432
and the next nothing is 4448
……………………这里省略很多行
peak.html
and the next nothing is 72758
and the next nothing is 71301

这样我们就得到了新的url

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

好了,这就是前五个关卡,,第五关很奇葩,明天再写吧…………

链接总结

游戏链接:     http://www.pythonchallenge.com/0关开始链接:http://www.pythonchallenge.com/pc/def/0.html
谜底:      0:http://www.pythonchallenge.com/pc/def/274877906944.html
            1:http://www.pythonchallenge.com/pc/def/ocr.html
            2:http://www.pythonchallenge.com/pc/def/equality.html
            3:http://www.pythonchallenge.com/pc/def/linkedlist.php
            4:http://www.pythonchallenge.com/pc/def/peak.html
发布了4 篇原创文章 · 获赞 8 · 访问量 461

猜你喜欢

转载自blog.csdn.net/weixin_46283214/article/details/105345260