Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Loading Cargo by old_user
# migrated from python 2.7
from itertools import combinations
def checkio(stones):
'''
minimal possible weight difference between stone piles
'''
weight = sum(stones)
min_diff = weight
for i in range(1, (len(stones)//2 + 2)):
for var in combinations(stones, i):
min_diff = min(min_diff, abs(weight - 2 * sum(var)))
return min_diff
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'
print('All is ok')
Jan. 19, 2012
Comments: