Python Challenge

时间:2024-07-05 09:05:08

0. 2的38次方

 print 2**38
 ##apply the result to the url

1. 看图是要right shift两位, 切片即可。

 import string

 intab = string.ascii_lowercase
 outtab = intab[2:] + intab[:2]
 trans_table = string.maketrans(intab, outtab)

 s = """
     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 s.translate(trans_table)
 print 'map'.translate(trans_table)

 ##The result is
 ##
 ##    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.
 ##ocr

 ##apply ocr to the url

2. 寻找出现次数少的character

 import string

 result = {}
 text = open('003help.txt').read()
 for ch in text:
     result[ch] = result.get(ch, 0) + 1

 print result
 print ''.join(ch for ch in result if result[ch]==1)
 s = []
 for i in text:
     if i in string.ascii_lowercase:
         s.append(i)
 print ''.join(s)

 ##apply 'equality' to the url

3. One small letter, surrounded by EXACTLY three big bodyguards on each of its sides. 正则表达式!

import re

pattern = re.compile('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]')
text = open('004help.txt').read()

t = re.findall(pattern, text)
print ''.join(t)

##apply to the url

4. follow the chain. 还是回到网页源代码,发现linkedlist.php?nothing=12345,替代linkedlist.php然后得到the next nothing is  .其实就是从这个网页中提取nothing后面的数字替换,作者说不超过400次,加一个for循环,如下:

 import re
 import urllib

 url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
 nothing = '
 pattern = re.compile(r'and the next nothing is (\d+)')
 for i in range(400):
     try:
         text = urllib.urlopen(url+nothing).read()
         nothing = pattern.findall(text)[0]
         print nothing
     except Exception, e:
         print e
         break
 print text

中间又一次exception,16044,让除以2,改成8022,继续,最后得到peak.html。

5. pronounce it. 读了好长时间,也没有神奇的事情发生,后来bing了才知道peakhell --> pickle. 也在我学识浅薄,没学习过这个模块。查了一下pickle的文档,这是一个python对象序列化的模块,感觉很抽象,好在api不复杂,页面源码提供了peakhell src,就是那个banner.p下载下来之后,用loads函数进行处理,下面是loads函数的说明(注意,不是load()):

pickle.loads(string)
Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.

 #cPickle是c语言实现的pickle模块
 import cPickle as pickle

 t = open('banner.p').read()
 s = pickle.loads(t)
 print s

结果是一个list,研究了半天只知道列和95(小学生思维)。这下更摸不着头脑了,只好__人有两件宝,双手和电脑__去搜索答案了。Amazing!

 for line in s:
     print ''.join(map(lambda pair: pair[0]*pair[1], line))

try it, try it! 不禁让我想到linuxlogo,这种ascii图还很养眼。

不过,前方的路坎坷又长

6. zip, 没有其他提示了,替换html,发现下载了channel.zip的文件,打开readme.txt,发现第一条提示类似前面的问题,又得到collect the comments,修改了re部分,发现没结果,后来百度才知道是有关zipfile这个module的。实现也不是很难:

 import re
 import os
 import zipfile

 def find_next():
     comments = []
     prefix = '
     suffix = '.txt'
     pattern = re.compile(r'\D+(\d+)')
     z = zipfile.ZipFile('/home/zhangqi/channel.zip', mode='r')
     while True:
         try:
             filename = 'channel/' + prefix + suffix
             text = open(filename).read()
             prefix = pattern.findall(text)[0]
             print prefix
             comments.append(z.getinfo(prefix + suffix).comment)

         except Exception, e:
             print e
             break

     print ''.join(comments)

 find_next()
 #it's in the air. look at the letters.

最后看组成hockey的字母,连环trick有木有!

stay tuned...