Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
n*n/2 solution in Speedy category for Loading Cargo by CDG.Axel
def checkio(data, wl=0, wr=0):
diff = {0}
for e in data:
diff = {abs(i + j * e) for i in diff for j in (-1, 1)}
return min(diff)
# recursive one-liner
# return abs(wl - wr) if not data else min(checkio(data[1:], wl+data[0], wr), checkio(data[1:], wl, wr+data[0]))
"""
# if you don't like recursive one-liner
def checkio(data):
res = []
for i in range(2 ** len(data)):
b = f'{i:0{len(data)}b}'
sl = sum(data[j] for j in range(len(data)) if b[j]=='0')
sr = sum(data[j] for j in range(len(data)) if b[j]!='0')
res.append(abs(sl - sr))
return min(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"
print('ok')
Oct. 25, 2021
Comments: