first commit
This commit is contained in:
29
perl/Examples/Chap4/permute-flop
Normal file
29
perl/Examples/Chap4/permute-flop
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
|
||||
###
|
||||
### permute-flop
|
||||
###
|
||||
|
||||
## Chapter 4 section 3.1
|
||||
|
||||
sub permute {
|
||||
my @items = @_;
|
||||
my $n = 0;
|
||||
return Iterator {
|
||||
$n++, return @items if $n==0;
|
||||
my $i;
|
||||
my $p = $n;
|
||||
for ($i=1; $i<=@items && $p%$i==0; $i++) {
|
||||
$p /= $i;
|
||||
}
|
||||
my $d = $p % $i;
|
||||
my $j = @items - $i;
|
||||
return if $j < 0;
|
||||
|
||||
@items[$j+1..$#items] = reverse @items[$j+1..$#items];
|
||||
@items[$j,$j+$d] = @items[$j+$d,$j];
|
||||
|
||||
$n++;
|
||||
return @items;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user