|Perl: the Markov chain saw|
Easiest way to brute force generate all combinations of items within a hash?by nat47 (Sexton)
|on Mar 01, 2015 at 21:28 UTC||Need Help??|
nat47 has asked for the wisdom of the Perl Monks concerning the following question:
I've got a hash with a number of items. Each item contains three characteristics: size, level, and ratio. The ratio is the relationship between the size and level. I want to generate all possible combinations of size while < maxSize (so not every item can be used together, they must be less than total size) and add up the total level of the items in each set that is combined.
Afterwards I'm looking for some of the worst and best level combinatinos that are possible to achieve.
I'm sure there must be a fairly quick way to do this in perl, but I'm not experienced enough to know.
If I didn't have these items in a hash I would initially think to write a nested for loop for each item I had, but that can't possibly be the best way to do this, right?
I've heard about some modules that can do Cartesian products... which is sort of what I want... However, I need to keep track of the Sum of a second hash element... And I am not interested in a worst combination being a single element with a low score. The goal is to get as close to the max weight as possible (&have a good level) Example data
(One example ould be that I can take a max weight of 200)