octothorpe has asked for the wisdom of the Perl Monks concerning the following question:
sub combinations { my @array = @_; if ($#array == 0) {return [ $array[0] ]; } my @results; my $element; foreach $element (0..$#array) { my @leftovers = @array; my $chosen_one = splice(@leftovers, $element, 1); foreach (&combinations(@leftovers)) { push(@results, [ $chosen_one, @{$_} ]); } } return @results; } use Data::Dumper; print Dumper(&combinations(qw(a b c d)));
|
---|
Replies are listed 'Best First'. | |
---|---|
(tye)Re: Permutations
by tye (Sage) on Mar 22, 2001 at 03:54 UTC | |
Re: Permutations
by indapa (Monk) on Mar 22, 2001 at 06:21 UTC | |
Re: Permutations
by dvergin (Monsignor) on Mar 22, 2001 at 03:37 UTC | |
by tye (Sage) on Mar 22, 2001 at 09:04 UTC |