laziness, impatience, and hubris PerlMonks

### How do I get all n element subsets of an array

by NodeReaper (Curate)
 on Jan 18, 2002 at 04:06 UTC 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

• 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.

Create A New User
Node Status?
node history
Node Type: perlquestion [id://139677]
help
Chatterbox?
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-03-22 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (280 votes). Check out past polls.

Notices?