穿越Python Challenge
第九关 Image
从页面上的图片可以看到有一串点,那么是不是代表该关与图像点有关? 我们从页面源码可以看到,有两段数字序列first和second,而有一个提示first+second=? 什么意思呢?难道是说(first, second)代表了图像点的坐标?不像,两段序列的长度有很大差异。那么算符+还有什么含义呢,有可能是将两段序列拼起来,然后每两个数字代表一个图像点。通过处理,我们在原图片上按照给定的坐标涂黑点,却发现什么都看不清;因此我们按照图片的规格新建一个图片,在黑底上涂白点,处理程序如下:
#!/bin/python
# file: good.py
import re, Image
file = open('d:\Python\good.html')
message = re.findall('(<!--[^-]+-->)', file.read(), re.S)[1]
file.close()
first = re.findall('(\d+)',re.findall('first:(.+)second',message,re.S)[0],re.S)
second = re.findall('(\d+)',re.findall('second:(.+)>',message,re.S)[0],re.S)
all = first + second
im = Image.open('d:\Python\good.jpg')
im2 = Image.new(im.mode, im.size)
for x in range(0,len(all),2):
im2.putpixel((int(all[x]),int(all[x+1])),(255,255,255,255))
im2.show()
结果出现一只牛的图样,根据英文拼写,我们得到cow单词,进入页面 ,得到提示“hmm. it's a male. ” 雄性的牛?公牛bull,进入页面 ,通关!
第十关 序列推理
图片下方有一串字符“len(a[30]) = ?”,学过编程的都会意识到a可能是一个以字符串为元素的列表。那么a从哪里来呢?我们看页面源码,发现有一个href链接,打开网页 ,我们得到提示“a = [1, 11, 21, 1211, 111221, ”。很显然,这需要我们从已有的数字序列中找到规律,继而计算出第31个元素。通过思考,发现每个元素其实就是按顺序对前一个数字序列的解释,比如说“21”包含了1个2,1个1,因此下一个元素是‘2111’,按照这种规律,‘111221’包含了3个1,2个2,1个1,因此下一个元素应该是‘312211’,我们编写代码如下:
#!/bin/python
# file: getLength.py
strings = ['1','11']
for i in range(1,31):
j = 0
string = ''
while j < len(strings[i]):
count = 1
相关文档:
# 015
# 默认参数的本质是:
# 不论你是否提供给我这个参数,我都是要用它,
# 那么我就要想好在你不向我提供参数的时候我该使用什么。
# 或者,这样来理解:
# 有一些参数在一般情况下是约定俗成的,
# 但,在极少情况下会有一些很有个性的人会打破传统而按照自己的习惯来做事
def theFirstDayInAWeek(theDay = 'Sunda ......
源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output:
>>>
This is the first line.
This is the second line.
This is ......
源代码下载:下载地址在这里
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()
output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()
output:
e.g.3
实现根据原始文件有没有最后一行空行的情况来进行&ldqu ......
源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()
output:
22
Hello! ......
源代码下载:下载地址在这里
# 039
while True:
try:
x = int(raw_input('Input a number:'))
y = int(raw_input('Input a number:'))
z = x / y
except ValueError, ev:
print 'That is not a valid number.', ev
except ZeroDivisionError, ez:
print 'Di ......