Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You may be able to simply brute force all options.</p> <p>Here is some example Python code:</p> <pre><code>from itertools import combinations F=['apple','banana','clementine'] T=['compass','telephone'] C=['socks','gloves','hat'] weights={'apple':1,'banana':2,'clementine':3,'compass':4,'telephone':5, 'socks':6,'gloves':7,'hat':8} values={'apple':10,'banana':9,'clementine':8,'compass':7,'telephone':6, 'socks':5,'gloves':4,'hat':3} choices=[] W=45 # Maximum allowed weight n=5 # Number of choices to display for M in range(3): # M represents which category gets an extra item num_food = 3 if M==0 else 2 num_toys = 2 if M==1 else 1 num_clothes = 3 if M==2 else 2 for food_to_take in combinations(F,num_food): for toys_to_take in combinations(T,num_toys): for clothes_to_take in combinations(C,num_clothes): things_to_take = food_to_take+toys_to_take+clothes_to_take weight = sum(weights[a] for a in things_to_take) value = sum(values[a] for a in things_to_take) if weight&lt;=W: choices.append([value,weight,things_to_take]) for value,weight,things_to_take in sorted(choices,reverse=True)[:n]: print value,weight,things_to_take </code></pre> <p>prints</p> <pre><code>43 23 ('apple', 'banana', 'clementine', 'compass', 'socks', 'gloves') 42 24 ('apple', 'banana', 'clementine', 'telephone', 'socks', 'gloves') 42 24 ('apple', 'banana', 'clementine', 'compass', 'socks', 'hat') 41 25 ('apple', 'banana', 'compass', 'telephone', 'socks', 'gloves') 41 25 ('apple', 'banana', 'clementine', 'telephone', 'socks', 'hat') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload