Guest User

Untitled

a guest
Jul 21st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.44 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use feature qw(say);
  4.  
  5. ##
  6. # Turns an arbitrary k-cycle into product of transpositions
  7. # param: a single k-cycle @cycle
  8. # returns: a product of transpositions @prod
  9. ##
  10. sub cycle_to_trans {
  11.    
  12.     my @cycle = @_;
  13.     my @prod = ();
  14.    
  15.     # debugging
  16.     say "CYCLE := (",@cycle,")";
  17.    
  18.     # length of the cycle
  19.     my $n = $#cycle;
  20.    
  21.     # (a_{1},a_{2},...,a_{n}) ==
  22.     # [(a_{1},a_{n}), (a_{1},a_{n-1}), ..., (a_{1},a_{3}), (a_{1},a_{2})]
  23.     for my $i ( 0 .. $n-1 ) {
  24.     my @trans = ($cycle[0],$cycle[$n-$i]);
  25.     push @prod, @trans;
  26.     say "(",@trans,")";
  27.     }
  28.     # return the list of transpositions for this cycle
  29.     return @prod;
  30. }
  31.  
  32. ##
  33. # Turns a given permutation into a product of transpositions
  34. # param: a permutation @perm
  35. # returns: a product of transpositions @prod
  36. ##
  37. sub perm_to_trans {
  38.  
  39.     my @perm = @_;
  40.     my @prod = ();
  41.    
  42.     # for each cycle in trans, turn it to a transposition
  43.     for my $aref ( @perm ) {
  44.     my @trans = cycle_to_trans(@$aref);
  45.     push @prod, @trans;
  46.     }
  47.     # return the list of transpositons for this permutation
  48.     return @prod;
  49. }
  50.  
  51. ##
  52. # The action of a transposition on a set
  53. # param: a transposition @trans, a set to permute @set
  54. # returns: the permuted set @res
  55. ##
  56. sub swap {
  57.     my @trans = @{ $_[0] };
  58.     my @set = @{ $_[1] };
  59.  
  60.     my @res = ();
  61.    
  62.     my $a = @trans[0];
  63.     my $b = @trans[1];
  64.    
  65.     # for each letter in @set
  66.     for my $i ( 0 .. $#set ) {
  67.     # if this letter is $a, swap it to $b
  68.     if ( @set[$i] == $a ) {
  69.         push @res, $b;
  70.     }
  71.     # if this letter is $b, swap it to $a
  72.     elsif ( @set[$i] == $b ) {
  73.         push @res, $a;
  74.     }
  75.     # otherwise, this element is fixed
  76.     else {
  77.         push @res, @set[$i];
  78.     }
  79.     }
  80.     # debugging
  81.     say @res;
  82.     # return the permuted set
  83.     return @res;
  84. }
  85.  
  86. ##
  87. # A controller for testing.
  88. ##
  89. sub test {
  90.     # the permutation to use
  91.     my @perm = ([1,5,4,2]);
  92.     # the set to permute
  93.     my @set = (1,2,3,4,5,6);
  94.     # the permutation as a product of transpositions
  95.     my @prod = perm_to_trans(@perm);
  96.     # a list to track how set permutes after each transposition
  97.     my @list = ( [ @set ] );
  98.  
  99.     ## ARRAY REF PROBLEMS HERE!
  100.     # apply each transposition to @set
  101.     for my $i ( 0 .. $#prod-1) {
  102.     my @tmpset = @list[$i];
  103.     my @trans = ();
  104.     push @trans,@prod[$i];
  105.     push @trans,@prod[$i+1];
  106.     my @newset = swap(@trans,@tmpset);
  107.     say @newset;
  108.     push @list,[ @newset ];
  109.     }
  110. }
  111.  
  112. # run the test
  113. test();
  114.  
  115. ## TODO ##
  116. # check that cycle does not contain "illegal" characters
  117. # param: the cycle to check
  118. # returns: $flag
  119. ## TODO ##
  120. sub is_valid_cycle {
  121.     my @cycle = @_;
  122.     my $flag = 0;
  123.     return $flag;
  124. }
  125.  
  126. ## TODO ##
  127. # check that each element in product is a transposition
  128. # param: a product of cycles to check
  129. # returns: $flag
  130. ## TODO ##
  131. sub is_prod_trans {
  132.     my $flag = 0;
  133.     return $flag;
  134. }
  135.  
  136. ## TODO ##
  137. # checks if a permutation is a product of disjoint cycles
  138. # param: the permutation
  139. # returns: $flag
  140. ## TODO ##
  141. sub is_disjoint {
  142.     my $flag = 0;
  143.     return $flag;
  144. }
  145.  
  146. ## TODO ##
  147. # checks if two permutations are equal
  148. # param: permutations to check
  149. # returns: $flag
  150. ## TODO ##
  151. sub check_perms {
  152.     my $flag = 0;
  153.     return $flag;
  154. }
  155.  
  156. ## TODO ##
  157. # main checker
  158. # param: @correct_answer, @student_answer, @set, $check_disjoint, $check_trans
  159. # returns: $flag
  160. ## TODO ##
  161. sub checker {
  162.     my $flag = 0;
  163.     return $flag;
  164. }
  165.  
  166. #EOF
Add Comment
Please, Sign In to add comment