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".
-
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.