Help:=proc() :print(`Rev(pi,i,j) , StupidS(pi), StupidS1(pi) `): end: #Rev(pi,i,j): the permutation obtained #by reversing pi[i], ..., pi[j] and #keeping the rest the same Rev:=proc(pi,i,j) local k: [op(1..i-1,pi), seq(pi[j-k],k=0..j-i), op(j+1..nops(pi),pi)] end: #StupidS1(pi): the pair [i,j] needed to restore #the first item not in its place to its place StupidS1:=proc(pi) local i,j: for i from 1 to nops(pi) while pi[i]=i do od: if i=nops(pi)+1 then RETURN(FAIL): fi: for j from i+1 to nops(pi) while pi[j]<>i do od: [i,j]: end: #StupidS(pi): inputs a permutation pi #and outputs a list of reversals [i,j] #that restores it to the identity StupidS:=proc(pi) local i,n,pi1,L,id1,pair1: n:=nops(pi): pi1:=pi: L:=[]: id1:=[seq(i,i=1..n)]: while pi1<>id1 do pair1:=StupidS1(pi1): L:=[op(L),pair1]: pi1:=Rev(pi1,op(pair1)): od: L: end: