The goal of this challenge is to collect selected items in a list and move them to a certain location in the list.
As a visual example, take the input values (represented by black boxed integers) and a corresponding list of truthy values where true denotes the item is selected (represented by blue boxes, where
T is truthy and
F is falsy):
The first logical step is to separate the items marked truthy and not truthy into their corresponding lists. Note that relative order in each list must be maintained (i.e. the order of selected items must be
1,4,5, and the order of unselected items must be
The second logical step is given an index in the remaining list of unselected items, insert all selected items before the item at the given index. Assuming indexing starts at 0, suppose you want to insert the selection at index 3. This corresponds to the spot before the
7 box, so the selected items should be inserted before the
The final solution is then
Note that this logical diagram depicts one way this could be done; your program does not need to take the same logical steps as long as the output always produces the same observable result.
Your program is given 3 inputs:
- A list of integers representing the items. This may be an empty list. This list will always consist of unique positive integers, not necessarily in sorted order (i.e. 5 will not be in the list twice).
- A list of truthy/falsy values with the same length as the list of items, where a truthy value represents that the item at the same index has been selected.
- An integer representing where to insert the selection. You may choose what the index of the first item of the list is as long as it is constant in every run of your program (e.g. the first item could be index 0 or index 1). Please specify which convention your program adheres to. This index should be in the range
[starting_idx, ending_idx+1], i.e. it will always be a valid index. For the case index is
ending_idx+1, the selection should be inserted at the end of the list. You may assume this integer will fit into your language's native integer type.
The input may come from any source desired (stdio, function parameter, etc.)
The output is a list representing the final sequence of items. This can be to any source desired (stdio, return value, function output parameter, etc.). You are allowed to modify any of the inputs in-place (for example, given a modify-able list as a function parameter, and having your function operate in-place on that list).
All of the following test cases assume 0-based indexing. I've used 0 and 1 to indicate falsy/truthy values respectively for the selection mask.
Test cases happen to have lists formatted as
[a,b,c], but as long as your input lists represent a finite ordered sequence that's fine.
  0
[1,2,3,4,5,6,7] [1,0,0,1,1,0,0] 3
[1,2,3,4,5,6,7] [1,0,0,1,1,0,0] 0
[1,2,3,4,5,6,7] [1,0,0,1,1,0,0] 4
[1,2,3,4,5,6,7] [1,1,1,1,1,1,1] 0
[1,2,3,4,5,6,7] [0,0,0,0,0,0,0] 5
[1,3,2,5,4,6] [1,0,0,1,1,0] 3
This is code golf; shortest answer in bytes wins. Standard loopholes are prohibited. You are allowed to use any built-ins desired.