Posts Tagged ‘Python Challenge’
-
Python Challenge Level 6
感觉变态程度越来越高了……
入口地址:http://www.pythonchallenge.com/pc/def/channel.html
进去之后,一支巨大的拉链映入眼帘。根据以往经验,查看源代码。分析了一下,只有一个内容有用:
<!-- <-- zip -->
根据那个拉链,再根据这个zip,看起来这道题跟zip关系十分之大。关键问题是怎么利用zip呢?百思不得其解。后来经过若干尝试,终于发现了原来隐藏的内容在channel.zip中。下载了这个文件,发现了一堆文件,里面又是指向下一个文件名的编号。和前几次中urllib的那回有异曲同工之处。打开readme文件:
welcome to my zipped list.
hint1: start from 90052
hint2: answer is inside the zip给了初始值,也给了暗示,答案在zip文件中。。按照当时写urllib的样子,写了这个循环的程序。
这里用到了python中的zip处理模块:zipfile。调用一个zip文件,可以通过zipfile.ZipFile()来打开;然后可以使用namelist()取得到所有zip文件中的文件名。infolist()可以得到关于zip的一些信息,另外通过read/write函数,可以读取、写入zip文件中的内容。
import re
import zipfilevalue,n = 90052,1
print “initial value: %s” % valuef = zipfile.ZipFile(“channel.zip”)
while(True):
try:content=f.read(“%s.txt” % value)
print content
value=re.search(r’(\d+)$’, content, re.IGNORECASE).group()
print “the next value: %s, this is the %d time” % (value,n)
n+=1
except:
break
给出来的结果:
initial value: 90052
Next nothing is 94191the next value: 94191, this is the 1 time
Next nothing is 85503
the next value: 85503, this is the 2 time
Next nothing is 70877
the next value: 70877, this is the 3 time
……
Next nothing is 8700
the next value: 8700, this is the 904 timeNext nothing is 45100
the next value: 45100, this is the 905 time
Next nothing is 68628
the next value: 68628, this is the 906 time
Next nothing is 67824
the next value: 67824, this is the 907 time
Next nothing is 46145
the next value: 46145, this is the 908 time
Collect the comments.
最后让我们收集comments。可是并没有发现zip文件里面有comments啊,这个是个大问题。搜索了一下,发现关于Zip文件操作的有zipfile和zipinfo两个类。而zipinfo下面有个comment,可能就是它吧。那怎么得到zipinfo的对象呢?zipfile里面有getinfo()和infolist()两个函数可以得到。经实验后得解。
最后得到了一个有点变态的list,看图:
又有点像是一个字符拼成的图像。不过不像上一道题,是由空格和*号组成的。这个里面什么都有。另外还是list类型,每个节点只存储了一个字符,干脆给连起来。然后就得到了结果。
hockey.html是出口?不是。。“it’s in the air. look at the letters. ”放大了,可以看到上面由字母组成的单词了,不是hockey,而是:oxygen。
我的代码:
import re
import zipfileimport string
value,n,comments = 90052,1,[]
#print “initial value: %s” % valuef = zipfile.ZipFile(“channel.zip”)
while(True):
try:content=f.read(“%s.txt” % value)
# print content
comments.append(f.getinfo(“%s.txt” % value).comment)
value=re.search(r’(\d+)$’, content, re.IGNORECASE).group()
# print “the next value: %s, this is the %d time” % (value,n)
n+=1
except:
break
#print comments
print string.join(comments)
下一级入口:oxygen.html
-
Python Challenge Level 5
r颇为变态的一道题。。
程序入口:http://www.pythonchallenge.com/pc/def/peak.html
文中强调:pronounce it… 把它发音读出来。通过观察源代码,发现它大量提到了 Peak hell 这个词。原来并没有接触过pickle,所以在这里费了半天劲,后来还是通过观察banner.p这个文件,发现很多存储的内容,很像序列化的东西。通过搜索后才确定是这个模块。
pickle是一个python的标准模块,几乎可以将任何一个Python对象作为字符串存储。在编程的时候我们经常管它叫做Serialization,也就是序列化。但Python里面将其称之为封装(pickle)。
例如,我们可以通过如下一段代码将一个tuple存入硬盘中:
import cPickle as pickle
x=(1,2,3,4,5)
f=open(‘c:\\1.txt’,‘w’)
pickle.dump(x,f)
f.close()而1.txt中是如下形式的一段内容:
(I1
I2
I3
I4
I5
tp1
.我们没有必要理解这段文档具体的存储方式,只是知道pickle可以存储内容就好了。接下来对banner.p解包。发现结果是一个list。
然后可以求出这个list的长度是23,而且list里面还嵌套list。拍拍脑袋想想这到底是做什么用的,想不出来……又仔细观察了一下,发现这个list的深度没有超过2,看起来像个矩阵。所以想到是不是图形?
后来实验了一下,成功了,呵呵。
附上源代码:
from urllib2 import urlopen
import cPickle as pickle
f=urlopen(‘http://www.pythonchallenge.com/pc/def/banner.p’)
result=pickle.Unpickler(f).load()
print len(result)buf = ”
for line in result:
for char in line:
buf += char[0]*char[1]
print buf + ‘\n’
buf = ”结果是?看图吧。。
下一级入口地址:channel.html
-
Python Challenge Level 4
Entry: http://www.pythonchallenge.com/pc/def/linkedlist.php
此题开始变态起来了,用到了linkedlist.php.…
思路:
点击中间的大图片,发现给了一个参数nothing,现在是12345。并告之下一个数是92512。把nothing的值替换为92512,则得到了下一个值为64505。周而复始。
这道题就是要我们用Python实现递推地找到下一个数的值,直到最后出现答案。手工做就会疯掉的。。。
需要使用Python的urllib库了。。
还有一个问题就是如何去匹配得到的内容中的数字。观察发现每一页的内容是这样的:
- and the next nothing is xxxxx
也就是说是一个以数字结尾的正则表达式。我们可以用$符号匹配结尾,用\d匹配数字,用+匹配至少一个字符。这样pattern就是:(\d+)$
另外到后来很变态。。网页中间突然断掉了,然后给出让把得到的结果除以二继续。。
加起来以后执行了208次,崩溃啊~这要是手动查找……
源代码:
from urllib import urlopen import re next='12345' next='46059' #the 198th times for n in range(400): webpage=urlopen('http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s'%next) text=webpage.read() webpage.close() next = re.search(r'(\d+)$', text, re.IGNORECASE).group() print 'This is the %s time' % n print 'text is %s' % text print 'next number is %s' % next print ''结果:
peak.html
-
Python Challenge Level 3
Entry: http://www.pythonchallenge.com/pc/def/equality.html
Requirement: One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.
思路:
要求是找出加在三个大写字母中间的一个小写字母。这个小写字母左右两个方向都有三个大写字母。一看就知道应该用正则表达式解决,那么这个正则表达式怎么写呢?
有且仅有一个小写字母:[a-z]
有且仅有三个大写字母:[A-Z]{3}那么这个Regular Expression的Pattern就是:[A-Z]{3}[a-z][A-Z]{3}
………………
不对。。在此我犯了一个小错误。上述表达式不能考虑到这种情况:ABCDaABCD。即三个大写字母再外侧是什么,也需要考虑进去。它们不应该是大写字母了。
所以还应该加上非大写字母的Pattern:[^A-Z]
So.… Pattern: [^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]
源代码:
content='''......''' import re import string result=re.findall(r'[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]',content) result=[l[4] for l in result] result=string.join(result,'') print result结果:
linkedlist
-
Python Challenge Level 2
http://www.pythonchallenge.com/pc/def/ocr.html
查看页面源代码后,发现了一堆乱码。。有一句话很重要:find rare characters in the mess below。题目是要我们在这堆乱码中找出仅有的几个字符。。
string模块里有个isalpha函数,来确定一个字符是否是字母。这次派上用场了。
源代码:
content='''......''' import string result=[c for c in content if c.isalpha()] print string.join(result,'')
结果:
equality
-
初学者的差距 [Python Challenge Level 1]
学习Python的时候,挑战Python Challenge。http://www.pythonchallenge.com/pc/def/map.html
我的源代码是这样的:
content='''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. ''' con=list(content) content='' for n in range(len(con)): c=con[n] if c in [' ','.','\'','(',')']: content+=c elif c=='y': content+='a' elif c=='z': content+='b' else: content+=chr(ord(con[n])+2) print content而实际上有更简单的方法:
import string content='''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=string.maketrans('abcdefghijklmnopqrstuvwxyz','cdefghijklmnopqrstuvwxyzab') print string.translate(content,result)对函数的不熟悉造成了编程时间的开销。以后继续努力了。
Paul’s Online Services
Dynamic Tag Cloud
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Recent Posts
- AIX Storage Learning 1
- 春节快乐! Happy Spring Festival!
- Sun is to the end of life
- 为cos-html-cache插件增加页面(Page)、标签(Tag)和分类(Category)的静态化功能
- How to configure Subversion in OpenSolaris
- 转载:IIS FastCGI PHP 环境下搭建 WordPress
- 在OpenSolaris下动态绑定域名
- Goodbye 2009, Hello 2010
- This Is It
- A Morse Code Exchanger
Recent Comments
- 新视界 (New Vision) » 在OpenSolaris下动态绑定域名 on 使用ZFS打造家庭廉价数据中心
- paul on Wordpress数据库转移网址变换的方法
- 知识 on Wordpress数据库转移网址变换的方法
- WP Super Cache V0.98 and IIS7 « Anders Heie on Speed up your WordPress Blog on IIS 7 by using WP-Super-Cache
- 博沈 on This Is It
- paul on 使用ZFS打造家庭廉价数据中心
- Anonymous on OpenSolaris 上的 Samba 服务器
- Anonymous on 使用ZFS打造家庭廉价数据中心
- Anonymous on OpenSolaris 上的 Samba 服务器
- Paul on 十年前和十年后的我们