Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I get all n element subsets of an array

by NodeReaper (Curate)
on Jan 18, 2002 at 04:06 UTC ( #139677=perlquestion: print w/ replies, xml ) Need Help??
NodeReaper has asked for the wisdom of the Perl Monks concerning the following question:


Reason: (FoxtrotUniform) edit -- needs code tags for legibility. Vote 'delete' if you think this is h/w; I'm unsure

For more information on this node visit: this

Comment on How do I get all n element subsets of an array
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 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 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-10-25 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (138 votes), past polls