13
1
Given an integer matrix a
and a nonnegative integer i
, output a mapping b
that maps the distinct values in the i
th column of a
to rows of a
who have that value in the i
th column.
You may assume that i
is in the half-open range [0, num_cols(a))
(or [1, num_cols(a)]
if you choose to use 1-based indices), and that all integers are within the representable range for your language. Input and output may be done in any reasonable manner, so long as it satisfies the basic requirements of the challenge (2D array -> mapping from ints to 2D arrays of ints). So long as the mapping is clear and consistent, the keys do not need to be included in the output.
Examples
[[1]], 0 -> {1: [[1]]}
[[3, 4, 5], [1, 4, 2], [5, 5, 5], [7, 7, 7], [1, 5, 9]], 1 -> {4: [[3, 4, 5], [1, 4, 2]], 5: [[5, 5, 5], [1, 5, 9]], 7: [[7, 7, 7]]}
[[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [2, 3, 4, 5, 6], [8, 9, 100, 0, 2]], 4 -> {5: [[1, 2, 3, 4, 5]], 1: [[5, 4, 3, 2, 1]], 6: [[2, 3, 4, 5, 6]], 2: [[8, 9, 100, 0, 2]]}
This is code-golf, so the shortest answer in bytes wins.
1Sandbox – Mego – 2018-05-20T03:29:36.893
Just to check, can the mapping be a function? I'm not aware if this is a default, but it seems like something you intend to allow. – FryAmTheEggman – 2018-05-20T03:37:48.563
@FryAmTheEggman Yes, a function that meets our usual requirements is allowed. The I/O is extremely flexible. – Mego – 2018-05-20T03:42:51.570
3I like this I/O format very much because the output does not actually need to contain the input in itself. It's completely fine to return a function that accesses the input by reference as long as the function is a mapping. – JungHwan Min – 2018-05-20T04:40:17.033
@JungHwanMin I'm glad. I wanted to experiment with a very loose I/O format, and it's going well so far – Mego – 2018-05-20T04:44:20.520