Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
solution in two lines solution in Clear category for Loading Cargo by dan_s
"""
I use the following algorithm.
x: the weights of one set
y: the weights of other set
S: the weights of all things
x + y = S => y = S - x
MIN = abs(x - y) = MIN(x - S + x) = MIN(2 * x - S)
We should get all combinations for X and Y but X and Y must be unique.
To get all combinations for X I use
combinations(data, i) => combo from one, two and len(data) // 2 things in one hand.
"""
from itertools import combinations, chain
def checkio(data):
temp = list(chain.from_iterable([combinations(data, i) for i in range(1, len(data) // 2 + 1)]))
return min(abs(2 * sum(i) - sum(data)) for i in temp) if temp else data[0]
#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"
July 21, 2020