Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
All,
Assume you have a variable number of groups. Each group has a variable number of members. Your task is to create all possible combinations of a fixed size such that no combination includes multiple members from the same group.

Ensuring that you don't get multiple member from the same group is easy - just used Algorithm::Loops NestedLoops. Getting all possible combinations of a fixed size is easy, use an iterator such as mine. The problem with this approach is that it generates duplicates.

for my $first (@group1) { for my $second (@group2) { for my $third (@group3) { for my $fourth (@group4) { # Assume fixed size = 2 # $first, $second and $third repeat here for each memb +er in @group4 # $first, $second pairings happen multiple times } } } }

Using a %seen hash won't work for two reasons. First, you will likely run out of memory given how quickly the number of possible results can grow with just a small input size. Second, generating a combination just to skip is bad for performance of a program that will already likely not finish before the heat death of the universe.

Your challenge then is to develop an algorithm that can iterate over the possible solutions without generating duplicates. Keep in mind that the following things are variable (number of groups, number of members in each group, the fixed size of each combination).

A sample to work with: group_1 = A B C group_2 = 1 2 3 4 group_3 = yellow blue green group_4 = tiny small medium large gigantic fixed_size = 2

Cheers - L~R


In reply to Challenge: Generate fixed size combination across groups without duplicates by Limbic~Region

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-28 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found