Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
brute force solution in Clear category for Loading Cargo by pokryshkin
from itertools import combinations, permutations
def checkio(data):
delta = 1000
perm = permutations(data,len(data))
for distribution in perm:
for i in range(len((distribution))):
left = sum(distribution[:i])
right = sum(distribution[i:])
if delta > abs(left - right): delta = abs(left - right)
if delta == 0: break
return delta
#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"
April 19, 2021
Comments: