Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Generalizing Code: Generating Unique Permutations

by ig (Vicar)
on Dec 17, 2008 at 05:34 UTC ( #730835=note: print w/replies, xml ) Need Help??


in reply to Generalizing Code: Generating Unique Permutations

Your various examples and descriptions seem a bit inconsistent to me, so I am probably misunderstanding something. None the less, the following appears to do what your last example does, without the hard coded nested loops. It could easily be modified for different combinations of combinations and variations (ouch!)

update: replaced foreach loop in sub with a map to tidy it up.

use strict; use warnings; use Algorithm::Combinatorics qw(combinations variations_with_repetitio +n); my @data = ('A', 'B', 'C', 'D', 'E', 'F'); my $cardinality = 2; for my $cardinality (1..3) { print "\n\nCardinality: $cardinality\n"; my $dataref = getPairsOfCombinations(\@data, $cardinality); if ($dataref == 0) { die "Bad return\n"; } for (my $i = 0; $i < scalar(@$dataref); $i++) { print "( " . join( ',', map { "( " . join(',', @{$_}) . " )" + } @{ $dataref->[$i] } ) . " )\n"; } } exit(0); sub getPairsOfCombinations { my ($data, $cardinality) = @_; my @combinations = combinations(\@data, $cardinality); my @variations = variations_with_repetition([0..$#combinations], 2 +); return [ map { [ map { $combinations[$_] } @{$_} ] } @variations ] +; }

This produces the following (abridged)

Cardinality: 1 ( ( A ),( A ) ) ( ( A ),( B ) ) ( ( A ),( C ) ) ( ( A ),( D ) ) ( ( A ),( E ) ) ( ( A ),( F ) ) ( ( B ),( A ) ) ( ( B ),( B ) ) ( ( B ),( C ) ) ( ( B ),( D ) ) ( ( B ),( E ) ) ( ( B ),( F ) ) ( ( C ),( A ) ) ( ( C ),( B ) ) ( ( C ),( C ) ) ( ( C ),( D ) ) ( ( C ),( E ) ) ( ( C ),( F ) ) ( ( D ),( A ) ) ( ( D ),( B ) ) ( ( D ),( C ) ) ( ( D ),( D ) ) ( ( D ),( E ) ) ( ( D ),( F ) ) ( ( E ),( A ) ) ( ( E ),( B ) ) ( ( E ),( C ) ) ( ( E ),( D ) ) ( ( E ),( E ) ) ( ( E ),( F ) ) ( ( F ),( A ) ) ( ( F ),( B ) ) ( ( F ),( C ) ) ( ( F ),( D ) ) ( ( F ),( E ) ) ( ( F ),( F ) ) Cardinality: 2 ( ( A,B ),( A,B ) ) ( ( A,B ),( A,C ) ) ( ( A,B ),( A,D ) ) ( ( A,B ),( A,E ) ) ( ( A,B ),( A,F ) ) ( ( A,B ),( B,C ) ) ( ( A,B ),( B,D ) ) ( ( A,B ),( B,E ) ) ( ( A,B ),( B,F ) ) ( ( A,B ),( C,D ) ) ( ( A,B ),( C,E ) ) ( ( A,B ),( C,F ) ) ( ( A,B ),( D,E ) ) ( ( A,B ),( D,F ) ) ( ( A,B ),( E,F ) ) ( ( A,C ),( A,B ) ) ( ( A,C ),( A,C ) ) ( ( A,C ),( A,D ) ) ( ( A,C ),( A,E ) ) ( ( A,C ),( A,F ) ) ( ( A,C ),( B,C ) ) ( ( A,C ),( B,D ) ) ( ( A,C ),( B,E ) ) ( ( A,C ),( B,F ) ) ( ( A,C ),( C,D ) ) ( ( A,C ),( C,E ) ) ( ( A,C ),( C,F ) ) ( ( A,C ),( D,E ) ) ( ( A,C ),( D,F ) ) ( ( A,C ),( E,F ) ) ( ( A,D ),( A,B ) ) ( ( A,D ),( A,C ) ) ( ( A,D ),( A,D ) ) ... Cardinality: 3 ( ( A,B,C ),( A,B,C ) ) ( ( A,B,C ),( A,B,D ) ) ( ( A,B,C ),( A,B,E ) ) ( ( A,B,C ),( A,B,F ) ) ( ( A,B,C ),( A,C,D ) ) ( ( A,B,C ),( A,C,E ) ) ( ( A,B,C ),( A,C,F ) ) ( ( A,B,C ),( A,D,E ) ) ( ( A,B,C ),( A,D,F ) ) ( ( A,B,C ),( A,E,F ) ) ( ( A,B,C ),( B,C,D ) ) ( ( A,B,C ),( B,C,E ) ) ( ( A,B,C ),( B,C,F ) ) ( ( A,B,C ),( B,D,E ) ) ( ( A,B,C ),( B,D,F ) ) ( ( A,B,C ),( B,E,F ) ) ( ( A,B,C ),( C,D,E ) ) ( ( A,B,C ),( C,D,F ) ) ( ( A,B,C ),( C,E,F ) ) ( ( A,B,C ),( D,E,F ) ) ( ( A,B,D ),( A,B,C ) ) ( ( A,B,D ),( A,B,D ) ) ( ( A,B,D ),( A,B,E ) ) ( ( A,B,D ),( A,B,F ) ) ( ( A,B,D ),( A,C,D ) ) ( ( A,B,D ),( A,C,E ) ) ( ( A,B,D ),( A,C,F ) ) ( ( A,B,D ),( A,D,E ) ) ( ( A,B,D ),( A,D,F ) ) ( ( A,B,D ),( A,E,F ) ) ( ( A,B,D ),( B,C,D ) ) ( ( A,B,D ),( B,C,E ) ) ( ( A,B,D ),( B,C,F ) ) ( ( A,B,D ),( B,D,E ) ) ( ( A,B,D ),( B,D,F ) ) ( ( A,B,D ),( B,E,F ) ) ( ( A,B,D ),( C,D,E ) ) ( ( A,B,D ),( C,D,F ) ) ( ( A,B,D ),( C,E,F ) ) ( ( A,B,D ),( D,E,F ) ) ( ( A,B,E ),( A,B,C ) ) ( ( A,B,E ),( A,B,D ) ) ( ( A,B,E ),( A,B,E ) ) ( ( A,B,E ),( A,B,F ) ) ( ( A,B,E ),( A,C,D ) ) ( ( A,B,E ),( A,C,E ) ) ( ( A,B,E ),( A,C,F ) ) ( ( A,B,E ),( A,D,E ) ) ( ( A,B,E ),( A,D,F ) ) ( ( A,B,E ),( A,E,F ) ) ( ( A,B,E ),( B,C,D ) ) ( ( A,B,E ),( B,C,E ) ) ( ( A,B,E ),( B,C,F ) ) ( ( A,B,E ),( B,D,E ) ) ( ( A,B,E ),( B,D,F ) ) ( ( A,B,E ),( B,E,F ) ) ( ( A,B,E ),( C,D,E ) ) ( ( A,B,E ),( C,D,F ) ) ( ( A,B,E ),( C,E,F ) ) ( ( A,B,E ),( D,E,F ) ) ( ( A,B,F ),( A,B,C ) ) ( ( A,B,F ),( A,B,D ) ) ( ( A,B,F ),( A,B,E ) ) ( ( A,B,F ),( A,B,F ) ) ( ( A,B,F ),( A,C,D ) ) ( ( A,B,F ),( A,C,E ) ) ( ( A,B,F ),( A,C,F ) ) ( ( A,B,F ),( A,D,E ) ) ( ( A,B,F ),( A,D,F ) ) ( ( A,B,F ),( A,E,F ) ) ( ( A,B,F ),( B,C,D ) ) ( ( A,B,F ),( B,C,E ) ) ( ( A,B,F ),( B,C,F ) ) ( ( A,B,F ),( B,D,E ) ) ( ( A,B,F ),( B,D,F ) ) ( ( A,B,F ),( B,E,F ) ) ( ( A,B,F ),( C,D,E ) ) ( ( A,B,F ),( C,D,F ) ) ( ( A,B,F ),( C,E,F ) ) ( ( A,B,F ),( D,E,F ) ) ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730835]
help
Chatterbox?
[oiskuu]: Someone needs to re-read How do I use the power of consideration responsibly? carefully. Consideration is not intended for stamping out humor, AFAIK.
[oiskuu]: E.g. Re^3: Big thank you to the Perl community. is surely dry humor (not mine). Don't they teach humor in german schools!?

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-04-27 19:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (512 votes). Check out past polls.