-2
我一直在研究這個Palindrome Python問題一段時間了。這裏是提示:迴文Python程序
'''
palindrome(T) is True if T is the same as
the backwards version of T, and otherwise
is False.
palindromes(L) returns a list of the palindromes in L
>>> palindrome("madam")
True
>>> palindrome([6,9,6])
True
>>> palindrome("T r ")
False
>>> palindromes([ [1], [3,2], [5,1,5], [0,0,1], [7,3,7,3] ])
[[1], [5, 1, 5]]
>>> palindromes("son daughter dad mom ewe any".split())
['dad', 'mom', 'ewe']
>>> palindromes("stressed desserts stop pots live evil".split())
[]
>>> palindromes(["stressed desserts", "stop pots", "can can", "too few"])
['stressed desserts', 'stop pots']
'''
if __name__ == "__main__":
import doctest
doctest.testmod()
這是我到目前爲止寫:
def palindrome(L):
if L == L[::-1]:
return True
else:
return False
def palindromes(L):
return filter(palindrome ,range(6))
我能夠拿到前四周的測試時,他們是簡單的真/假答案,但是當我有開始使用過濾功能我開始有麻煩了。我知道我必須做某種索引,但我不確定如何。
提示:您可能希望用傳遞給您的函數的'L'做些事情。 –
你也可以在第一個函數中返回L == L [:: - 1]'。 – refaim
爲什麼'範圍(6)'? – Don