We don't bite newbies here... much PerlMonks

### Re: Multiple Combinatorics

by BrowserUk (Pope)
 on Apr 15, 2012 at 11:14 UTC ( #965146=note: 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?

Comment on Re: Multiple Combinatorics

Create A New User
Node Status?
node history
Node Type: note [id://965146]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2015-08-03 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?