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

(tye)Re: Combinatorics

by tye (Sage)
on Aug 22, 2002 at 03:10 UTC ( [id://191912]=note: print w/replies, xml ) Need Help??


in reply to Combinatorics

sub genFixedSubsets { my( $size, @set )= @_; my @idx= reverse 0..$size-1; return sub { return if $size < @idx; my @ret= @set[@idx]; my $i= 0; $i++ until ++$idx[$i] < @set-$i || $size < $i; $idx[$i]= 1+$idx[1+$i] while 0 <= --$i; return @ret; }; } my $gen= genFixedSubsets( $ARGV[0] || 3, 1..($ARGV[1]||5) ); my @subset; while( @subset= $gen->() ) { print "@subset\n"; }

For example:

$ subsets 3 5 3 2 1 4 2 1 5 2 1 4 3 1 5 3 1 5 4 1 4 3 2 5 3 2 5 4 2 5 4 3
        - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

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

    No recent polls found