How to resolve the algorithm Permutations by swapping step by step in the Perl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Permutations by swapping step by step in the Perl programming language
Table of Contents
Problem Statement
Generate permutations of n items in which successive permutations differ from each other by the swapping of any two items. Also generate the sign of the permutation which is +1 when the permutation is generated from an even number of swaps from the initial state, and -1 for odd. Show the permutations and signs of three items, in order of generation here. Such data are of use in generating the determinant of a square matrix and any functions created should bear this in mind. Note: The Steinhaus–Johnson–Trotter algorithm generates successive permutations where adjacent items are swapped, but from this discussion adjacency is not a requirement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Permutations by swapping step by step in the Perl programming language
Source code in the perl programming language
use strict;
use warnings;
# This code uses "Even's Speedup," as described on
# the Wikipedia page about the Steinhaus–Johnson–
# Trotter algorithm.
# Any resemblance between this code and the Python
# code elsewhere on the page is purely a coincidence,
# caused by them both implementing the same algorithm.
# The code was written to be read relatively easily
# while demonstrating some common perl idioms.
sub perms :prototype(&@) {
my $callback = shift;
my @perm = map [$_, -1], @_;
$perm[0][1] = 0;
my $sign = 1;
while( ) {
$callback->($sign, map $_->[0], @perm);
$sign *= -1;
my ($chosen, $index) = (-1, -1);
for my $i ( 0 .. $#perm ) {
($chosen, $index) = ($perm[$i][0], $i)
if $perm[$i][1] and $perm[$i][0] > $chosen;
}
return if $index == -1;
my $direction = $perm[$index][1];
my $next = $index + $direction;
@perm[ $index, $next ] = @perm[ $next, $index ];
if( $next <= 0 or $next >= $#perm ) {
$perm[$next][1] = 0;
} elsif( $perm[$next + $direction][0] > $chosen ) {
$perm[$next][1] = 0;
}
for my $i ( 0 .. $next - 1 ) {
$perm[$i][1] = +1 if $perm[$i][0] > $chosen;
}
for my $i ( $next + 1 .. $#perm ) {
$perm[$i][1] = -1 if $perm[$i][0] > $chosen;
}
}
}
my $n = shift(@ARGV) || 4;
perms {
my ($sign, @perm) = @_;
print "[", join(", ", @perm), "]";
print $sign < 0 ? " => -1\n" : " => +1\n";
} 1 .. $n;
#!perl
use strict;
use warnings;
sub perms {
my ($xx) = (shift);
my @perms = ([+1]);
for my $x ( 1 .. $xx ) {
my $sign = -1;
@perms = map {
my ($s, @p) = @$_;
map [$sign *= -1, @p[0..$_-1], $x, @p[$_..$#p]],
$s < 0 ? 0 .. @p : reverse 0 .. @p;
} @perms;
}
@perms;
}
my $n = shift() || 4;
for( perms($n) ) {
my $s = shift @$_;
$s = '+1' if $s > 0;
print "[", join(", ", @$_), "] => $s\n";
}
You may also check:How to resolve the algorithm Run-length encoding step by step in the TMG programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the Rust programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Tau function step by step in the Wren programming language