Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Batteries, dynamic solution in Uncategorized category for Loading Cargo by ashkop
import copy
def checkio(stones):
'''
minimal possible weight difference between stone piles
'''
total = sum(stones)
weights = list()
for i in range(total // 2):
weights.append(set())
for j, stone in enumerate(stones):
if stone == (i + 1):
weights[i].add(j)
break
elif i >= stone and weights[i - stone] and j not in weights[i-stone]:
weights[i] = copy.copy(weights[i-stone])
weights[i].add(j)
break
for i, w in enumerate(reversed(weights)):
if w:
return total % 2 + i * 2
return stones[0]
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. 23, 2012