ITeye博客

Python Challenge 3

finux · 2011-04-26 13:28 · Python · 编程语言 · Markdown

Python Challenge 3: 

One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

 

大概的意思就是:有一个小写字母,它两旁有且仅有3个大写字母;然后把所有这些小写字母串接起来即可;

可举例:helloAAApBBBworldCCCyDDDwhat,那么解析后结果即是py

仍然是一个文本处理的题目,个人觉得用正则表达式处理最方便不过了,呵呵。。。

表达式:re.compile('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', re.DOTALL)

 

那么代码就相当简单了~

 

#!/usr/bin/env python
#encoding = utf-8

import urllib
import re

def main():
        url = 'http://www.pythonchallenge.com/pc/def/equality.html'
        content = urllib.urlopen(url).read()
        reg = re.compile('[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]', re.DOTALL)
        result = ''.join(reg.findall(content))
        print result

if __name__ == '__main__':
        main()

 

运行结果就是linkedlist,那么把URL修改成XXX/linkedlist.php就是第4题了。

不知道还有没有更方便的处理方式呢?呵呵。。。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。