3 ms·
Yep, except this is so small, you can look at all combinations. Something like this would do it (without replacement). from itertools import combinations
by IncRnd 5y ago
Yep, except this is so small, you can look at all combinations.
Something like this would do it (without replacement).
from itertools import combinations
arr = [
("yoyo",1.22),("doll",2.75),("duckie",1.85),("tractor",5.97),("airplaine",6.47),
("ball",2.16),("racecar",7.13),("dog",4.57),("jumprope",1.46),("car",5.18),
("elephant",3.16),("bear",4.89),("xylophone",7.11),("tank",6.45),("checkers",4.77),
("boat",8.04),("train",6.71),("jacks",2.31),("truck",6.21),("whistle",0.98) ("pinwheel",0.87)
]
for r in range(1,len(arr)+1):
for thisset in list(combinations(arr, r)):
if 43.94==sum([tup[1] for tup in thisset]):
print ([tup[0] for tup in thisset])
- mkl 5y agoYou're missing a comma before the pinwheel one, but otherwise this finds the same 686 solutions as mine.
- deleted 5y ago[deleted]
- IncRnd 5y agoYou're correct... I must have made a mistake when pasting it into the HN edit box, since I performed slight edits to make it not jam into a single line. Thanks!