Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

permutations of array

by NodeReaper (Curate)
on Feb 22, 2002 at 02:48 UTC ( [id://146866]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: permutations of array
by data64 (Chaplain) on Feb 22, 2002 at 02:58 UTC
Re: permutations of array
by jynx (Priest) on Feb 22, 2002 at 05:28 UTC

    a couple of things,

    The major problem with the code you have there is that it doesn't give combinations of separated list items, it only gives items from one point forward. As an example:
    for n=3, with items 1,2, and 3, i get the following from your code:
    1
    1 2
    1 2 3
    2
    2 3
    3
    Note the missing '1 3' case. For the items 1,2,3, and 4, '134', '124', '13', '14', etc would be missing.

    Merlyn's snippets are a nice and easy way to go; a simple transformation and they will return an array of strings instead of an AoA. As an exercise, i chalked up a non-recursive solution as well; it returns an array of arrays as merlyn's does.

    sub combinations { my (@comb, $pos); my $limit = 2**@_; while (my $item = shift) { my $step = 2**@_; for ($pos = 0; $pos < $limit; $pos += $step) { push @{$comb[$pos++]}, $item for 1..$step; } } return @comb, []; }
    jynx
Re: permutations of array
by virtualsue (Vicar) on Feb 22, 2002 at 13:24 UTC
    There was nothing particularly wrong with this question. Why was it reaped? Even if it was homework, asking for help on homework isn't wrong. THAT'S HOW PEOPLE LEARN!
      I usually don't reply just to concur, but I think it's important here. The whole homework-- thing is completely out of control. Copy-n-paste homework questions are bead because someone is trying to get out of the work. lbcarey described the problem and showed what they have already tried. There's an honest attempt to solve the problem and they're looking for HELP. Not for someone to do it for them.

      How is this any different than someone asking for help with a work project??? Not so long ago there was a discussion about how perlmonks wasn't for people starting to learn Perl. Wonder how it got that reputation? There was much discussion about how to shed that reputation.. this isn't doing it.

      Rich

Re: permutations of array
by lbcarey (Initiate) on Feb 22, 2002 at 04:08 UTC
    oops, spoke too soon. the cpan module and sample code generate permutations, I need combinations. I don't care about the order of the array elements.
    @array=qw(big bad prog) --->
    big
    bad
    prog
    big bad
    big prog
    big bad prog
      #Yet another way to do it
      sub combinations{ return map{my $i=$_<<1;[grep{($i>>=1)&1}@_]}0..(1<<@_)-1 }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2025-03-19 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (59 votes). Check out past polls.