Help:=proc(): print(`WhatType(pi), FindBlock(pi,a) `): end: #WhatType(pi): inputs a permutation and outputs its #type (a triple (or pair)) of pairs indicating #whether the first block (block #0) is a singleton (0) increasing #or decreasing, and similarly for block #1 (the block #of integers immediately larger than the first block etc. #for example, WhatType([2,3,5,4,1,6]) should be #[ [0,1],[1,-1],[-1,0]], and retunrs the "parsing" #[[2,3],[5,4],[1]] WhatType:=proc(pi) local i,t,BZ,B1,Bm1,rebecca, wei,s, s1,sm1: rebecca:=[]: wei:=[]: if nops(pi)=1 then RETURN([[0,0]]): fi: t:=pi[1]: if pi[2]=t+1 then for i from 2 to nops(pi) while pi[i]-pi[i-1]=1 do od: BZ:=[op(1..i-1,pi)]: rebecca:=[op(rebecca),[0,1]]: wei:=[op(wei), BZ]: elif pi[2]=t-1 then for i from 2 to nops(pi) while pi[i]-pi[i-1]=-1 do od: BZ:=[op(1..i-1,pi)]: rebecca:=[op(rebecca),[0,-1]]: wei:=[op(wei), BZ]: else BZ:=[t] : rebecca:=[op(rebecca),[0,0]]: wei:=[op(wei), BZ]: fi: s:=BZ[nops(BZ)]: if rebecca[1][2]=0 then s1:=s+1: sm1:=s-1: elif rebecca[1][2]=1 then s1:=s+1: sm1:=t-1: elif rebecca[1][2]=-1 then s1:=t+1: sm1:=s-1: fi: rebecca, wei,s1,sm1: end: #inputs a perm. pi and an integer a finds # the block that it belogs to and whether # it is a sinleton (0), increasing (1) or decreasing (-1) #For example,FindBlock([2,3,1,4,5,6,7,8],5); should return #[4,5,6,7,8],1 FindBlock:=proc(pi,a) local i,ty,n,b,e,j: n:=nops(pi): for i from 1 to nops(pi) while pi[i]<>a do od: i: if (i>1 and abs(pi[i-1]-pi[i])<>1) and (i1) then ty:=0: elif (i>1 and abs(pi[i]-pi[i-1])=1) and (i1) then e:=a: if pi[i-1]-pi[i]=1 then ty:=-1: for j from i to 2 by -1 while pi[j-1]-pi[j]=1 do od: RETURN([op(j..i,pi)],ty): elif pi[i-1]-pi[i]=-1 then ty:=1: for j from i to 2 by -1 while pi[j-1]-pi[j]=-1 do od: RETURN([op(j..i,pi)],ty): fi: fi: end: