Help:=proc(): print(`FirstBlock(pi), BD(pi)`): print(`SimaoHerdade(B), WhatType(pi), FindBlock(L,a)`): end: with(combinat): #inputs a permutation pi and outputs the first #(consectutive) block (may be a singleton) written #as a list, and the left-over. #For example, FirstBlock([5,4,1,2,3]); #should yield [5,4] , [1,2,3]. FirstBlock:=proc(pi) local i: for i from 1 to nops(pi)-1 while abs(pi[i+1]-pi[i])=1 do od: [op(1..i,pi)], [op(i+1..nops(pi),pi)]: end: #BD(pi): inputs a permutation pi and outputs its #decomposition into (consec.) blocks. For example #BD([2,3,5,4,6,1]); should be #[[2,3],[5,4],[6],[1]]; BD:=proc(pi) local L,L1,fb: #L1: what is left. L the running output L1:=pi: L:=[]: while L1<>[] do fb:=FirstBlock(L1): L:=[op(L),fb[1]]: L1:=fb[2]: od: L: end: #inputs a list of numbers and outputs 0 if it #is a singleton, +1 if it increasing and -1 if #if it decreasing SimaoHerdade:=proc(B) if nops(B)=1 then RETURN(0): elif B[2]-B[1]>0 then RETURN(1): elif B[2]-B[1]<0 then RETURN(-1): else ERROR(`Something bad happened`): fi: end: #FindBlock(L,a): inputs a list of lists and #a number a and outputs the place of the #list containing a, or returns FAIL #For example, FindBlock([[3,4],[1,5],[6,7]],5); #should return 2. FindBlock:=proc(L,a) local i: for i from 1 to nops(L) while not member(a,L[i]) do od: if i=nops(L)+1 then FAIL: else i: fi: end: #WhatType(pi): inputs a permutation pi and outputs #its type in the format of (usually) triplet #of pairs [-1,0],[-1,-1],[-1,1],[0,0],[0,-1],[0,1] etc. #e.g. [-1,0] is the block of the elements immeidately #smaller than the leftmost block and the [0] in [-1,0] #means that it is a singleton. If it would be increasing #it would be labelled [-1,1], and decreasing [-1,-1] WhatType:=proc(pi) local L,M,M1,t1,t2,i,j: if pi=[] then RETURN([]): fi: M:=BD(pi): M1:=M[1]: L:=[[0, SimaoHerdade(M1) ]]: t1:=min(M1)-1: t2:=max(M1)+1: #i= the location of t1 #j= the location of t2 i:=FindBlock(M,t1): j:=FindBlock(M,t2): if i<>FAIL and j<>FAIL and iFAIL and j<>FAIL and i>j then L:=[op(L), [1,SimaoHerdade(M[j])],[-1,SimaoHerdade(M[i])]]: elif i<>FAIL and j=FAIL then L:=[op(L),[-1,SimaoHerdade(M[i])]]: elif j<>FAIL and i=FAIL then L:=[op(L),[1,SimaoHerdade(M[j])]]: fi: L: end: