Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Loading Cargo by James
import itertools
def checkio(stones):
'''
minimal possible weight difference between stone piles
'''
return min(abs(sum(comb[n] * w for n, w in enumerate(stones)))
for comb in itertools.product(*([-1,1] for i in range(len(stones)))))
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')
Oct. 29, 2012
Comments: