def find_weights(W):
    weights = []
i = 0
while sum(weights) < W:
    weights.append(3 ** i)
i += 1
weights.pop()
weights.append(W - sum(weights))
return weights
print(find_weights(40))
[1, 3, 9, 27]
Solving Puzzle in Python