This question is similar to my other question: How do I randomly select 'k' positions in a list and shuffle their respective elements without effecting the other elements?
Imagine first a pack of cards. We randomly draw $k$ cards from the pack, and then sequentially reinsert them 'somewhere' in the deck (between cards or above or below the deck). Now I'd like to do the same thing with list elements using some hypothetical function pluckReinsert
:
k = 3;
list = {card1, card2, card3, card4, card5, card6, card7, card8, ..., cardN}
pluckReinsert[list,k]
output > {card2, card1, card3, card7, card4, card5, card42, card6, card8, ...}
Here, by chance, we randomly chose to "pluck" the set of cards: {card2, card7, card42}
. We then, sequentially, placed card2
on top of card 1 (there were $N+1$ total choices given the $N$ cards in the deck), placed card7
between card3
and card4
, and finally, placed card42
between card5
and card6
. At any of these sequential addition steps, there are were $N+1$ choices for placing the card (i.e. there would be nothing to stop us from an ordering like: {card42, card2, card1, card3, card4, card5, card6, card8, ..., cardN, card7}
).
Is there a clever Mathematica trick to do this in one or two lines?
Developer``DemiCompile
it can be done in half a line. $\endgroup$