gradual.pl

#!/usr/bin/perl 

# degree or gradual suite of a permutation
# emilbarton 2013

## modules:
######################################################################
use strict;
use warnings FATAL => "all";

## args and globals
######################################################################
if ((@ARGV)== 0 || $ARGV[0] =~ /help|usage/){ &Usage() };
my $TARGET = $ARGV[0]; my @TARGET = split //, $TARGET;
my $PERMUTATION = $TARGET; my @PERMUTATIONS; 
my $DEG = 1; my $LEN = (@TARGET); my $MODE;
if ($ARGV[1]) { $MODE = $ARGV[1] }
else { $MODE = 0 }
my $SIGNS; my $SEEN = "";
my $INDEX = "0123456789ABCDEFGHIJKLMN";
my @INDEX = split //,$INDEX;

## permutation check:
######################################################################
die "invalid length" if ($LEN > (@INDEX));
for (my $i = 0; $i < $LEN; $i++) { $SIGNS = $SIGNS.$INDEX[$i] }  
foreach (@TARGET) {
	my $char = $_; 
	die "invalid sign: $char" if ($SIGNS !~ /$char/i);
	die "repeated signs not allowed" if ($SEEN =~ /$char/i);
	$SEEN .= $char;
}

## gradual:
######################################################################
push @PERMUTATIONS, $TARGET;
goto THE_END if ($INDEX =~ /$TARGET/);
while (++$DEG){
  $TARGET = &Composind($TARGET,$PERMUTATION);
  push @PERMUTATIONS, $TARGET ;
  last if ($INDEX =~ /$TARGET/);
}
THE_END:
if ($MODE == 1) { print "@PERMUTATIONS"."\n" }
else { print $DEG."\n" }

## subroutines
######################################################################

### compose:
sub Composind { 
	my ($t,$g) = @_;
  my @target = split //,$t; 
  my @agent = split //,$g;
  my (@agent_by_char, $cnt, $result, @tmp_result, @target_by_char);
  for (my $i = 0; $i < (@agent); $i++) { # We create char-indice pairs.
    $target[$i] = [$target[$i],$i]; $agent[$i] = [$agent[$i],$i];
  }
  @target_by_char = sort {@$a[0] cmp @$b[0]} @target; # We order pairs according to the char.	
  @agent_by_char = sort {${$a}[0] cmp ${$b}[0]} @agent;
  @tmp_result = map { [ @$_ ] } @target_by_char; # Bait modifying sequence: note the indispensable call to map().
  for (my $i = 0; $i < (@agent_by_char); $i++){ ${$tmp_result[$i]}[1] = ${$target_by_char[${$agent_by_char[$i]}[1]]}[1] }
  foreach (sort {${$a}[1] <=> ${$b}[1]} @tmp_result) { $result .= ${$_}[0] }
  return $result;
} ## END Composind().


### usage:
sub Usage {
	my $data;
	while (<DATA>) { $data .= $_ };
	die "Usage:\n$data";
} ## END Usage()

__DATA__

This program can be used to compute a gradual suite of permutations or the 
number of items in this suite which we call "degree" of a permutation. 

Try:

  ./gradual permutation [mode]

  Examples:
  
  ./gradual.pl 73206514
  6
  
  ./gradual.pl 73206514 1
  73206514 40271536 67243501 14260573 36217540 01234567
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s