Pythonchallenge一起来闯关(二)

前情提要:Pythonchallenge一起来闯关(一)

这一篇来闯关10-15.感觉这几关比先前的难了不少,有的题目完全没思路.

10.

页面源码中的链接点击后有a = [1, 11, 21, 1211, 111221,

google之后知道这是一个叫做http://en.wikipedia.org/wiki/Look-and-say_sequence的数列.下面是一种比较简洁的解法.

import re

x="1"
for each in range(5):
    print(x)
    l = re.findall(r"(\d)(\1*)", x)
    print(l)
    x="".join([str(len(i+j))+i for i,j in re.findall(r"(\d)(\1*)", x)])

    #re.findall()返回一个元祖的list,元祖的元素个数为表达式中group的个数
    #re中\number表示匹配group(number)匹配到的内容,比如说(\d)(\1)如果(\d)匹配的是2,那么这里的\1则要匹配数字2
    #如果(\d)匹配到的是1,那么这里的\1则要匹配数字1
    
    #print(x)

print(len(x))
注意一下正则表达式中\number的意思.我看完文档后写了好几个例子才搞清楚是什么意思.
>>> re.findall(r‘(\d)(\w)\1‘,"1a1234")
[(‘1‘, ‘a‘)]
>>> re.findall(r‘(\d)(\w)\1‘,"2a1234")
[]
>>> re.findall(r‘(\d)(\w+)\1‘,"2a1234")
[(‘2‘, ‘a1‘)]

 

11.

根据提示 “odd even"猜想取出坐标(x,y)一个为奇数一个为偶数的像素点.按这个思路写完之后发现不对,换个思路,将原图中坐标(x,y)一个为奇数一个为偶数的像素点去除(将该点像素设成黑色(0,0,0)).得到答案。

import urllib.request as ur
from PIL import Image



def main():
    #需要用户名密码访问url
    url = "http://www.pythonchallenge.com/pc/return/cave.jpg"
    username = huge
    password = file
    p = ur.HTTPPasswordMgrWithDefaultRealm()
    p.add_password(None, url, username, password)
    handler = ur.HTTPBasicAuthHandler(p)
    opener = ur.build_opener(handler)
    ur.install_opener(opener)

    dataBytes = ur.urlopen(url).read()
    f = open("11_cave.jpg","wb")
    f.write(dataBytes)

    im = Image.open("11_cave.jpg")
    w,h = im.size
    mode = im.mode
    bands = im.getbands()
    print(w,h)
    print(type(mode),mode)
    print(type(bands),bands)

    #imAnswer = Image.new(mode,(w,h),(255,255,255))      
    
    for i in range(w):
        for j in range(h):
            if (i + j)%2 == 1:
                pos = (i,j)
                im.putpixel(pos,0)
    
    im.save(11answer.jpg)

if __name__ == __main__:
    main()

 

12.

完全没有思路的一道题.搜索之后知道是有5张图片,二进制的数据被写到了一个叫做evil2.gfx的文件中,需要像挨个发牌一样一字节一字节的重新"发回到"5个图片文件....

太考验发散思维了... 

import urllib.request as ur

def main():
    #需要用户名密码访问url
    url = "http://www.pythonchallenge.com/pc/return/evil2.gfx"
    username = huge
    password = file
    p = ur.HTTPPasswordMgrWithDefaultRealm()
    p.add_password(None, url, username, password)
    handler = ur.HTTPBasicAuthHandler(p)
    opener = ur.build_opener(handler)
    ur.install_opener(opener)

    dataBytes = ur.urlopen(url).read()
    f = open("12_evil2.gfx","wb")
    f.write(dataBytes)

    print(len(dataBytes))
    types = [jpg,png,gif,png,jpg]
    for i in range(5):
        imagedata = dataBytes[i::5]
        image = open(12answer_%d.%s % (i,types[i]),wb)
        image.write(imagedata)

if __name__ == __main__:
    main()

执行代码后,有5张图片,前面4张字母连起来就是答案.用二进制的方式打开图片文件.可以看到.jpg文件的首字节0xff,.png文件的首字节为0x89,猜测应该是首字节或者是前几字节

代表图片的格式的字段,没有去深究.

 

13

页面源码中有一个链接指向phonebook.php,点击进入以后出现提示:This XML file does not appear to have any style information associated with it. The document tree is shown below.google之后知道是少了某些描述文件,可以简单理解为类似于html中缺少了css,所以浏览器就无法正常展示格式吧.然而还是不知道要如何继续下去,去标准库中查找xml相关的内容,还是没啥思路.继续google页面中的节点名称<methodResponse>,找到了xmlrpc---xml远程调用,到这里问题就简单了.代码如下:

import xmlrpc.client

def main():
    x = xmlrpc.client.ServerProxy("http://www.pythonchallenge.com/pc/phonebook.php")  
    print(x.system.listMethods())  
    print(x.phone(Bert))

if __name__ == __main__:
    main()

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data.类似于webservice,server端实现了一些功能,并提供一些接口给客户端使用.

 

14.

这一题没啥思路,google了之后知道是要将下面的那个size为10000*1的条形码像上面的那个便便一样的面包一样不断旋转,最终得到一个100*100的
图.有了思路之后就好办了,我的代码如下:
import urllib.request as ur
from PIL import Image

l = [[i, i-1, i-1, i-2] for i in range(100, 0, -2)]
#print(l)

def main():
    im = Image.open("wire.png")
    #print(im.size)
    (width,height) = im.size
    mode = im.mode
    #pos = [(x,y) in range(width,height)]
    #print(pos)
    #print(width,height)
    #print(im.getpixel((639,486)))
        
    x,y=0,0
    imAnswer = Image.new(mode,(100,100),(255,255,255)) 
    #print(imAnswer.size)
    #左上角为坐标(0,0),左下角为坐标(0,99),右上角坐标为(99,0),右下角坐标为(99,99)
    #im.getpixel((i,0))
    #imAnswer.putpixel((0,0),(255,0,0))
    #imAnswer.putpixel((99,0),(0,255,0))
    #imAnswer.putpixel((0,99),(0,0,255))
    #imAnswer.putpixel((99,99),(255,255,255))
    #imAnswer.save(‘14Answer.jpg‘)

    lastPos=(-1,0) #上一个坐标位置
    maxStep=100 #当前最多走到多少步需要变向
    currentSteps=0 #当前方向已经走了多少步
    currentDirection=1 #1:向右,2:向下,3:向左,4向上
    
    for i in range(0,10000):
        pixel = im.getpixel((i,0))
        
        if currentDirection == 1:
            pos = (lastPos[0] + 1,lastPos[1])
        if currentDirection == 2:
            pos = (lastPos[0],lastPos[1] + 1)
            print(lastPos,pos)
        if currentDirection == 3:
            pos = (lastPos[0] - 1,lastPos[1])
        if currentDirection == 4:
            pos = (lastPos[0],lastPos[1] - 1)
        
        #print(currentDirection,lastPos,pos)
        
        try:
            imAnswer.putpixel(pos,pixel)
        except:
            pass
            #print(pos)
        
        #更新点的位置信息        
        lastPos = pos
        
        currentSteps += 1
        
        #处理到达边界的情况
        if (currentSteps == maxStep):
            if currentDirection == 1:
                currentDirection = 2
                currentSteps = 0
                maxStep -= 1
                #print("turn to down!",currentDirection)
            elif currentDirection == 2:
                currentDirection = 3
                currentSteps = 0
            elif currentDirection == 3:
                currentDirection = 4
                currentSteps = 0
                maxStep -= 1
            elif currentDirection == 4:
                currentDirection = 1
                currentSteps = 0

    imAnswer.save(14Answer.jpg)

if __name__ == __main__:
    main()

执行完后得到一张猫的图片,输入http://www.pythonchallenge.com/pc/return/cat.html得到提示:and its name is uzi. you‘ll hear from him later.把cat换成uzi即是下一关地址.

 

15.

这一题比较简单
标题是whom?图片是一个日历.应该是找出某个日期的某个人.图片中右下角的图仔细看可以看到2月29日,说明是闰年.
import calendar
from datetime import date

yearlist = []
for x in range(0,10):
    for y in range(0,10):
        year = 1000 + x*100 + y*10 + 6
        if calendar.isleap(year):
            yearlist.append(year)
#print(yearlist)

for year in yearlist:
    d = date(year,1,1)
    if d.weekday() == 3:   #周一到周日:0-6
        print(year)
#第二年轻的,是1756年.google 1756年1月26日 第二天是莫扎特诞生

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。