Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Loading Cargo by oduvan
def one_hand_weights(items, init=0):
if items:
head, tail = items[0], items[1:]
yield from one_hand_weights(tail, init)
yield from one_hand_weights(tail, init + head)
else:
yield init
def checkio(items):
'''
minimal possible weight difference between stone piles
'''
overal = sum(items)
return min(map(lambda x: abs(overal - x - x), one_hand_weights(items)))
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'
print ('All is ok')
Oct. 28, 2012
Comments: