Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Combinatorics

by Aristotle (Chancellor)
on Aug 22, 2002 at 03:23 UTC ( #191916=note: print w/ replies, xml ) Need Help??


in reply to Combinatorics

#!/usr/bin/perl -w use strict; use Data::Dumper; sub unshift_many { my $scalar = shift; unshift @$_, $scalar for @_; @_; } sub combinations { my ( $array, $len, $start ) = @_; $start ||= 0; return unless $len > 0; return $len == 1 ? map [ $_ ], @{ $array }[ $start .. $#$array ] : map unshift_many( $array->[$_], combinations( $array, $len-1 +, $_+1 ) ), $start .. $#$array; } $Data::Dumper::Indent=0; print Dumper( [ combinations [ qw( 1 2 3 4 5 ) ], 3 ] ), "\n";

Makeshifts last the longest.


Comment on Re: Combinatorics
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2015-07-06 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls