Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Loading Cargo by erdavila
# migrated from python 2.7
def checkio(stones, left=0, right=0):
if stones:
first = stones[0]
rest = stones[1:]
result1 = checkio(rest, left + first, right)
result2 = checkio(rest, left, right + first)
if result1 < result2:
result = result1
else:
result = result2
else:
result = abs(left - right)
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')
April 19, 2013
Comments: