Python Challenge Level 3

Entry: http://www.pythonchallenge.com/pc/def/equality.html

Requi­re­ment: One small letter, sur­roun­ded by EXACTLY three big body­gua­rds on each of its sides.

思路:

要求是找出加在三个大写字母中间的一个小写字母。这个小写字母左右两个方向都有三个大写字母。一看就知道应该用正则表达式解决,那么这个正则表达式怎么写呢?

有且仅有一个小写字母:[a-z]
有且仅有三个大写字母:[A-Z]{3}

那么这个Regu­lar Expression的Pattern就是:[A-Z]{3}[a-z][A-Z]{3}

………………

不对。。在此我犯了一个小错误。上述表达式不能考虑到这种情况:ABCDaA­BCD。即三个大写字母再外侧是什么,也需要考虑进去。它们不应该是大写字母了。

所以还应该加上非大写字母的Pattern:[^A-Z]

So.… Pattern: [^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]

源代码:

content='''......'''

import re
import string
result=re.findall(r'[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]',content)
result=[l[4] for l in result]
result=string.join(result,'')
print result

结果:

lin­ke­dlist

  1. Oj
    Jul 8th, 2009 at 17:59 | #1

    r.findall(‘[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]’, con­tent)

    结果也是lin­ke­dlist
    不过我试的http://www.pythonchallenge.com/pc/def/linkedlist.html无法打开,只显示linkedlist.php几个字样

    卡到这里了,郁闷~~

    ReplyReply

    [Reply]

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>