Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
Just another Perl shrine
 
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 avoiding work at the Monastery: (11)
As of 2014-04-21 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls