【发布时间】:2019-08-06 17:41:13
【问题描述】:
我正在自学递归回溯。对于骰子求和问题,我不知道如何优雅地收集结果。
作为参考,这是我的代码,它只打印任何符合条件的骰子。理想情况下,我想改变它,而不是打印输出,我可以建立一个列表,列出那些选择的骰子并返回它。
下面是不符合我要求的代码
def dice_sum(num_dice: int, target_sum: int) -> None:
dice_sum_helper(num_dice, target_sum, [])
def dice_sum_helper(num_dice: int, target_sum: int, chosen: List[int]) -> None:
if num_dice == 0 and sum(chosen) == target_sum:
print(chosen)
elif num_dice == 0:
pass
else:
for i in range(1, 7):
chosen.append(i)
dice_sum_helper(num_dice - 1, target_sum, chosen)
chosen.pop()
我希望它做这样的事情
from typing import List
DiceResult = List[List[int]]
def dice_sum(num_dice: int, target_sum: int) -> DiceResult:
return dice_sum_helper(num_dice, target_sum, [])
def dice_sum_helper(num_dice: int, target_sum: int, chosen: List[int]) -> DiceResult:
if num_dice == 0 and sum(chosen) == target_sum:
# Return the value that meets the constraints
return chosen
elif num_dice == 0:
pass
else:
for i in range(1, 7):
chosen.append(i)
# Return the result of my recursive call and build the list of lists?
result = dice_sum_helper(num_dice - 1, target_sum, chosen)
return result.append(result)
# End of that logic
chosen.pop()
我更多的是寻找要使用的理论或模式,而不是确切的代码。如果不使用外部列表,我无法完全获取代码来收集和附加每个结果。
【问题讨论】:
标签: python recursion backtracking recursive-backtracking