Help:=proc() if nargs=0 then print(`This package treats the Jacob Goodman problem`): print(`and contains procedures:`): print(`Flip(L,i),NS(L),FindElement(i,L),NSSteps(L)`): elif nargs=1 and args[1]=Flip then print(`Flip(L,i):`): print(`Inputs a permutation L as a list of integers`): print(`and 'flips' the first i entries.`): print(`Try: Flip([1,2,3,4,5],3)`): elif nargs=1 and args[1]=NS then print(`NS(L):`): print(`Inputs a permutation L as a list of integers and`): print(`naively sorts them by searching for the largest integer`): print(`not in its correct place, flips that integer to the front`): print(`of the list and then flips the front of the permutation`): print(`to place it correctly.`): print(`Outputs a list of the positions where flips were carried`): print(`out at.`): print(`Try: NS([2,1,5,4,3])`): elif nargs=1 and args[1]=FindElement then print(`FindElement(i,L):`): print(`Inputs an element i and list L and outputs`): print(`the position of i in L. If i is not in L, no output.`): print(`Try: FindElement(2,[1,4,7,2,3,5,6])`): elif nargs=1 and args[1]=NSSteps then print(`NSSteps(L):`): print(`Inputs a permutation L as a list of integers and`): print(`outputs the number of steps in the naive sort method NS(L).`): fi: end: #=======================================================================Courtesy of David J. Wilson###################### #Flip(L,i): takes a permutation L as a list of integers #and 'flips' the first i entries Flip:=proc(L,i) local j,L1: L1:=L: for j from 1 to i do L1[j]:=L[i-j+1]: od: L1: end: #======================================================================= #NS(L):Inputs a permutation L as a list of integers and #naively sorts them by searching for the largest integer #not in its correct place, flips that integer to the front #of the list and then flips the front of the permutation #to place it correctly. #Outputs a list of the positions where flips were carried #out at NS:=proc(L) local L1,i,pon,P: L1:=L: P:=[]: for i from nops(L) to 1 by -1 do if L1[i]<>i then pon:=FindElement(i,L1): L1:=Flip(L1,pon): P:=[op(P),pon]: L1:=Flip(L1,i): P:=[op(P),i]: fi: od: P: end: #======================================================================= #FindElement(i,L):Inputs an element i and list L and outputs #the position of i in L. If i is not in L, no output. FindElement:=proc(i,L) local j: for j from 1 to nops(L) do if L[j]=i then RETURN(j): fi: od: end: #======================================================================= #NSSteps(L):Inputs a permutation L as a list of integers and #outputs the number of steps in the naive sort method NS(L). NSSteps:=proc(L) local L1,i,pon,steps: nops(NS(L)): end: