Udacity Design of Computer Programsのメモその2

左右対称の文字列を含む文字列を与えると、左右対称の開始位置から終了位置までを切り取ってくれる。

grow関数・・・ある特定の文字を中心に左右対称性があれば、2文字ずつ左右に伸ばして最長の左右対称文字列を取得する関数

 

def longest_subpalindrome_slice(text):
"Return (i,j) such that text[i:j] is the longest palindrome in text."
if text == '': return (0,0)
def length(slice): a,b = slice; return b-a
candidates = [grow(text, start, end)
for start in range(len(text))
for end in (start, start+1)]
return max(candidates, key=length)

def grow(text, start, end):
"Start with a 0- or 1- length palindrome; try to grow a bigger one."
while(start > 0 and end < len(text)
and text[start-1].upper() == text[end].upper()):
start -= 1; end += 1
return (start, end)

def test():
L = longest_subpalindrome_slice
assert L('racecar') == L('Racecar') == L('RacecarX') == (0, 7)