finux 发表于 2013-2-7 02:02:08

Python Challenge 3

Python Challenge 3: 
One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.
 
大概的意思就是:有一个小写字母,它两旁有且仅有3个大写字母;然后把所有这些小写字母串接起来即可;
可举例:helloAAApBBBworldCCCyDDDwhat,那么解析后结果即是py
仍然是一个文本处理的题目,个人觉得用正则表达式处理最方便不过了,呵呵。。。
表达式:re.compile('[^A-Z]{3}(){3}[^A-Z]', re.DOTALL)
 
那么代码就相当简单了~
 
#!/usr/bin/env python#encoding = utf-8import urllibimport redef main():      url = 'http://www.pythonchallenge.com/pc/def/equality.html'      content = urllib.urlopen(url).read()      reg = re.compile('[^A-Z]{3}(){3}[^A-Z]', re.DOTALL)      result = ''.join(reg.findall(content))      print resultif __name__ == '__main__':      main() 
运行结果就是linkedlist,那么把URL修改成XXX/linkedlist.php就是第4题了。
不知道还有没有更方便的处理方式呢?呵呵。。。
页: [1]
查看完整版本: Python Challenge 3