Pythonchallenge一起来闯关(二)
这一篇来闯关10-15.感觉这几关比先前的难了不少,有的题目完全没思路.
页面源码中的链接点击后有a = [1, 11, 21, 1211, 111221,
google之后知道这是一个叫做http://en.wikipedia.org/wiki/Look-and-say_sequence的数列.下面是一种比较简洁的解法.
import re x=""
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))
根据提示 “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()
完全没有思路的一道题.搜索之后知道是有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,猜测应该是首字节或者是前几字节
代表图片的格式的字段,没有去深究.
页面源码中有一个链接指向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端实现了一些功能,并提供一些接口给客户端使用.
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即是下一关地址.
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日 第二天是莫扎特诞生
Pythonchallenge一起来闯关(二)的更多相关文章
- Pythonchallenge一起来闯关
http://www.pythonchallenge.com/是一个在线的python过关游戏,一共有33关.玩这个游戏对熟悉python用法及相关库的使用都很有好处. 目前做到了第九关.python ...
- 网页闯关游戏(riddle webgame)--SQL注入的潘多拉魔盒
前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. web开发初学者往往会忽视一些常见的漏洞 ...
- 淘宝ued - 前端智勇大闯关(第三季)答案(更新)
淘宝ued - 前端智勇大闯关(第三季)答案(更新) 下午在微博上看到了淘宝智勇大闯关第三季的信息,感觉挺有意思的,于是就尝试做了下.附上题目地址: http://ued.campus.alibaba ...
- 《JavaScript闯关记》视频版硬广
<JavaScript闯关记>视频版硬广 stone 在菜航工作时,兼任内部培训讲师,主要负责 JavaScript 基础培训,2016年整理的<JavaScript闯关记>课 ...
- XSS Challenges闯关笔记
前言 做xss做疯了再来一个. 地址:https://xss-quiz.int21h.jp/ ,这个貌似是日本的一个安全研究员yamagata21做的. 做到第九关就跪了,而总共有二十关.一半都还没有 ...
- W3CSchool闯关笔记(Bootstrap)
该闯关内容与JS闯关衔接. 每一题的答案均在注释处, 第一关:把所有的HTML内容放在一个包含有container-fluid的class名称的div下(注意,是所有的HTML内容,style标签属于 ...
- THEPYTHONCHALLENG闯关记录
由于是自己看视频学python,总觉得不写几行代码就什么都没有学到. 找了一个写代码的网站其实只是因为这个看起来好玩. 闯关地址http://www.pythonchallenge.com/index ...
- python 闯关之路四(下)(并发编程与数据库编程) 并发编程重点
python 闯关之路四(下)(并发编程与数据库编程) 并发编程重点: 1 2 3 4 5 6 7 并发编程:线程.进程.队列.IO多路模型 操作系统工作原理介绍.线程.进程演化史.特点.区别 ...
- C#设计模式开启闯关之路
前言背景 这是一条望不到尽头的编程之路,自踏入编程之路开始.就面临着各式各样的挑战,而我们也需要不断的挑战自己.不断学习充实自己.打好坚实的基础.以使我们可以走的更远.刚踏入编程的时候.根据需求编程, ...
随机推荐
- Flesch Reading Ease(模拟)
http://poj.org/problem?id=3371 终于遇到简单一点的模拟题了.不过本人真心没有耐心读题目... 它的大致意思就是给一段合法的文章,求出这段文章的单词数,句子数,音节数,按照 ...
- Paths on a Grid
http://poj.org/problem?id=1942 题意:一个n*m的格子,从左下角走到右上角有多少种走法,规定只能向上或向右走: 思路:解法挺水的,高中学组合数的时候老师给讲过:求C[m+ ...
- 3 D. Least Cost Bracket Sequence
题目大意: 这是一个规则的字符括号序列.一个括号序列是规则的,那么在序列里面插入‘+’ 和 ‘1’ 会得到一个正确的数学表达式. 合法:(())(), (),(()(())) 不合法:)(,((),( ...
- 【转】文件读写NDK(或Linux)
原文网址:http://www.ithao123.cn/content-10709539.html 使用NDK进行文件读写,有利于保存数据的安全性,项目需要,要文件读写从Java中处理搬到Linux平 ...
- UDP 收/发 广播包
网络通信基础 如果网络中两个主机上的应用程序要相互通信,其一要知道彼此的IP,其二要知道程序可监听的端口.因为同一主机上的程序使用网络是通过端口号来区分的. UDP Socket的使用过程: 1. 初 ...
- unicode编码、字符的转换和得到汉字的区位码
一:unicode编码.字符的转换截图 二:unicode编码.字符的转换代码 using System; using System.Collections.Generic; using System ...
- socket(TCP)发送文件
一:由于在上一个随笔的基础之上拓展的所以直接上代码,客户端: using System; using System.Collections.Generic; using System.Componen ...
- SRM 406(1-250pt, 1-500pt)
DIV1 250pt 题意:有几家宠物店,vecort<int>A表示每家宠物店含有小狗占小狗总数的百分比.现在要做扇形统计图统计每家店的小狗百分比,如下图,问作出来的扇形统计图中最多含有 ...
- lightoj 1063 求割点
题目链接:http://lightoj.com/volume_showproblem.php?problem=1063 #include<cstdio> #include<cstri ...
- RabbitMQ-清空队列中(一个channel或连接中)的Unacknowledged状态的消息
清空所有:nack 时将参数delivery-tag设为0,multiple设为1. 清空小于等于某delivery-tag的所有消息:nack 时将参数delivery-tag设为正数(介于1和92 ...