问题描述
我在此链接中找到了以下解决方案:https://stackoverflow.com/a/65029364/15413964
但是,当没有解决方案时,它不考虑。例如,给定一个列表,当它被分成 2 个时,两个列表永远不会相加为相同的数量。
基本情况应该是什么使得给定的列表没有解决方案,并且它会为左右返回 None ?
def sumSplit(left,right=[],difference=0):
sumLeft,sumRight = sum(left),sum(right)
if sumLeft<sumRight or len(left)<len(right): return
# return a solution if sums match the tolerance target
if sumLeft-sumRight == difference:
check = True
return left,right
# recurse,brutally attempting to move each item to the right
for i,value in enumerate(left):
solution = sumSplit(left[:i]+left[i+1:],right+[value])
if solution:
return solution
#test case:
print(sumSplit([-14,3,4,13,-1,-5,5,-10,8,-4,10,-12,11,9,12,-6,-11,-9,-8]))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)