我正在回答一個問題,其中給出了 3 個字串:one,two和three。我們想回傳一個布林值來說明我們是否可以string three通過交織字串one和來創建two。
例如:
one = "aab", two = "aac", three = "aaab" -> this should return True
然而
one = "abb", two = "acc", three = "aaab" -> this should return False
我的方法是兩個簡單地創建three pointers指向三個字串的開頭。當我們找到字串one and three或之間的匹配項時,將它們一一遞增two and three。
上述變得棘手時pointer_one,并pointer_two點在相同的字符one,并two分別!在這種情況下,我們需要運行一個recursion. 現在我的問題是我們什么時候開始向recursive stack. 我們可能會到達終點 string three,并且可以return True為整體功能。
然而,在我們的遞回堆疊中仍然有(部分完成的)遞回呼叫!所以這個最新的return True, 將被傳遞回前一個呼叫者,但我只想return True針對整個函式并忽略遞回堆疊中的剩余專案 - 無論如何要這樣做嗎?
或者我是否必須以這樣的方式撰寫代碼,return直到堆疊為空,然后return True才能進行最終呼叫?
我寫的代碼很長,所以我暫時省略了它,但如果需要,我可以包含它。
謝謝!
我的代碼:
def interweavingStrings(one, two, three, pointer_one=0, pointer_two=0, pointer_three=0):
possible_interwoven = False
while pointer_three < len(three):
if one[pointer_one] == two[pointer_two]:
possible_interwoven = interweavingStrings(one, two, three, pointer_one 1, pointer_two, pointer_three 1)
if not possible_interwoven:
possible_interwoven = interweavingStrings(one, two, three, pointer_one, pointer_two 1,
pointer_three 1)
if not possible_interwoven:
break
if pointer_two <= len(two) - 1 and three[pointer_three] == two[pointer_two]:
pointer_two = 1
pointer_three = 1
possible_interwoven = True
if pointer_one <= len(one) - 1 and three[pointer_three] == one[pointer_one]:
pointer_one = 1
pointer_three = 1
possible_interwoven = True
if pointer_two <= len(two) - 1 and pointer_one <= len(one) - 1 and one[pointer_one] != three[pointer_three] and two[pointer_two] != two[pointer_two]:
return False
return possible_interwoven
uj5u.com熱心網友回復:
這應該是遞回函式結構的一部分。當您進行遞回呼叫時,如果它回傳 True,則從當前呼叫回傳一個值為 True。
例如:
def canweave(a,b,c):
if not c : return True
if len(a) len(b)<len(c): return False
if a and a[0]==c[0] and canweave(a[1:],b,c[1:]):
return True # works with 1st of a
if b and b[0]==c[0] and canweave(a,b[1:],c[1:]):
return True # works with 1st of b
return canweave(a[1:],b[1:],c) # try with other chars
# return result from recursion
print(canweave("aab","aac","aaab")) # True
print(canweave("abb","acc","aaab")) # False
對您的代碼的觀察:
結果possible_interwoven = interweavingStrings(...)應該是確定的,而不僅僅是一種可能性。當您從該呼叫中獲得 True 時,您必須確定其余字符是“可交織的”。因此,當possible_interwoven為 True時,您應該立即回傳True。這將自動滴入遞回呼叫以產生最終結果。
您如何推進指標也存在問題,但我無法通過簡單的調整找到一種簡單的方法來解決這個問題。
這是使用您的指標方法的修訂版本:
def interweavingStrings(one, two, three,
pointer_one=0, pointer_two=0, pointer_three=0):
if pointer_three == len(three):
return True
if pointer_one >= len(one) and pointer_two >= len(two):
return False
if pointer_one < len(one) and one[pointer_one] == three[pointer_three]:
if interweavingStrings(one,two,three,
pointer_one 1,pointer_two,pointer_three 1):
return True
if pointer_two < len(two) and two[pointer_two] == three[pointer_three]:
if interweavingStrings(one,two,three,
pointer_one,pointer_two 1,pointer_three 1):
return True
return interweavingStrings(one,two,three,
pointer_one 1,pointer_two 1,pointer_three)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/355896.html
上一篇:phyloseq箱線圖的變數著色
