PythonChallenge4-7题详解Python版

第4题:http://www.pythonchallenge.com/pc/def/linkedlist.php
点击图片找到链接,根据页面提醒,更改nothing找到下一个链接。。。知道结束,应该是不是数字的时候结束。

根据正则匹配nothing的值应该简单的

import urllib   
import re

def next_url(p):
    text=urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' + str(p)).read()
    num=re.match('and the next nothing is ([0-9]+)',text)
    if not num: print text
    return num.group(1)

num=12345
for i in range(400) :num=next_url(num)

根据上面程序中间有需要自己手工更改num的值
下面一的代码和上面的差不多,但是这两个程序我都没有完整的运行结束,据说是网络的问题。

import urllib   
import re

def next_url(p):
    text=urllib.urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s' %p).read()
    num=re.search('([0-9]+)',text)
    if not num: print text
    print text,num.group(1)
    return num.group(1)

num=12345

for i in range(400) : 
    num=next_url(num)

=========================就这样吧======================
第5题链接:http://www.pythonchallenge.com/pc/def/peak.html

至于如何找到题目的,在之前的java写的代码说了下,但是主要还是看别人的博客说的,英语差也没办法的


import urllib

def get_urldata(p):
    text=urllib.urlopen('http://www.pythonchallenge.com/pc/%s'%p).read()
    return text

text=get_urldata('def/banner.p')

import pickle

data=pickle.loads(text)

print '\n'.join([''.join([p[0]*p[1] for p in row])for row in data])

对于最后一行,不知道什么意思特别是p[0]*p[1]

for line in pickle.load(data):  
    print ''.join([x[0] * x[1] for x in line]) 

这样也行,不明白什么意思
================================================================
第6题链接:http://www.pythonchallenge.com/pc/def/channel.html
上面的图片看着很邪恶。。。
http://www.pythonchallenge.com/pc/def/channel.html/zip 下载数据,与第4题差不多的

读取文件:



import zipfile import StringIO import urllib import re def get_challenge(s): return urllib.urlopen('http://www.pythonchallenge.com/pc/' + s).read() z=zipfile.ZipFile(StringIO.StringIO(get_challenge('def/channel.zip'))) print z.read('readme.txt') print z.read('90052.txt') 

找到最后一个文件的内容:


def nexttxt(p):
    text=z.read('%s.txt'%p)
    num=re.search('([0-9]+)',text)
    if not num:print text
    return num.group(1)


p=90052
for i in range(len(z.namelist())): 
    p=nexttxt(p)

Collect the comments.
Traceback (most recent call last):
File “”, line 3, in
File “”, line 5, in nexttxt
AttributeError: ‘NoneType’ object has no attribute ‘group’

继续:



zp=[] p=90052 for i in range(len(z.namelist())): zp.append(p)#文件名连接起来了 p=nexttxt(p) print ''.join([z.getinfo('%s.txt' % p).comment for p in zp]) 

或者:

zc=[]
for p in zp:
    zinfo=z.getinfo('%s.txt'%p)
    zc.append(zinfo.comment)

print zc

print ''.join(zc)

zc就是输出的结果:HOCKEY
================说明=================
string.join(seq):Merges (concatenates)以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
comment :获取文件的注释,具体怎么找到,我也不知道了
zinfo=z.getinfo(‘%s.txt’%p):获取信息
=============================================================
第7题链接:http://www.pythonchallenge.com/pc/def/oxygen.html
图片中的那条马赛克按照ASCII解码
读取图片信息:


import urllib
import re
from PIL import Image
def get_challenge(s):
    return urllib.urlopen('http://www.pythonchallenge.com/pc/' + s).read()

def get_image(s):
    return Image.open(StringIO.StringIO(get_challenge(s)))

p='def/oxygen.png'

im=get_image(p)

im=Image.opem('http://www.pythonchallenge.com/pc/def/oxygen.png')

查看图片大小:print im.size (629,95)

下面我就不知道怎么搞的了

print ''.join([chr(im.getpixel((i, 47))[0]) for i in xrange(0, 625,7)])  

print ''.join(map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121]))

对于输出:

“`

print ”.join([chr(im.getpixel((i, 47))[0]) for i in xrange(0, 625,7)])
smart guy, you made it. the next level is [105, 110, 116, 101, 103, 114, 105, 116, 121]pe_
print ”.join(map(chr, [105, 110, 116, 101, 103, 114, 105, 116, 121]))
integrity
`
第8题就是Integer.html

====================================
搞到了第8题了,几乎都是网上整理的程序,对Python所需学的东西还是很多的。下面要整理下所需的Python模块资料。

你可能感兴趣的:(python,正则匹配,py挑战)