Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

my %list = ( 'player1' => {rating=> 10,weight=> 60, ratio=> .166}, 'player2' => {rating=> 12, weight=> 100, ratio=> .12}, 'player3' => {rating=> 4,weight=> 23, ratio=> .173913043}, 'player3' => {rating=> 2,weight=> 142, ratio=>}, 'player4' => {rating=> 3, weight=> 119,ratio=>}, 'player5' => {rating=> 1, weight=> 14, ratio=>}, );
(One example ould be that I can take a max weight of 200)

In reply to Easiest way to brute force generate all combinations of items within a hash? by nat47

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (5)
    As of 2020-07-07 16:12 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found