之前,忘了在哪儿看到这个网站Pythonchalleage,感觉对学习Python有点帮助,就来试了试,发现还是挺有意思的。
为了帮助他人(或者说是剧透),我就来写写我的通关笔记。

The First Step

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

看图上就是2的38次方,提示让你改变Url地址,你就把地址改为http://www.pythonchallenge.com/pc/def/274877906944.html 就行了

The Second Step

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

根据图上的规律,把每个单词后移了两位,根据这个,写个程序把下面那串乱序的单词,翻译过来:

1
2
3
4
5
6
7
8
9
10
11
def add(s):
sl = ''
for w in s:
if w == ' ' or w == '.' or w == '(' or w == ')' or w == "'":
sl += w
elif w >= 'y':
sl += chr(ord(w)-24)
else:
sl += chr(ord(w)+2)
return sl
print add("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 rcvrgqqmjmle.sqgleqrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.")

(写这个时我还没看正则,不够简洁)得到结果,意思就是说让你用string.maketrans()把url翻译了……
其实你可以看看现在的url地址,map后移两位换成ocr,通关!

The Third Step

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

根据页面提示说,字符识别,可能在网页源代码中,看看呗,那。。。。。。。。么多
既然让找字符就找呗
我把那堆注释放到一个文件里,然后

1
2
3
4
5
6
7
def find():
for line in open('hehe.txt'):
line = line.rstrip('\n')
for w in line:
if w >= 'a' and w<='z':
print w,
find()

结果是equality,下一关……

The Fourth Step

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

这关和上一关差不多,只是规则不一样,是让你找被前后都有三个大写字母包围的小写字母

1
2
3
4
5
6
7
8
9
import re
def find():
text = open('equality.txt','r')
l = ''
for line in text:
if re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]',line)!= []:
l += re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]',line)[0]
return l
print find()

结果是linkedlist
会提示换成.php

The Fifth Step

http://www.pythonchallenge.com/pc/def/linkedlist.php

点了图片就知道要换nothing值,可是手动明显不现实,不信可以试试
现学了下python网络编程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/python
#encoing = utf-8

import urllib
import re

r = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=这个值自己换")
while True:
s = r.fp.read()
l = re.findall(r"[0-9]+",s)
if l == []:
break
r = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="+l[0])
print l[0]

中间间断一次,显示Yes. Divide by two and keep going.
修改程序继续,程序会被误导到82683,回到上一个是82682
显示There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579
修改程序继续,最终得到peak.html

The Sixth Step

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

这一关算是奇葩了,给你一个山丘的图片,让你pronounce it ,看源代码还说peak hell sounds familiar ? 实在搞不懂,只有求助了,看了攻略才明白要用啥pickle,实在没办法看不懂,直接抄袭了http://www.cnblogs.com/dukeleo/p/3460223.html

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# encoding: utf-8
import urllib
import pickle

url = 'http://www.pythonchallenge.com/pc/def/banner.p'
rst = urllib.urlopen(url).read()
pList = pickle.loads(rst)
for item in pList:
print ''.join([i[0] * i[1] for i in item])

就这样,结果就是这样,想不到啊,想不到

Channel

The Seventh Step

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

这关的图片是拉链(zip),肯定和zip有关了,下载channel.zip,解压之后,出现很多txt文件,根据里面的内容,看来和第五关差不多了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import re        
import zipfile

z = zipfile.ZipFile("../channel.zip")
name = input("filename:")
while True:
with open('{0}.txt'.format(name),'r') as f:
line = f.readlines()

if len(line) > 1:
print('Different file {0}'.format(name))
else:
try:
# print(z.getinfo("{0}.txt".format(name)).comment.decode(),end=' ')
p = re.compile('[\d]+').findall(line[0])
assert len(p) == 1
name = p[0]
except:
print("It's the last one,{0}".format(name))
print(line)
break

上了第五关的当,我加了检查误导的代码,结果发现没有误导的指向
得到末尾文件是46145.txt
内容是Collect the comments.
到这里我不懂了,作了下弊,得知zipfile模块有comment的属性
用上面注释的语句输出,得出下图
hockey
http://www.pythonchallenge.com/pc/def/hockey.html
它说it’s in the air. look at the letters.
是氧气oxygen。。。。。呵呵哒

The Eight Step

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