Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Combinations/Perl5i/combinations.p5i
Normal file
22
Task/Combinations/Perl5i/combinations.p5i
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use perl5i::2;
|
||||
|
||||
# ----------------------------------------
|
||||
# generate combinations of length $n consisting of characters
|
||||
# from the sorted set @set, using each character once in a
|
||||
# combination, with sorted strings in sorted order.
|
||||
#
|
||||
# Returns a list of array references, each containing one combination.
|
||||
#
|
||||
func combine($n, @set) {
|
||||
return unless @set;
|
||||
return map { [ $_ ] } @set if $n == 1;
|
||||
|
||||
my ($head) = shift @set;
|
||||
my @result = combine( $n-1, @set );
|
||||
for my $subarray ( @result ) {
|
||||
$subarray->unshift( $head );
|
||||
}
|
||||
return ( @result, combine( $n, @set ) );
|
||||
}
|
||||
|
||||
say @$_ for combine( 3, ('a'..'e') );
|
||||
Loading…
Add table
Add a link
Reference in a new issue