修改 我知道这个问题已被问了一百万次,但我似乎找不到任何适用于我特定情况的东西。出于上下文的目的,我在Windows操作系统上运行Python 3.6。
我尝试过以下代码:
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print(f"sum({partial})={target}")
#print("sum({})={}".format(partial, target))
if s == target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
#remaining = numbers[:i] + numbers[i+1:]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
s= [-765143.910000006, -14522903.45, -185360.52, -161161.559999994, -31947.78, 167450, 47715.46, -1725.24, -1532.91, 338381.23, -40962.19, -321.869999999997, -28215.17, -66345.71, 13063.28, -389.37, 6215992.30000001, 2193804.53000001, -458374.52, 106792.609999979, -335194.01, 203687.94, 91147.0500000003, -18.9500000000004, -19.1200000000016, -2.31494823310641]
k= [-2191.62999999806, 5481451.91, -17941.98, 166.719999999996, -2.72848410531878, -3.42659234320308, -13109322.84, -5320290.35000001, -977974.9, 2224562.69999999, 404360.300000002, 579934.88, 1131275.75, 3889264.3, 3364573.99000001, 5225874.59, 2191.62999999806, 176248.27, 19925.25, 2090.84, 11461.32, 3457.83, 4655.76, -17929.46, 449.48, 2187.61, 3084.35, 176274, 48909.78, 55.43]
x= [14795.41, 6497.05, 324.6, 5589.19, 2224.45, 5571.92, 3575.24, 3041.33, 4658.22, 6244.92, 433.59, 2146.55, 1489, 28686.93, 205, 2267.76, 1532.91, -12539.19, 46221.03, 9959.25, 20175.14, 735, 9449, 26880, 426.12, 1355.18, 220.48, 695.88, -389.99, -1.12, -37.56]
v= [-1.96999999999248, 1.58761892521397, -2.1600499167107, -2791.41999999999, 606814.85, -19.1200000000016, -1.49999999999995, -54.3300000000086, 34608.19, -661601.97, 3149949.45, 32247.78, 350.64, 328574.84, 42461.52, 1273, 6635.21, 504, -3100.27, 9868.07, 148179.28, 29205.46, -206.65, -552]
y = [s+k+x+v]
if __name__ == "__main__":
subset_sum(y, -765143.910000006)
答案 0 :(得分:2)
这一行
print(sum(%s)=%s) % (partial, target)
是错误的,因为您正在使用字符串格式的模式来处理不是字符串的事情。你可能想做
print("sum(%s)=%s" % (partial, target))
仍然是旧语法,您更喜欢the new syntax
print("sum({})={}".format(partial, target))
答案 1 :(得分:0)
关于SyntaxError:您使用的格式不正确。有关PyFormat网站的更多信息,请加快解决方案:
print("sum(%s) =%s" % (partial, target))
答案 2 :(得分:0)
您的代码无效,因为
print(sum(%s)=%s) % (partial, target)
应该是
print "sum(%s)=%s" %(partial, target)
此外,您需要在主声明
之后缩进该行if __name__ == "__main__":
subset_sum([3,9,8,4,5,7,10],15)
应该是这个
if __name__ == "__main__":
subset_sum([3,9,8,4,5,7,10],15)
此外,您的代码无法完全正常工作,因为subset_sum([3,9,8,4,5,7,10,-9],1)即使返回[10,-9]也不会返回结果= 1。
此外,为了改进您的代码,我建议使用memoization,以避免在大型数字集上执行此操作时不止一次计算相同的子集。
答案 3 :(得分:0)
我相信这段代码有效:
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
if len(numbers) == 0:
return
elif s == target:
print("sum({})={}".format(partial, target))
return
else:
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[:i] + numbers[i+1:]
subset_sum(remaining, target, partial + [n])
y = [-15, 5, 10, -10, 3, 7, -4, -2]
if __name__ == "__main__":
subset_sum(y, 6)
随着可能combinations的数量增加,这可能会非常缓慢,您可以通过数学计算并以天文数字增长,因此您觉得它永远不会结束。
N.B。我没有Python 3.6,所以格式化将在这里保持老派。
编辑:另一个注意事项是你应该never use equality between floats,就像你提出的新输入的终止条件一样。您可以查看this answer获取if isclose(s, target)
条件的灵感。