python challenge 0,1,2解题以及总结

level 0:

2**38=274877906944 change the url to http://www.pythonchallenge.com/pc/def/274877906944.html

level 1:

     代码如下:

import string
 
def change(inputstr,fromtable,totable):
       newtable=string.maketrans(fromtable,totable)
       hintstring=inputstr.translate(newtable)
       return hintstring
 
if __name__=='__main__':
     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"
        table="'().abcdefghijklmnopqrstuvwxyz"
        newtable="'().cdefghijklmnopqrstuvwxyzab"
        result=change(a,table,newtable)
        url=change('map',table,newtable)
        print result
        print url



这个主要是考察string.maketrans和str.translate的作用,translate除了本代码里面的作用外还可以起到过滤的作用,如 >>> 'this0not1a2stroy!3333'.translate(None,'0123') 'thisnotastroy!'

level 2:

这个主要是考察正则表达式和re。 首先获取网页的源码,会得到如下信息: source 简单的代码如下:

import re
pattern='[A-Za-z0-9]+'
result=''
with open('D:/level2.txt','r') as f:
        for eachline in f:
              m=re.search(pattern,eachline)
              if m is not None:
                  result+=m.group()
print result


阅读全文 »

python challenge 5解题总结

python challenge 5 网址:www.pythonchallenge.com/pc/def/peak.html

 

网页上只有两个英语单词pronounce it,再看图片上的画面,上面显示的是山顶,山顶的英文单词为peak,这与python的模块pickle音相同,于是想到这是一个序列化问题,看网页的源码会得到banner.p这么一个信息,于是利用urllib2打开,用pickle进行反序列化。


源代码如下:

import urllib2, pickle
url="http://www.pythonchallenge.com/pc/def/banner.p"
result=''
tmp=urllib2.urlopen(url)
fileobj=pickle.load(tmp)
for i in fileobj:
    for j in i:
        result=result+j[0]*j[1]
    result=result+'n'
print result


阅读全文 »

python challenge 3解题总结

python challenge 3网址:www.pythonchallenge.com/pc/def/equality.html
 

图片下面显示一句话:One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.


从提示的话语里面可以看出需要寻找一个小写字母左右各有三个大写字母的形式,例如:1ASDaFGH9.


接着查看网页源码,能够发现一大堆类似“kAewtloYgcFQaJNhHVGxXDiQmzjfcpYbzxlWrVcqsmUbCunkfxZWDZjUZMiGqhRRiUvGmYmvnJIHEmbT”这样的字符串,联想到提示,就知道需要寻找一些小写字符,小写字符的形式是左右各三个大写字母。


源代码如下:

import urllib
import re
urldata=urllib.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
geturldata=urldata.read()
getdata=re.findall(r'',geturldata,re.S)
charlist =''.join(re.findall(r'[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', getdata[0], re.S)) 
print charlist

输出linkedlist
也可以把字符串拷贝进一个本地文档,直接读取文档,用正则表达式匹配出符合要求的字符。


 

阅读全文 »

python challenge 4解题总结


 python challenge 4网址:www.pythonchallenge.com/pc/def/linkedlist.php

打开这一关的页面只显示一个图片,按照惯例,先查看页面源码,会得出如下信息:
urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough
并且会得到一个网址linkedlist.php?nothing=12345,打开http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345后会得到如下信息:
and the next nothing is 44827
接着把nothing=12345换成nothing=44827,会得出如下信息:
and the next nothing is 45439
看到这里,并且联想提示:urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough,直接读取网页信息,利用正则表达式取出网页信息中的数字,填入nothing=*****中,直到得出结果。
(这里需要注意的是中间有部分网页会出现Yes. Divide by two and keep going.这样的信息,所以需要程序里面进行判断。)


源代码如下:

import re   
import urllib   
url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="  
nothing = "12345"  
search = re.compile(" (d*)$")   
search_html = re.compile(".html$")   
  
for i in xrange(300):    
    print "%s: " % nothing,    
    line = urllib.urlopen( "%s%s" % (url,nothing) ).read()   
    print line     
    if search_html.findall (line):   
        break             
    match = search.findall (line)   
    if match:   
        nothing = match [0]   
    else:   
        nothing = str (int (nothing) / 2 )  


得出的结果是:peak.html,改变url进入下一关。
 

阅读全文 »

python challenge 6解题总结

python challenge 6的网址:www.pythonchallenge.com/pc/def/channel.html

这一关只有这一张图片,并且从源码里面也没有得出什么具体的信息,于是突然想到zip可以表示图片里面的拉链的意思,于是打开www.pythonchallenge.com/pc/def/channel.zip, 原来玄机就在这里。对付ZIP,python模块里面有zipfile

解题代码如下:

import zipfile, re
start = '90052'
r = 'Next nothing is (d+)'
file = zipfile.ZipFile('D:channel.zip')
f = "%s.txt"
c = []
while True:
      try:
          start = re.search(r, file.read(f % start)).group(1)
     except:
         print file.read(f % start)
         break
     c.append(file.getinfo(f % start).comment)
print "".join(c)


阅读全文 »

python challenge 7解题总结

 

python challenge 7网址:www.pythonchallenge.com/pc/def/oxygen.html 

从图片一看就发现图片中间有灰色的东西,这时候就想到是不是需要用python的图片模块来出来这段灰色的地方,这里可能是解题的关键,python处理图片比较好的模块是PIL。将全幅图像转换为灰度值后,对中间的那条带子隔7取1。

代码如下:

import PIL.Image
temp=PIL.Image.open('D:oxygen.png')           #把图片拷贝到本地
temp2=temp.convert('L').getdata()
beginpoint=temp.size[0]*50
endpoint=beginpoint+608
ra=range(beginpoint,endpoint,7)
temp3=[]
for v in ra:
        temp3.append(chr(temp2[v]))

print ''.join(temp3)
test=[105, 110, 116, 101, 103, 114, 105, 116, 121]
answer=''
for line in test:
    answer=answer+chr(line)
print answer


阅读全文 »

python challenge 8解题总结

 

python challenge 8网址:www.pythonchallenge.com/pc/def/integrity.html

 

 

这个图片以及后面的源码初看起来没有什么思路,后来突然发现如下的文字:

un: 'BZh91AY&SYAxafx82rx00x00x01x01x80x02xc0x02x00 x00!x9ah3Mx07<]xc9x14xe1BAx06xbex084'
pw: 'BZh91AY&SYx94$|x0ex00x00x00x81x00x03$ x00!x9ah3Mx13<]xc9x14xe1BBPx91xf08'

在google里面搜索一下BZh91AY,就发现原来需要用python的bz2模块来处理。

源代码如下:

import bz2

un= 'BZh91AY&SYAxafx82rx00x00x01x01x80x02xc0x02x00 x00!x9ah3Mx07<]xc9x14xe1BAx06xbex084'
pw= 'BZh91AY&SYx94$|x0ex00x00x00x81x00x03$ x00!x9ah3Mx13<]xc9x14xe1BBPx91xf08'

username=bz2.decompress(un)

password=bz2.decompress(pw)

阅读全文 »

python challenge 9解题总结

 

python challenge 9 网址:www.pythonchallenge.com/pc/return/good.html

从网页的源代码可以得出两个数字列表:

first=[146,399,163,403,170,393,169,391,166,386,170,381,170,371,170,355,169,346,167,335,170,329,170,320,170,
310,171,301,173,290,178,289,182,287,188,286,190,286,192,291,194,296,195,305,194,307,191,312,190,316,
190,321,192,331,193,338,196,341,197,346,199,352,198,360,197,366,197,373,196,380,197,383,196,387,192,
389,191,392,190,396,189,400,194,401,201,402,208,403,213,402,216,401,219,397,219,393,216,390,215,385,
215,379,213,373,213,365,212,360,210,353,210,347,212,338,213,329,214,319,215,311,215,306,216,296,218,
290,221,283,225,282,233,284,238,287,243,290,250,291,255,294,261,293,265,291,271,291,273,289,278,287,
279,285,281,280,284,278,284,276,287,277,289,283,291,286,294,291,296,295,299,300,301,304,304,320,305,
327,306,332,307,341,306,349,303,354,301,364,301,371,297,375,292,384,291,386,302,393,324,391,333,387,
328,375,329,367,329,353,330,341,331,328,336,319,338,310,341,304,341,285,341,278,343,269,344,262,346,
259,346,251,349,259,349,264,349,273,349,280,349,288,349,295,349,298,354,293,356,286,354,279,352,268,
352,257,351,249,350,234,351,211,352,197,354,185,353,171,351,154,348,147,342,137,339,132,330,122,327,
120,314,116,304,117,293,118,284,118,281,122,275,128,265,129,257,131,244,133,239,134,228,136,221,137,
214,138,209,135,201,132,192,130,184,131,175,129,170,131,159,134,157,134,160,130,170,125,176,114,176,
102,173,103,172,108,171,111,163,115,156,116,149,117,142,116,136,115,129,115,124,115,120,115,115,117,
113,120,109,122,102,122,100,121,95,121,89,115,87,110,82,109,84,118,89,123,93,129,100,130,108,132,110,
133,110,136,107,138,105,140,95,138,86,141,79,149,77,155,81,162,90,165,97,167,99,171,109,171,107,161,
111,156,113,170,115,185,118,208,117,223,121,239,128,251,133,259,136,266,139,276,143,290,148,310,151,
332,155,348,156,353,153,366,149,379,147,394,146,399]

second=[156,141,165,135,169,131,176,130,187,134,191,140,191,146,186,150,179,155,175,157,168,157,163,157,159,
157,158,164,159,175,159,181,157,191,154,197,153,205,153,210,152,212,147,215,146,218,143,220,132,220,
125,217,119,209,116,196,115,185,114,172,114,167,112,161,109,165,107,170,99,171,97,167,89,164,81,162,
77,155,81,148,87,140,96,138,105,141,110,136,111,126,113,129,118,117,128,114,137,115,146,114,155,115,
158,121,157,128,156,134,157,136,156,136]

于是想到利用python的图片处理模块PIL来对这两个数字字符串位置进行填充,于是就有源码如下:

from PIL import Image
from PIL import ImageDraw
temp=Image.open('good.jpg')
tempdraw=ImageDraw.Draw(temp)
tempdraw.line(first,fill=255)
tempdraw.line(second,fill=0)
temp.save('good.jpg')


如果需要用户名和密码的话,用户名为huge,密码为file。

阅读全文 »

python challenge 10解题总结

 python challenge 10网址:www.pythonchallenge.com/pc/return/bull.html 

点击图片后出现www.pythonchallenge.com/pc/return/sequence.txt,信息如下:

a = [1, 11, 21, 1211, 111221,

原来就是要找规律的题目,第一个数字是1,第二个数字是11,第三个是21,第四个是。。。,仔细看看上述的数字,会发现原来规律就是:

第一个是1;

第二个是前面一个数字是一个1,就是11;

第三个是前面的数字有两个1,就是21;

第四个就是前面的数字有一个2,一个1,就是1211;

第五个就是前面的数字为一个1,一个2,两个1,就是111221;

。。。。。。。。。。。。。。。。。

得出结果是5808,python challenge 11的网址就是:www.pythonchallenge.com/pc/return/5808.html

如果需要用户名和密码的话,用户名为huge,密码为file。


源代码:

test=['1']
a='1'
i=0
j=0
s=''
while(i<=31):
    while(j<len(a)):
        temp=a[j]
        count=1
        for k in range(j+1,len(a)):
            if a[k]==temp:
                count+=1
                continue
            else:
                break
        s=s+str(count)+a[j]
        j=j+count
    test.append(s)
    a=s
    s=''
    i+=1
    j=0
print len(test[30])    

阅读全文 »

python challenge 11解题总结

python challenge 11网址:www.pythonchallenge.com/pc/return/5808.html

首先页面的图片很模糊,而且页面源码的标题为odd,even,那么需要对图片进行奇偶坐标的一些处理:

直接把奇列和偶列分开来,显示为两张图片就行了,代码如下:

from PIL import Image
if __name__ == '__main__':   
       
    img = Image.open('cave.jpg')   
       
    w = img.size[0]   
    h = img.size[1]   
       
    odd = even = Image.new(img.mode, (w/2, h/2))   
       
    for x in range(w):   
        for y in range(h):   
            pixel = img.getpixel((x, y))   
            if x % 2 == 0 and y % 2 == 0:   
                odd.putpixel((x/2, y/2), pixel)   
            elif x % 2 == 1 and y % 2 == 0:   
                even.putpixel(((x-1)/2, y/2), pixel)   
            elif x % 2 == 0 and y % 2 == 1:   
                even.putpixel((x/2, (y-1)/2), pixel)   
            elif x % 2 == 1 and y % 2 == 1:   
                odd.putpixel(((x-1)/2, (y-1)/2), pixel)   
                   
    odd.show()   
    even.show() 


结果会显示evil,下一关的网址是:www.pythonchallenge.com/pc/return/evil.html

 

 

如果需要用户名和密码的话,用户名为huge,密码为file。

阅读全文 »