function B = TestV(V)
    B=%F;
  for i = 1 : length(V)
     if V(i)==1 then
         B=%T ; 
         break ;
     end
  end  
endfunction


function B = dejaVue(ListA,toCheck)
    B=%F;
    
if size(ListA)(1) > 1 then 
    L=size(ListA)(1);  
  for i = 1 : L
     if ListA(i)==toCheck then
         B=%T ; 
         break ;
     end
  end  
end 
endfunction

function [ m ] = PermuteMatrixColumn(M,LikeThisColumn)
// M  = 
//   1.   0.   0.   0.   0.
//   0.   1.   0.   0.   0.
//   0.   0.   1.   0.   0.
//   0.   0.   0.   1.   0.
//   0.   0.   0.   0.   1.
// and
// LikeThisColumn= 4.   5.   1.   3.   2.
m=zeros(length(LikeThisColumn),length(LikeThisColumn));
for i = 1 : length(LikeThisColumn)
    for j = 1 : length(LikeThisColumn)
        m(i,j)=M(i,LikeThisColumn(j));
    end
end
endfunction


function n  = WhereDoesItGo(e,This)
n=0;
	for i = 1 : length(This)
       if   This(i)==e then
           n=i;
           break
       end

    end 
endfunction



function [ListSeqString,ListSeq ] = GetTheSequence(ThisS,LastThisS)
 // get the List of sequences which has been applied
 //  to transform LastThisS (Latter This) to ThisS
 // Example LastThisS='ABCD' then if 
 // ThisS:CBAD  S:3214  => ListSeq=list([1 3])
 // ThisS:CABD  S:3124  ==> ListSeq=list([1 2 3])
 // ThisS:DBAC  S:4213  ==> ListSeq=list([1 3 4])
 // ThisS:DABC  S:4123  ==> ListSeq=list([1 2 3 4])
 // ThisS:BADC  S:2143  ==> ListSeq=list([1 2],[3 4])
 
   // test if This is Matrix of Letter
   // if yes transpose it in number matrix
   This=[];
   LastThis=[]; 
 if type(ThisS)==10 then 
     // convert This in number
     This=ascii(ThisS) ;
     This=This-64 ;
 end 

 if type(LastThisS)==10 then 
     // convert This in number
     LastThis=ascii(LastThisS) ;
     LastThis=LastThis-64 ;
 end 
 

 
LThis=length(This) ;
LLastThis=length(LastThis) ;
if LThis ~= LLastThis then
 error('The Two Inputs Variables must have the same length ! '+ string(length(ThisS))+'<>'+string(length(ThisS)) );
 abort   
end
V=ones(This); // set the flags
ListSeq=list() ;
k=1;
l=1;
FirstPointer=1;
Pointer=0;

// Check Element which does not change
// and get rid of them(clear the element flag)

for j=1 : LThis
    if This(j)==LastThis(j)then 
      V(j)=0 ;
    end
end
Seq=[];   
//disp('V:');disp (V);
while  TestV(V)==%T & l< 7
    
        Seq=[] ; // reset for next sequence
    for j=1 : LThis
        if V(j)== 1 then
            FirstPointer=j ;
            break // Next sequence to be processed
        end
    end
    Pointer=WhereDoesItGo(LastThis(FirstPointer),This);
    V(FirstPointer)=0 ; V(Pointer)=0 ;
    Seq=[Seq  LastThis(FirstPointer) LastThis(Pointer)];
        disp (V);
        while WhereDoesItGo(This(Pointer),This)~=FirstPointer
            // Browse the sequence to be processed until
            // until looping on the first one which flag has been cleared
            
            Pointer=WhereDoesItGo(LastThis(Pointer),This);
            Seq=[Seq   LastThis(Pointer)];
            V(Pointer)=0 ;
            
            disp (V); disp('Pointer:'+string(Pointer)) ; 
      
        end
        //gets rid of the last one
        //(Because it's repetition of the first one !)
        Seq($)=[];
        
        ListSeq= lstcat( ListSeq,list(Seq))  ;

end
ListSeqString='( ';
for i=1 : size(ListSeq)
    SLQ=size(ListSeq(i));
    M=ListSeq(i) ;
    if i > 1 then ListSeqString=ListSeqString +' )( ' ; end
//    for j=SLQ(2) : -1:1
    for j=1 : SLQ(2)
        ListSeqString=ListSeqString + string(M(j)) +' ' ;
       // disp(M(j)); 
    end 
end
ListSeqString=ListSeqString +' )' ;
endfunction


//input ThisS in this form ThisS=['A', 'B', 'C', 'D'] ;
//or in this form ThisS=['ABCD'] ;
// ; (must be natural number)
//Times=1;
//[S SS]= PermuteThis(ThisS,LikeThat,Times)
function [S,SS]  = PermuteThis(ThisS,LikeThat,Times)
   
   tt=type(ThisS) ;
   
   // test if This is Matrix of Letter 
 if tt==10 then 
     // convert This in number
     This=ascii(ThisS) ;
     This=This-64 ;
 end  

S=[This ] ; 


LThis=length(S) ;
LikeThat=[LikeThat] ;
L=length(LikeThat) ;

for j = 1 : Times
	for i = 1 : L-1
       S(LikeThat(i+1))=This(LikeThat(i));   
        //disp('copy number: ' + string(i) +' = '+ string(This(LikeThat(i))) ..
        //+ ' Goes to ' + string(This(LikeThat(i+1))) );
    end 
    //  last implicit swap (last number swap with 1 )
    // replace first element of LikeThat 
    // with the last element of the sequence LikeThat
    S(LikeThat(1))=This(LikeThat(L)) ;
    //disp('copy number: ' + string(L) +' = '+ string(This(LikeThat(L))) ..
    //    + ' Goes to ' + string(This(LikeThat(1))) );

end      
if tt==10 then 
// convert S in letter
 // convert S in letter
   ttt=S+64 ;
   SS=ascii(ttt);
end

endfunction

// execute all permutation (one per row) contained in LikeThat List)
// example : if you want to apply  < <1 2 3 >, <3 4 > >
// on a Sequence of 4 letters ABCD which means 
// first < 1 2 3 > and then < 3 4 >
// you must input 
//ThisS=['ABCD'] ; ListLikeThat=list( [ 1 2 3 ],[ 3 4 ]) ; Times=1 
// [S,SS]  = SequencePermuteThis(ThisS,ListLikeThat,Times)
function [SL,SSL]  = ListPermuteThis(ThisS,ListLikeThat,Times)
 SizeListLikeThat=size(ListLikeThat) ;
 TSSL=ThisS;
 for j = 1 : Times // repeat it Times Value
    for i = 1 : SizeListLikeThat(1)
        [SL,SSL]=PermuteThis(TSSL,ListLikeThat(i),1);
         TSSL= SSL ; 
    end
    
 end
endfunction
