分组数组数据


13

给定一个整数矩阵a和一个非负整数i,输出的映射b,所述不同值映射在i的列a到列a谁具有在该值i列。

您可以假定该i值在半开范围内[0, num_cols(a))(或者[1, num_cols(a)]如果您选择使用基于1的索引),并且所有整数都在您的语言可表示的范围内。只要满足挑战的基本要求即可进行输入和输出(2D数组->从int到int的2D数组的映射)。只要映射清晰且一致,就不需要在输出中包含键。

例子

[[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]]}

这是,因此最短的答案以字节为单位。



仅检查一下,映射是否可以起作用?我不知道这是否是默认设置,但似乎您打算允许这样做。
FryAmTheEggman

@FryAmTheEggman是的,允许使用满足我们通常要求的函数。I / O非常灵活。
麦戈

3
我非常喜欢这种I / O格式,因为输出实际上不需要本身包含输入。只要函数是映射,返回一个通过引用访问输入的函数就可以了。
JungHwan Min

@JungHwanMin我很高兴。我想尝试一种非常宽松的I / O格式,到目前为止进展顺利
-Mego,

Answers:


4

八度,24字节

@(a,i)@(n)a(a(:,i)==n,:)

在线尝试!

这将创建一个匿名函数,该函数返回行与条件匹配的矩阵。八度索引数组为1,而不是零,矩阵的行由a分隔;

矩阵是Octave最擅长的方面,因此,事实上,可以使用纯语法而不使用内置函数来解决此难题。

说明

@(a,i)                   % creates an anonymous function that...
      @(n)               % returns another function that takes input n and
                         % maps it to the rows of a.
          a(         ,:) % Return all the columns of a, with the rows filtered by...
            a(:,i)       % whether the ith column of each row of a...
                  ==n    % equals n


3

Wolfram语言(Mathematica),21字节

#~GroupBy~Extract@#2&

1个索引。返回一个Association映射。

在线尝试!

在极少数情况下,较长的函数(Extract)会减少字节数(较短的字符是Part[[ ... ]]),因为它Extract可以咖喱。结果就是这种极为简洁的两功能解决方案。

说明

Extract@#2

提取<second input>th元素的函数。

#~GroupBy~ ...

<first input>与不同键关联的列表分组<above function>[element]



2

干净,40字节

import StdEnv

\n l i=filter(\a=a!!n==i)l

在线尝试!

:: Int [[Int]] Int -> [[Int]]仅部分应用前两个参数的lambda()会映射第三个参数。


2

J,16字节

-3个字节,感谢FrownyFrog!

{"1(~.@[;"0</.)]

在线尝试!

说明:

二进位动词,i以其左引数和a其右引数为准。

] 是正确的论据, a

{"1i在每一行的第th列找到数字

</. 右参数的框组,由键选择,由左一个提供

~.@[ 找到唯一的键

;"0 将按键链接到所选组


;"0而不是,:保存3
FrownyFrog

@FrownyFrog当然!我想我尝试过,但是显然不是正确的方法。
Galen Ivanov '18

2

jq,100个字节

使用对象进行输出,$f在标准输入上使用命令行参数和数组

([.[]|.[$f]]|unique) as $c|[$c[] as $d|{($d|tostring):([.[]|[select(.[$f]==$d)]]|add)}]|add

消除混淆:

.fieldnum as $field |
.input as $input |
([$input[] | .[$field]] | unique) as $categories |
[
    $categories[] as $category |
    {
        ($category | tostring) :
            ([$input[] | [select(.[$field]==$category)]] | add)
    }
] | add

你使用的语言?
世纪





0

果冻,5个字节

ịⱮ⁹¹ƙ

在线尝试!

省略键,但应该清楚。

参数1:i + 1
参数2:a


我认为这没有键就不能作为映射。
丹尼斯

@Dennis Hm,我在评论中问过,OP说我们可以省略键(正是我在问题中编辑的内容),而且我还在此链接了此解决方案(也许不应该这么早就标记出来... )。我的确在此答案的先前版本中包含了键(等待答案),所以我将仅发表另一条评论,让我们看看OP怎么说。
暴民埃里克(Erik the Outgolfer)'18年

0

Java 10,135 64字节

m->i->n->new java.util.Stack(){{for(var a:m)if(a[i]==n)add(a);}}

返回一个Function<Integer, List<int[]>>接受整数输入n,该整数输入返回一个数组列表(矩阵行),其中第i'th个值等于给定n

在线尝试。

说明:

m->i->               // Method with int-matrix and int parameters and Function return-type
  n->                //  Return a Function with integer as parameter
    new java.util.Stack(){{
                     //  and List of integer-arrays as return-type
      for(var a:m)   //   Loop over the arrays of the input-matrix
        if(a[i]==n)  //    If the `i`'the value of the current array equals `n`:
          add(a);}}  //     Add it to the return-List
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.