Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Loading Cargo by rybld2
def checkio(data):
TOTAL = sum(data)
res, ens = TOTAL, {0}
for d in data:
prov = min([abs(TOTAL - 2 * (d + e)) for e in ens])
if prov == 0: return 0
if prov < res: res = prov
ens |= {d + e for e in ens}
return res
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([10, 10]) == 0, "1st example"
assert checkio([10]) == 10, "2nd example"
assert checkio([5, 8, 13, 27, 14]) == 3, "3rd example"
assert checkio([5, 5, 6, 5]) == 1, "4th example"
assert checkio([12, 30, 30, 32, 42, 49]) == 9, "5th example"
assert checkio([1, 1, 1, 3]) == 0, "6th example"
Dec. 5, 2020
Comments: