Skip to content

Instantly share code, notes, and snippets.

@JialunC
Created June 28, 2020 20:57
Show Gist options
  • Save JialunC/5a8a6001452b6e17b586ef0833f08efb to your computer and use it in GitHub Desktop.
Save JialunC/5a8a6001452b6e17b586ef0833f08efb to your computer and use it in GitHub Desktop.
permutations
/**
* Write a description of class Permutations here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Permutations
{
// instance variables - replace the example below with your own
public static int[] chain(int[] p1, int[] p2) {
int[] result = new int[p1.length];
for (int i=0; i < result.length; i++) {
result[i] = p1[p2[i]];
}
return result;
}
public static int[] inverse(int[] perm) {
int[] result = new int[perm.length];
int[] arr = new int[perm.length];
for (int i=0; i < result.length; i++) {
result[perm[i]] = i;
}
return result;
}
public static int[] square(int[] perm) {
return chain(perm , perm);
}
public static int[] power(int[] perm, int k) {
while (k != 1 && k != -1 && k !=0) {
if ( k % 2 == 0) {
return chain(power(perm, k/2), power(perm, k/2));
} else {
return chain(power(perm, (k+1)/2), power(perm, (k-1)/2));
}
}
if (k==0 || k== -1) {
return inverse(perm);
}
return perm;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment