第五题的网址:http://www.pythonchallenge.com/pc/def/peak.html,如果大家访问不了该网站,可以看看下面的网站截图。
这道题比上一道题好,最起码还给点提示,“pronounce it”,但是发什么音呢?这就是一个土堆呀……我们看看标题,“peak hell”,原来是这个词组的发音。我们知道发音,但是不知道下一题的网址……还是看看页面的源代码吧,应该有更多的提示。
<html><head><title>peak hell</title><link rel="stylesheet" type="text/css" href="../style.css"></head><body><center><img src="peakhell.jpg"/><br><font color="#c0c0ff">pronounce it<br><peakhell src="banner.p"/></body></html><!-- peak hell sounds familiar ? -->
源代码给我们的提示虽少,但是很关键。
- <!-- peak hell sounds familiar ? –>
- <peakhell src="banner.p"/>
第一个提示信息让我们想一想“peak hell”的发音跟什么比较像……这个我可想不出来,不过我们可以到“Python Documentation”中去翻一翻。对,就在“index”中查看以P开头的对象。
PPATH (environment variable), [Link]pickle (standard module)pop() (list method) Python3000PYTHONPATH (environment variable), [Link], [Link], [Link], [Link]PYTHONSTARTUP (environment variable), [Link]
哈哈,找到了,就是pickle,和“peak hell”发音的确相似。
第二个提示信息很奇怪吧,这个不是html的标签呀,而且banner.p好像一个文件啊!我们试一试把它下载下来。下载完banner.p后,用文本编辑器打开,果然有东西,但是不知道这些是什么信息。没关系,上面不是提示我们使用pickle了嘛,学习一下pickle就知道了。
什么是pickle?看看官方文档就知道了。
The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. ``Pickling'' is the process whereby a Python object hierarchy is converted into a byte stream, and ``unpickling'' is the inverse operation, whereby a byte stream is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as ``serialization'', ``marshalling,''13.1 or ``flattening'', however, to avoid confusion, the terms used here are ``pickling'' and ``unpickling''.
看看banner.p中不知所云的字符,我们知道,我们要将它们转换成python对象了。
'''Created on 2011-7-18@author: hengha'''import pickledef unpickling(path):f=open(path)obj=pickle.load(f)return obj
if __name__ == '__main__':result=unpickling('banner.p')print result
激动人心的时刻到来了,让我们看看打印的结果:
[[(' ', 95)], [(' ', 14), ('#', 5), (' ', 70), ('#', 5), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 15), ('#', 4), (' ', 71), ('#', 4), (' ', 1)], [(' ', 6), ('#', 3), (' ', 6), ('#', 4), (' ', 3), ('#', 3), (' ', 9), ('#', 3), (' ', 7), ('#', 5), (' ', 3), ('#', 3), (' ', 4), ('#', 5), (' ', 3), ('#', 3), (' ', 10), ('#', 3), (' ', 7), ('#', 4), (' ', 1)], [(' ', 3), ('#', 3), (' ', 3), ('#', 2), (' ', 4), ('#', 4), (' ', 1), ('#', 7), (' ', 5), ('#', 2), (' ', 2), ('#', 3), (' ', 6), ('#', 4), (' ', 1), ('#', 7), (' ', 3), ('#', 4), (' ', 1), ('#', 7), (' ', 5), ('#', 3), (' ', 2), ('#', 3), (' ', 5), ('#', 4), (' ', 1)], [(' ', 2), ('#', 3), (' ', 5), ('#', 3), (' ', 2), ('#', 5), (' ', 4), ('#', 4), (' ', 3), ('#', 3), (' ', 3), ('#', 4), (' ', 4), ('#', 5), (' ', 4), ('#', 4), (' ', 2), ('#', 5), (' ', 4), ('#', 4), (' ', 3), ('#', 3), (' ', 5), ('#', 3), (' ', 3), ('#', 4), (' ', 1)], [(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 3), (' ', 4), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 6), ('#', 4), (' ', 2), ('#', 4), (' ', 1)], [(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 10), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 7), ('#', 3), (' ', 2), ('#', 4), (' ', 1)], [('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 5), ('#', 2), (' ', 3), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 7), ('#', 3), (' ', 2), ('#', 4), (' ', 1)], [('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 10), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 14), (' ', 2), ('#', 4), (' ', 1)], [('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 4), ('#', 4), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 12), ('#', 4), (' ', 1)], [('#', 4), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 5), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 12), ('#', 4), (' ', 1)], [(' ', 1), ('#', 3), (' ', 11), ('#', 4), (' ', 5), ('#', 4), (' ', 1), ('#', 4), (' ', 5), ('#', 3), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 12), ('#', 4), (' ', 1)], [(' ', 2), ('#', 3), (' ', 6), ('#', 2), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 3), (' ', 4), ('#', 4), (' ', 4), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 3), (' ', 6), ('#', 2), (' ', 3), ('#', 4), (' ', 1)], [(' ', 3), ('#', 3), (' ', 4), ('#', 2), (' ', 3), ('#', 4), (' ', 5), ('#', 4), (' ', 3), ('#', 11), (' ', 3), ('#', 4), (' ', 5), ('#', 4), (' ', 2), ('#', 4), (' ', 5), ('#', 4), (' ', 4), ('#', 3), (' ', 4), ('#', 2), (' ', 4), ('#', 4), (' ', 1)], [(' ', 6), ('#', 3), (' ', 5), ('#', 6), (' ', 4), ('#', 5), (' ', 4), ('#', 2), (' ', 4), ('#', 4), (' ', 1), ('#', 6), (' ', 4), ('#', 11), (' ', 4), ('#', 5), (' ', 6), ('#', 3), (' ', 6), ('#', 6)], [(' ', 95)]]
不是我们期待的结果,还需要我们继续努力呀!我们获得的是一个list,里面的元素还是list,而且这些子list的元素个数不同。这些子list中的元素是元组,前面的不是' ',就是'#',后面的是数字。不过我们认真看一下,会发现一个特点,就是每个子list中的数字之和等于95。例如第二个子list,14+5+70+5+1=95。这是不是在用' '和'#'画图呀!我们试一试就知道了,修改代码如下:
'''Created on 2011-7-18@author: hengha'''import pickledef unpickling(path):f=open(path)obj=pickle.load(f)return obj
if __name__ == '__main__':result=unpickling('banner.p')for sublist in result:str=''for (a,b) in sublist:str+=a*bprint str
这下再看看程序的结果:
##### ######### ######## ######## ######## ######## ######## ######## ####### #### ### ### ##### ### ##### ### ### ####### ## #### ####### ## ### #### ####### #### ####### ### ### ####### ### ##### #### ### #### ##### #### ##### #### ### ### ####### #### #### ### ### #### #### #### #### ### #### ####### #### #### ### #### #### #### #### ### ### ######## #### #### ## ### #### #### #### #### #### ### ######## #### #### ########## #### #### #### #### ############## ######## #### #### ### #### #### #### #### #### #### ######## #### #### #### ### #### #### #### #### #### ####### #### #### #### ### #### #### #### #### ### ####### ## #### #### ### #### #### #### #### #### ### ## ####### ## #### #### ########### #### #### #### #### ### ## ####### ###### ##### ## #### ###### ########### ##### ### ######
看到这个结果,我们终于放心了,这道题基本解决了!让我们试一试将peak换成channel,成功进入下一道题!