Three friends Ade, Ben and Cade have 9 marbles in front of them. Their weights are as follows:
154, 16, 19, 101, 10, 17, 13, 46 and 22 grams.
Each of the three friends takes 3 marbles. Ade's bunch weighs precisely twice as much as Bill's bunch.
What is the total weight in Cade's bunch?
The program finds only one solution whose output is:
Ade, Ben, Cade are [16, 46, 22] [19, 10, 13] [154, 101, 17]
and their sums are 84 42 272
Python program follows:
from itertools import combinations
marbles = [154, 16, 19, 101, 10, 17, 13, 46, 22]
for ade in combinations(marbles,3):
copy = marbles[:]
for elem in ade:
copy.remove(elem)
for bill in combinations(copy,3):
if 2*sum(bill) != sum(ade):
continue
copy2 = copy[:]
for elem2 in bill:
copy2.remove(elem2)
cade = copy2 # because other marbles removed
print ('Ade, Ben, Cade are ', list(ade),list(bill),list(cade))
print ('and their sums are ', sum(ade),sum(bill),sum(cade))
Edited on November 8, 2020, 10:35 am
|
Posted by Larry
on 2020-11-08 10:34:27 |