我試圖制止for循環低於一次值(x,y)
或(z,2)
已經返回,這樣的價值i
不會繼續增加,而僅僅是暫停,當if
或elif
條件是第一暫停一個遞歸調用函數
def maxPalindrome(theList):
# students need to put some logic here
maxcomplist = theList[:]
maxcomplist.reverse()
control = len(theList) - 1
# exit if maxPalindrome is True
for i in range(control):
if maxcomplist[:] == theList[:]:
x = 0
y = len(theList)
return (x, y)
break
elif maxcomplist[i:control] == theList[i:control]:
successList = theList[i:control]
z = i
w = len(theList) - z - 1
return (z, w)
我該如何做到這一點?
而不是試圖找出如何「停止」遞歸執行,我可能會建議你檢查[recursion]上的Wikipedia頁面(http://en.wikipedia.org/wiki/Recursion)。更具體地說,閱讀**基本案例**,因爲這是你如何停止遞歸執行。 –
不是一個答案,但是當你開始的時候,一些遞歸函數可能很難讓你頭腦發熱。考慮拿起一本像Little Schemer這樣的書(http://www.ccs.neu.edu/home/matthias/BTLS/),教會你如何以更適合遞歸的方式分解問題。 –
這看起來不像遞歸函數。它不會在任何地方稱它爲自己。 – vikingosegundo