所以我写了一个递归函数,找到最佳得分2 dna链,我的问题是,我有2个变种我想保存和使用(功能以外的变量,如提起的变量在java上):如何使用var从所有递归调用中中断?
1 )如果找到最佳解决方案,我不想继续搜索 2)并保留一个包含我迄今为止最好的解决方案的变种,所以如果当前的分数无法胜过它,我想停止搜索。
代码:
def main_fun(str1,str2)
best_so_far = None
best_score_possible = len(str)* match
score_fund = False
def recursive(index, index2, score)
if score_fund is True:
return
if score + chances < best_so_far:
return
if score == best_score_possible:
score_fund = True
# rest of code and other calls
这不是真正的代码,但它是我想要做的,任何想法? 谢谢
真正的(不完全)代码:
def best_helper(dna_1, dna_2, index_1, index_2, score):
best_future = score + calculate_best_future(index_1, index_2)
if best_future < worst_score_possible:# or best_future < best_so_far:
return worst_score_possible, dna_1, dna_2
#returns when done and complete rest with dash
if index_1 == len(first_dna) and index_2 == len(second_dna):
if len(dna_1) > len(dna_2):
dna_2 += '-' * (len(dna_1) - len(dna_2))
score += dash * (len(dna_1) - len(dna_2))
elif len(dna_2) > len(dna_1):
dna_1 += '-' * (len(dna_2) - len(dna_1))
score += dash * (len(dna_2) - len(dna_1))
if score == best_score_possible:
strand_found = True
if score > best_so_far:
best_so_far = score
return score, dna_1, dna_2
的问题是,我不能改变或从外部函数read瓦尔,它说:“没有解决refernce迄今为止最好的 – user2918984
?什么?相信你能... –
pyCharm说我不能:-( – user2918984