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