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
Recent Comments