Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Update: My sets can overlap too. For exmaple I may have 4 sets like '0..7','1-6', '6-12',and '3..9'.

You don't say how many from each of those four sets, so I did 2 from each, but it should be obvious where to change the numbers supplied to permuteSets for different requirements.

The following runs in 1.7MB and takes a few seconds for wc -l to count the results:

#! perl -slw use strict; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CO +DE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } sub permuteSets (&@) { my $code = shift; my @subsets; while( @_ ) { my( $n, $set ) = ( shift, shift ); push @subsets, []; nFor { push @{ $subsets[-1] }, [ @{ $set }[ @_ ] ] } (scalar @ +{ $set } ) x $n; } nFor{ $code->( map{ @{ $subsets[ $_ ][ $_[ $_ ] ] } } 0 .. $#subsets + ); } map scalar @$_, @subsets; } permuteSets{ print join '-', @_; } 2, [0..7], 2, [1..6], 2, [6..12], 2, [3..9]; __END__ C:\test>965138 | wc -l 5531908 C:\test>965138 0-0-1-1-6-6-3-3 0-0-1-1-6-6-3-4 0-0-1-1-6-6-3-5 0-0-1-1-6-6-3-6 0-0-1-1-6-6-3-7 0-0-1-1-6-6-3-8 0-0-1-1-6-6-3-9 0-0-1-1-6-6-4-3 0-0-1-1-6-6-4-4 0-0-1-1-6-6-4-5 0-0-1-1-6-6-4-6 0-0-1-1-6-6-4-7 0-0-1-1-6-6-4-8 0-0-1-1-6-6-4-9 0-0-1-1-6-6-5-3 ... 7-7-6-6-12-12-9-6 7-7-6-6-12-12-9-7 7-7-6-6-12-12-9-8 7-7-6-6-12-12-9-9
I also need to eliminate duplicate items. In the case of overlapping sets, every resulting row should satisfy the initial condition.

I don't suppose you'd care to describe the application?

AFAIK, there in no combinatorics algorithm that would address those requirements, so it is going to be a post-production step and is left as a exercise.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re: Multiple Combinatorics by BrowserUk
in thread Multiple Combinatorics by aartist

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (12)
    As of 2014-10-23 18:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (126 votes), past polls