http://www.perlmonks.org?node_id=139677

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

  • Comment on How do I get all n element subsets of an array

Replies are listed 'Best First'.
Re: How do I get all n element subsets of an array
by I0 (Priest) on Jan 18, 2002 at 04:35 UTC
    my @x = qw(a b c d e f); my $n = 4; my $x = (1<<$n)-1; while( $x < 1<<@x ){ my $i = $x<<1; print join", ",(grep{($i>>=1)&1}@x),"\n"; $i = $x&(~$x>>1); $i &= -$i; $x += $i--; if( $i &= $x ){ $x -= $i; $i /= ($i&-$i); $x += $i; } }
Re: How do I get all n element subsets of an array
by FoxtrotUniform (Prior) on Jan 18, 2002 at 04:24 UTC
Re: How do I get all n element subsets of an array
by ehdonhon (Curate) on Jan 18, 2002 at 04:37 UTC

    Hrm, I dunno. What does the answer in the back of the book say?

Re: How do I get all n element subsets of an array
by coolmichael (Deacon) on Jan 18, 2002 at 12:32 UTC
    This was homework for me, almost verbatim, in first year computer science. It is fairly simple code, but I will warn you that it can take a very long time to calculate. Try to do it yourself, and show your results. I'll be happy to help once you've tried to do it yourself.

    good luck.