Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Combinatorics

by Aristotle (Chancellor)
on Aug 22, 2002 at 03:23 UTC ( [id://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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-19 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found