Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
using recursion and a helper function solution in Clear category for Loading Cargo by info.leadx
def helper(data, a=[], b=[]):
if not data:
return abs(sum(a) - sum(b))
else:
return min(helper(data[1:], a+[data[0]], b),
helper(data[1:], a, b+[data[0]]))
def checkio(data):
return helper(data)
#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"
May 12, 2020