一个学习python好玩的网站

Thursday, January 28, 2010

http://www.pythonchallenge.com/

通过网页提示也好,回答问题,找到下一个链接的入口。

Level 0
http://www.pythonchallenge.com/pc/def/0.html
>>> 2**38
274877906944L

Level 1
http://www.pythonchallenge.com/pc/def/map.html
先解决hint,之前的想法是list后再用映射对每个字符ord后再加2,然后chr一下,最后jion出来,不过显然不对题意,因为z->b,y->a,并且字符不需要替换。
hint推荐的方法是string.maketrans()
>>> from string import maketrans
>>> 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."
>>> print str.translate(maketrans("yzabcdefghijklmnopqrstuvwx","abcdefghijklmnopqrstuvwxyz"))
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.
>>> str="map"
>>> print str.translate(maketrans("yzabcdefghijklmnopqrstuvwx","abcdefghijklmnopqrstuvwxyz"))
ocr

Level 2
http://www.pythonchallenge.com/pc/def/ocr.html
>>> import re
>>> str = "网页源码中的一票乱字符,记得先把换行符替换掉"
>>> re.sub(r'\W|_','',str)
'equality'

Level 3
http://www.pythonchallenge.com/pc/def/equality.html
>>> import re
>>> str = "网页源码中的一票乱字符,记得先把换行符替换掉"
>>> print "".join(["%s" % b for b in re.findall('^|[a-z]{1,1}[A-Z]{3,3}([a-z])[A-Z]{3,3}[a-z]{1,1}|$',str)])
linkedlist

Level 4
http://www.pythonchallenge.com/pc/def/linkedlist.php
一开始没有明白什么意思,就写了一个搓的
import re
import urllib2
running = True
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=46059'
while running:
response = urllib2.urlopen(url)
result = response.read()
mathes= re.findall("\d",result)
if len(mathes) == 0:
print result
running = False
else :
url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='+"".join([elem for elem in re.findall("\d",result)])
print result
print url

监视了屏幕的输出,才发现里面还是有玄机的。

import urllib2
nothing = "12345"

while nothing:
print nothing
html = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + nothing).read()
if "Divide" in html:
nothing = str(int(nothing) / 2)
else:
nothing = "".join([item for item in html.split() if item.isdigit()])

print html

peak.html

Level 5
http://www.pythonchallenge.com/pc/def/peak.html
import pickle
import urllib2
url = "http://www.pythonchallenge.com/pc/def/banner.p"
response = urllib2.urlopen(url).read()
banner = pickle.loads(response)
print '\n'.join(["".join([i[0]*i[1] for i in b]) for b in banner])

channel

Level 6
http://www.pythonchallenge.com/pc/def/channel.html
import zipfile,re
file = zipfile.ZipFile("channel.zip","r")
idx = "90052"
history = []
while True:
history.append(idx)
data = file.read(idx+".txt")
#print "File:"+idx+".txt\t"+data
idx = "".join(re.findall('\d',data))
if len(idx)==0:
break
print "".join(file.getinfo(idx+".txt").comment for idx in history)

Level 7
http://www.pythonchallenge.com/pc/def/oxygen.html
import Image,re
im = Image.open("oxygen.png")
print "Image Info:", im.format, im.size, im.mode
row = [im.getpixel((x, (im.size[1]-1)/2)) for x in range(0, im.size[0], 7)]
ords = [r for r, g, b, a in row if r == g == b]
print "".join(map(chr,ords))
L = re.findall('\d+',"".join(map(chr,ords)))
print "".join(map(chr,map(int,L)))

Image Info: PNG (629, 95) RGBA
smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]
integrity
提示:
1、使用Image模块
2、灰色部分隐藏了提示
3、灰色部分在图片的当中位置(im.size[1]-1)/2
4、RGB灰色为R,G,B三个值都相等r for r, g, b, a in row if r == g == b
5、仔细看图片,以及得出的list,发现只有第1个灰色块占据了5个像素,后面基本为7个像素,所以步长为7可以得到所有的灰度R,G,B值range(0, im.size[0], 7)
6、map是个好函数

Level 8
http://www.pythonchallenge.com/pc/def/integrity.html
import urllib2,re
url = "http://www.pythonchallenge.com/pc/def/integrity.html"
data = urllib2.urlopen(url).read()
unraw = "".join(re.findall(r'un: \'(.*)[\'$]',data))
pwraw = "".join(re.findall(r'pw: \'(.*)[\'$]',data))
print "user:",unraw.decode("string_escape").decode("bz2")
print "pass:",pwraw.decode("string_escape").decode("bz2")

Level 9
http://www.pythonchallenge.com/pc/return/good.html
import urllib2,Image,ImageDraw,re
url = "http://www.pythonchallenge.com/pc/return/good.html"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(url).read().replace("\n","")
first = re.findall('\d+',re.findall(r'first:(.*)second',page)[0])
second = re.findall('\d+',re.findall(r'second:(.*)-->',page)[0])
img = Image.new('RGB', (500,500))
draw = ImageDraw.Draw(img)
draw.line(map(int,first))
draw.line(map(int,second))
img.save("9.jpg")
img.show()

图片显示的是公牛(bull)哦!

Level 10
http://www.pythonchallenge.com/pc/return/bull.html
求a = [1, 11, 21, 1211, 111221, 这个序列的第30个字串的长度
首先看这个序列的规律
方法1:
strings = ['1','11']

for i in range(1,31):
j = 0
string = ''
while j < len(strings[i]):
count = 1
while j<(len(strings[i])-1) and (strings[i][j]==strings[i][j+1]):
j = j + 1
count = count + 1
string = '%s%d%c' % (string,count,strings[i][j])
j = j + 1
strings.append(string)
print len(strings[30])
5808
方法2:
import re
def describe(s):
return "".join([str(len(m.group(0))) + m.group(1) for m in re.finditer(r"(\d)\1*", s)])

s = "1"
for dummy in range(30):
s = describe(s)
print len(s)
5808
注意:m.group(0)记忆的是正则表达式的本身,m.group(1)记忆的是圆括号中匹配到内容

Level 11
http://www.pythonchallenge.com/pc/return/5808.html
import Image
im = Image.open("cave.jpg")
print "Image Info", im.format, im.size, im.mode
w,h=im.size[0],im.size[1]
new = Image.new(im.mode,(w//2, h//2))
for x in range(1,w,2):
for y in range(1,h,2):
new.putpixel((x//2, y//2), im.getpixel((x, y)))
new.save("odd.png", "PNG")
evil

Level 12
http://www.pythonchallenge.com/pc/return/evil.html
import urllib2
url = "http://www.pythonchallenge.com/pc/return/evil2.gfx"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)

page = opener.open(url).read()

for i in range(5):
file = open("evil_0%d.jpg" % i, "wb")
file.write(page[i::5])
file.close()

运行代码,查看5个图片,按顺序可以得到"dis","pro","port","ional","ity",但最后一个单词被划去了,因此我们得到单词 disproportional
注意:使用了::操作符,下面是用法
>>> s = 'abcdefgh1234dazadfa'
>>> s[0::5]
'af3a'
>>> s[0:len(s):5]
'af3a'

Level 13
http://www.pythonchallenge.com/pc/return/disproportional.html
import xmlrpclib,urllib2
url = "http://www.pythonchallenge.com/pc/return/evil4.jpg"
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, "huge", "file")
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
page = opener.open(url).read()
xmlrpc = "http://www.pythonchallenge.com/pc/phonebook.php"
service = xmlrpclib.ServerProxy(xmlrpc)
print service.phone(page[:4])
555-ITALY

Level 14
http://www.pythonchallenge.com/pc/return/italy.html

Posted by Michael.Ding at 3:00 PM

0 comments:

Post a Comment