我正在学习递归,我有下面的树:
# cough
# Yes / \ No
# fever healthy
# Yes / \ No
# influenza cold
使用以下代码:
def diagnose_helper(symptoms, node, symptoms_checked):
if node.positive_child == None and node.negative_child == None:
print(node.data, "In")
return node.data
for symp in symptoms:
if symp == node.data and symp not in symptoms_checked:
symptoms_checked.append(symp)
return diagnose_helper(symptoms, node.positive_child,
symptoms_checked)
else:
return diagnose_helper(symptoms, node.negative_child,
symptoms_checked)
检查症状是否在列表中,并返回当前情况。 即,对于该输入:
diagnose_helper(["cough, fever"], root, [])
它应该返回“流感”,因为咳嗽->;是的,发烧->;是的,但它实际上又变冷了。 我似乎不知道为什么,如果有人知道我会接受的
函数将只检查症状中的第一个元素,因为if语句的两个分支中都有一个返回。或许可以这样尝试:
相关问题 更多 >
编程相关推荐