Suppose I have a function cyclePart
which has a definition for the case
cyclePart[list_->{},n_,Δn_,cycle_:True]:=...
But for example in the algorithm, if it encounters a case like
cyclePart[{a,b,c,d,e,f,g,h,i,j}->{b,d,i,j},5,2,True]
I want it to transform it into the previous form so its base definition can be applied. In this case, the number 5
is referring to the position in the original list and the list to the right of the arrow tells it to drop these elements.
cyclePart[list_->drop_,n_,Δn_,cycle_:True]:=
cyclePart[Complement[list,drop]->{},...updated n...,Δn,cycle]
But how to transform n
here? Is there a builtin function that can get an updated position after element drops? Note in the example, the updated list's position 3
refers to position 5
since two elements {b,d}
to the left of position 5
are being removed so position 5
moves down to position 3
.
As seen in the comment of @kglr Complement[list, drop]
should be changed to DeleteCases[list, Alternatives @@ drop]
since the former is a set drop which means it removes duplicates and sorts the list in addition to the drop. I incorrectly used Complement[list, drop]
but meant the actions of DeleteCases[list, Alternatives @@ drop]
.
cyclePart[list_ -> drop_, n_, \[CapitalDelta]n_, cycle_: True] := Module[{newlist = DeleteCases[list, Alternatives @@ drop]}, cyclePart[newlist -> {}, Position[newlist, list[[n]]], \[CapitalDelta]n, cycle]]
? $\endgroup$cyclePart[list_ -> drop_, n_, \[CapitalDelta]n_, cycle_: True] := Module[{newlist = DeleteCases[list, Alternatives @@ drop]}, cyclePart[newlist -> {}, PositionIndex[newlist][list[[n]]], \[CapitalDelta]n, cycle]]
? $\endgroup$cyclePart[{e,b,c,d,e,f,g,h,i,e}->{b,d,i,j},5,2,True]
$\endgroup$ReplacePart
on the original list to differentiate the element first then use this trick but that doesn't seem like a professional way of doing it. Is there a function that can target positions directly? $\endgroup$