Inspired by Help me pair my socks, and the fact that I have some pairs of socks where the left sock is distinct from the right.
Background
Again, I have a huge pile of socks. And a machine that can give the list of labels of the socks for me. This time, the labels follow these rules:
- Some socks have left and right versions. In this case, left socks are labelled
-n
and right socks are labelledn
, wheren
is a unique positive integer. The list of such socks will be explicitly given to you. - The others don't have such a thing, and they just match themselves. All socks of this type are simply labelled
n
(no negative labels).
An example pile would look like this:
[3, 3, -2, 4, -1, -1, 1, 4, 4, 3, 2, 3, -1, 4, 4]
where 1 and 2 have left and right versions. Then the pairs and leftovers are:
pairs: {3: 2, 4: 2, 1: 1, 2: 1} or [1, 2, 3, 3, 4, 4]
leftovers: {4: 1, -1: 2} or [-1, -1, 4]
Note that two -1
s don't make a pair because both are left socks.
Challenge
Given the pile of socks and the list of sock labels having L/R versions, output the pairs that can be made from the pile, and leftovers that are not part of any pairs.
Input and output
You can assume the following for input:
- Zeros won't appear in either list.
- L/R labels are unique and all positive, but possibly not sorted.
- Negative numbers that are not part of L/R labels won't appear in the pile.
Both outputs can be represented as either a mapping type (label to count) or a flat list (each item appearing once per count). You can output in following ways:
- Pairs can be represented as either a pair
(3, 3) / (-1, 1)
or a single number3 / 1 / -1
, or a mixture of both. But the same pairs should be represented in the same way in a single run, e.g. outputting[3, (3, 3)]
or[(-1, 1), (1, -1)]
or[1, -1]
is not allowed. - For both pairs and leftovers, a pair and single number can be wrapped in any kind of containers, e.g. singleton arrays can be used to represent a single leftover.
- For both pairs and leftovers, the order of items is not important.
- If you use a mapping type, zero-count items are allowed, even the ones not in the pile of socks.
Scoring and winning criterion
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
Pile: [3, 3, -2, 4, -1, -1, 1, 4, 4, 3, 2, 3, -1, 4, 4]
LR: [1, 2]
Pairs: [1, 2, 3, 3, 4, 4]
Leftovers: [-1, -1, 4]
Pile: [1, 1, 2, 2]
LR: [1]
Pairs: [2] (two right 1's and two simple 2's, so only 2's make a pair)
Leftovers: [1, 1]
Pile: [5, -10, 5, 10, -10, 10]
LR: [10, 20]
Pairs: [5, 10, 10]
Leftovers: []
Pile: [-6, 7, -9, 3, 4, -6, 4, -9, 8]
LR: [6, 9, 4]
Pairs: []
Leftovers: [-6, 7, -9, 3, 4, -6, 4, -9, 8]
Pile: []
LR: [1, 2]
Pairs: []
Leftovers: []
[[4],[-1],[-1]],[[3,3],[3,3],[-2,2],[4,4],[4,4],[-1,1]]
- this is the current output of the Jelly answer, or the output of the Japt answer without its second line), or is it mandatory to simplify it (like the Japt answer did with its second line)? \$\endgroup\$Pairs
and which ones are theLeftovers
? I.e. can one test case result in[[[1],[1]], [[2,2]]]
([Leftovers, Pairs]
) while another test case results in[[[3,3],[-2,2]], [[2],[2],[5]]]
(Pairs, Leftovers]
)? \$\endgroup\$