Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

permutations of all number in a given set

by fadingjava (Acolyte)
on Apr 22, 2004 at 17:01 UTC ( [id://347413]=perlquestion: print w/replies, xml ) Need Help??

fadingjava has asked for the wisdom of the Perl Monks concerning the following question:

greetings monks, need help with a very frustrating problem. i have an array of arrays each element of which contains a different number of elements, ranging from 2 to 5.
say @AoA = ([0,1] ,[8,9,10,11])
now for each of these arrays i want to generate the permutation of all the elements of that array taken 2 at a time. like
0,1 1,0 8,9 9,8 8,10 10,8 8,11 11,8
and so on... cant figure out how to do this? if somebody has written something like this or has any suggestions on possible approach, please help

Replies are listed 'Best First'.
Re: permutations of all number in a given set
by kvale (Monsignor) on Apr 22, 2004 at 18:17 UTC
    Both DamnDirtyApe and Anonymous Monk gave clever, general answers. Here is a simple approach:
    my @AoA = ([0,1] ,[8,9,10,11]); foreach my $arr (@AoA) { foreach my $i (0..@$arr-1) { foreach my $j (0..@$arr-1) { next if $i == $j; print "$arr->[$i],$arr->[$j]\n"; } } }

    -Mark

Re: permutations of all number in a given set
by DamnDirtyApe (Curate) on Apr 22, 2004 at 17:21 UTC

    Take a look at Re: Combinatorics; you could adapt this strategy to your problem.


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: permutations of all number in a given set
by Anomynous Monk (Scribe) on Apr 22, 2004 at 17:50 UTC
    use warnings; use strict; my @aoa = ([1..3],["x".."y"],["a".."c"]); my@perms=map{my$a=$_;map{my$i=$_;map[@$a[$i,$_]],0..$i-1,$i+1..$#$a}0. +.$#$a}@aoa; use Data::Dumper; $Data::Dumper::Indent = 0; print "$_\n" for Dumper @perms; __END__ $VAR1 = [1,2]; $VAR2 = [1,3]; $VAR3 = [2,1]; $VAR4 = [2,3]; $VAR5 = [3,1]; $VAR6 = [3,2]; $VAR7 = ['x','y']; $VAR8 = ['y','x']; $VAR9 = ['a','b']; $VAR10 = ['a','c']; $VAR11 = ['b','a']; $VAR12 = ['b','c']; $VAR13 = ['c','a']; $VAR14 = ['c','b'];
Re: permutations of all number in a given set
by ysth (Canon) on Apr 22, 2004 at 17:24 UTC
    The actual question you ask doesn't seem to have anything to do with your having arrays of arrays; maybe you'd find it easier to think of how to go about it by solving the problem for a simple array and then extending it to your array of arrays.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://347413]
Approved by sunadmn
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found