python challenge 0-10

0.求2^38

计算之后用结果替代url中的0

#solution 1
pow(2,38)

#solution 2
2**38


1.字母移位密码

涉及函数:

ord('a')

chr(97)

trans=string.maketrans('abc','def')

str.translate(trans)

获取24个小写/大写字母:string.ascii_lowercase,string.ascii_uppercase

获取字母:string.ascii_letters

#solution 1
str="""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 letter in str:   
    if ord(letter)>=ord('a') and ord(letter)<=ord('z'):
        code=ord(letter)+2
        if code==ord('z')+2: code=ord('b')
        if code==ord('y')+2: code=ord('a')
        letter=chr(code)
    result=result+letter
print result

#solution 2
import string

trans = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[0:2]) 
text = """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. """
print text.translate(trans)
print "map".translate(trans)


2.查看网页源码,寻找一段乱码中的字母

import string
text="""  """ 

text_list = list(text)

text_real=[]
for c in text_list:
   if c in string.ascii_letters:
      text_real.append(c)

print ''.join(text_real)

3.正则式

关键是正则式,读取网页源码
之前一直直接用str= ' http://www.pythonchallenge.com/pc/def/equality.html ',无法得出正确结果

import re,urllib 
f=urllib.urlopen('http://www.pythonchallenge.com/pc/def/equality.html') 
str= f.read() 

ans=re.findall('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', str) 
print ' '.join(ans) 
结果为:linkedlist

4.循环读取网页内容
import urllib2 

def getNextId(uid): 
url="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="+uid 
page=urllib2.urlopen(url).read() 
print page 
nextid=page.split(" ")[-1] 
return nextid 

uid="12345" 
while uid.isdigit(): 
uid=getNextId(uid)
结果为:peak.html

5.pickle
pickle用于实现基本的数据序列化和反序列化

 图片下有这么一句话“ pronounce it ”,再看源代码:

    “peak hell sounds familiar ”?pickle模块。按照给定的文件banner.p,使用pickle解序列,将得到一个包含了不定数个元组的列表的列表。经过分析,每个列表的所有元组的第二项相加均为95,而元组的第一项要么是‘ ’,要么是‘#’;那么,很显然按一行95个字符的排列输出将得到有效信息,代码如下:

import pickle  
import urllib  
     
data = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p").read()  
list = pickle.loads(data)  
     
string = ''  
for metalist in list:  
        for meta in metalist:  
                string += meta[0]*meta[1]  
        string += '/n'  
print string  

运行程序,将得到channel字样。下一关网址将是channel.html ,通关!

6.取出zip文件中的comment信息
http://www.pythonchallenge.com/pc/def/channel.html

查看源文件其他的信息都是无关的,只有一个zip,将网址html改为zip,得到了一个zip文件:channel.zip

解压后看到里面有个readme.txt文件,里面内容是:

welcome to my zipped list.

hint1: start from 90052
hint2: answer is inside the zip

根据提示打开90052.txt文件,发现跟第四关是差不多的内容,先遍历它,python脚本如下:

import re  
p = re.compile(r"Next nothing is (\d+)")  
prefix = "channel/"  
seed = '90052'  
while True:  
    fname = prefix + seed + ".txt"  
    text = open(fname, 'r').read()  
    print text  
    lists = p.findall(text)  
       
    if lists:  
        seed = lists[0]  
        print "   Next is", seed  
    else:  
        break
得到结果:Collect the comments.

根据该提示和hint2,要取出zip中文件的comment信息,修改上面的脚本:

<span style="font-size:14px;">import re, zipfile  
p = re.compile(r"Next nothing is (\d+)")  
prefix = "channel/"  
seed = '90052'  
comments = []  
z = zipfile.ZipFile("channel.zip", "r")  
while True:  
    fname = prefix + seed + ".txt"  
    comments.append(z.getinfo(seed + ".txt").comment)  
    text = open(fname, 'r').read()  
    print text  
    lists = p.findall(text)  
      
    if lists:  
        seed = lists[0]  
        print "   Next is", seed  
    else:  
        break  
      
print "".join(comments)</span>

得到结果:

修改url,http://www.pythonchallenge.com/pc/def/hockey.html,回车:

it's in the air. look at the letters.

氧气在空气中,oxygen,进入第七关


7.对图片上的灰度条按ASCII编码

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

Hint1:图片中有一道灰度条。
没有其他提示,必然要从图片下手了。用到PIL。
该图像模式是RGBA。每个像素是个四元组,前3个分量分别代表红(R)、绿(G)、蓝(B),对于灰度图像,R=B=G。
这样,我们可以找到这段灰度条。将其数值按照ASCII码映射为字符(chr 函数)。

import Image
im = Image.open('oxygen.png')
print ('image information:',im.format,im.size,im.mode)
y_begin = 0
while True:
    p = im.getpixel((0,y_begin))
    if p[0] == p[1] == p[2]:
        break
    y_begin += 1
x_end = 0
while True:
    p = im.getpixel((x_end,y_begin))
    if not p[0] == p[1] == p[2]:
        break
    x_end += 1
print('Y first corrdinate:',y_begin,'X last coordinate:x_end:',x_end)
msg = []
for i in range(0,x_end,7):
    p = im.getpixel((i,y_begin))
    msg.append(chr(p[0]))
print(''.join(msg))
msg = [105, 110, 116, 101, 103, 114, 105, 116, 121]
print(''.join([chr(x) for x in msg]))

结果:integrity

8.用户名密码解密

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

网页源码里面有个链接,点进入,弹出一个登陆框需要用户名和密码,而注释里面有一段加密的文字,un,pw。这两个应该就是账号和密码,现在要做的是解密,根据图像是蜜蜂bees,根据发音有可能是bz2加密的,python解密代码如下:

import bz2  
un = 'BZh91AY&SYA\xaf\x82\r\x00\x00\x01\x01\x80\x02\xc0\x02\x00 \x00!\x9ah3M\x07<]\xc9\x14\xe1BA\x06\xbe\x084'  
pw = 'BZh91AY&SY\x94$|\x0e\x00\x00\x00\x81\x00\x03$ \x00!\x9ah3M\x13<]\xc9\x14\xe1BBP\x91\xf08'  
print bz2.decompress(un)  
print bz2.decompress(pw)  

输出结果:

huge
file
输进去,进入下一关


9.图片相关操作

http://www.pythonchallenge.com/pc/return/good.html(账号:huge  密码:file)

根据提示是要将里面的点转换为线,画出图像,使用python脚本如下:

import Image, ImageDraw  
img = Image.new('RGB', (640,480))  
draw = ImageDraw.Draw(img)  
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]  
draw.line(first)  
draw.line(second)  
img.show()  
画出的图像为牛,输入bull,进入下一关。

10.look and say 正则表达式

http://www.pythonchallenge.com/pc/return/bull.html

有个sequence.txt文件,点进去:

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

查找规律是:

后面的一个数是前面的数的描述

1

11                      代表前面是1个1

21                     代表前面是2个1

1211                 代表前面是1个2,1个1

111221            代表前面是1个1,1个2,2个1

312211

13112221

然后求第30个元素的长度,python实现如下:

import re  
def describe(s):  
    lists = [str(len(m.group(0))) + m.group(1) for m in re.finditer(r"(\d)\1*", s)]  
#     print lists  
    return "".join(lists)  
s = "1"  
for dummy in range(30):  
    s = describe(s)  
    print s  
print len(s)
结果:5808

re.finditer(r"(\d)\1*", s)
匹配s中的数字,相邻数字若相同,则划分为一组

返回值m.group(0):相同数字组

返回值m.group(!):该组的首字符

你可能感兴趣的:(python challenge 0-10)