Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Loading Cargo by lanior
# migrated from python 2.7
def checkio(stones):
s = sum(stones)
result = s
for i in range(1 << len(stones)):
first = 0
for j, weight in enumerate(stones):
if i & (1 << j):
first += weight
result = min(result, abs(s - 2 * first))
return result
if __name__ == '__main__':
assert checkio([10,10]) == 0, 'First, with equal weights'
assert checkio([10]) == 10, 'Second, with a single stone'
assert checkio([5, 8, 13, 27, 14]) == 3, 'Third'
assert checkio([5,5,6,5]) == 1, 'Fourth'
assert checkio([12, 30, 30, 32, 42, 49]) == 9, 'Fifth'
assert checkio([1, 1, 1, 3]) == 0, "Six, don't forget - you can hold different quantity of parts"
print('All is ok')
Nov. 8, 2012
Comments: