Thanks to the PPCG community, Santa has managed to remanufacture all of his presents and after the assembly line, the presents are now ready to be moved into the transport docks!
Each of Santa's transport docks only holds a range of present sizes because the transport sleighs are specialized for a specific size (any lighter and it would be wasteful, any heavier and the sleigh wouldn't be able to handle the load). Thus, he needs you to help him take his presents and sort them into the correct transport docks.
Challenge
Given a list and the transport dock ranges, stably organize the presents into the correct order.
Let's take this for example: the presents are [5, 3, 8, 6, 2, 7]
and the dock ranges are [[1, 5] and [6, 10]]
.
The presents 5
, 3
, and 2
go into the first dock and the presents 8
, 6
, and 7
go into the second dock. This can be shown as [[5, 3, 2], [8, 6, 7]]
. This list will be closer to being sorted than the input, but stably
means that within each dock, the order of the presents must be the same as the order of the input (otherwise you could just sort the entire list).
Your final output for this case would be [5, 3, 2, 8, 6, 7]
(as a flat list).
Formatting Specifications
You will be given input as a flat list of integers and a list of ranges in any reasonable format (for example, the range for the above case could be given as [[1, 5], [6, 10]]
, [1, 5, 6, 10]
, or [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
). Your output should be a flat list of integers in any reasonable format.
The input can contain duplicate values; in this case, you need to return all instances of them. All present sizes will be in exactly one size range, and you can assume that the ranges will never overlap. There can be gaps in the ranges as long as all present sizes are covered.
Rules
- Standard Loopholes Apply
- This is code-golf, so the shortest answer in bytes wins
- No answer will be accepted
- You can assume that there will be no empty ranges (
[7, 4]
would be invalid because ranges go up)
Test Cases
[1, 2, 3, 4, 5, 6, 7] ; [[1, 3], [4, 7]] => [1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7] ; [[4, 7], [1, 3]] => [4, 5, 6, 7, 1, 2, 3]
[7, 3, 5, 4, 6, 1, 2] ; [[1, 3], [4, 5], [6, 7]] => [3, 1, 2, 5, 4, 7, 6]
[4, 7, 6, 3, 5, 2, 1] ; [[1, 4], [5, 7]] => [4, 3, 2, 1, 7, 6, 5]
[1, 1, 3, 3, 6, 4, 7] ; [[1, 4], [6, 7]] => [1, 1, 3, 3, 4, 6, 7]
Note: I drew inspiration for this challenge series from Advent Of Code. I have no affiliation with this site
You can see a list of all challenges in the series by looking at the 'Linked' section of the first challenge here.