I have simulated balancing a scale by having each side total the same weight.
The scale has two weights (left and right side) and some spare weights:
weights = [3, 4] spare_weights = [1, 2, 7, 7]
A maximum of two weights can be used from the spare weights to balance the scale. The spare(s) may be on a single side or split between both.
The output should be the minimum number of weight needed to balance the scale.
import itertools weights = [3, 4] spare_weights = [1, 2, 7, 7] c = enumerate([subset for l in range(0, 3) for subset in itertools.combinations(spare_weights, l)]) cc = cc = list(itertools.combinations(c, 2)) left, right = weights qualifying_combos =  for c in cc: l, r = c if sum(l) + left == sum(r) + right: qualifying_combos.append(list(l) + list(r)) elif sum(l) + right == sum(r) + left: qualifying_combos.append(list(l) + list(r)) print(min(qualifying_combos, key=len))
Is there a more elegant way to code this?