反转索引列表的列表


14

灵感来自这个 StackOverflow的职位。

介绍

Bob的工作是创建电子表格并对其进行组织。除Bob以外,很少有人知道他组织这些文件的方式,但是他创建了属于同一组的每个电子表格的列表。他创建的电子表格中有很多数据,但是现在我们只查看其中的一条数据:从他开始工作到创建电子表格之间的天数。第一天,他创建了两个电子表格,将它们都记录下来,0然后将它们分类到正确的位置。

现在,他的老板要求对每天发生的电子表格种类进行审查,编写一些代码来为Bob找出答案是您的工作。他手头上有太多电子表格。

输入值

鲍勃(Bob)提供给您的信息是以(0或1索引)锯齿状数组的形式出现的,每个数据的形式都是x = a[i][j]a是我所说的锯齿状数组本身,i是电子表格的类型,x也是数组创建的日期。 j不重要。

任务

给定按类型划分的电子表格创建日期的锯齿形数组,返回按电子表格创建日组织的电子表格类型的锯齿形数组。

例子

Bob不会只留下这些抽象数据。他给了我一些电子表格的一个子集,以帮助您弄清楚应该是什么。

输入示例(0索引):

a = [
[3,2,5,0], # Bob doesn't necessarily sort his lists
[1,3],
[2,1,0,4],
[4,5,3],
[6,6]
]

输出示例(带注释,当然不是必需的):

output = [
[0,2] # On day 0, Bob made one type 0 and one type 2 spreadsheet
[1,2] # On day 1, Bob made one type 1 and one type 2 spreadsheet
[0,2] # On day 2, Bob made one type 0 and one type 2 spreadsheet
[0,1,3] # On day 3, Bob made one type 0, one type 1, and one type 3 spreadsheet
[2,3] # On day 4, Bob made one type 2 and one type 3 spreadsheet
[0,3] # On day 5, Bob made one type 0 and one type 3 spreadsheet   
[4,4] # On day 6, Bob made two type 4 spreadsheets
]

请注意,鲍勃并非总是每天都制作两个电子表格,因此输出也可能会参差不齐。但是他每天总是至少制作一个电子表格,因此输出永远不需要包含空数组-尽管如果输出的末尾有空数组,则不需要删除它们。

更多测试用例:

[[3,5,6,2],[0,0,0],[1,0,3,4]] -> [[1,1,1,2],[2],[0],[0,2],[2],[0],[0]]
[[-1]] -> Undefined behavior, as all input numbers will be non-negative integers. 
[[0],[0],[],[0]] -> [[0,1,3]]

输出的内部列表不需要排序。

与往常一样,没有标准漏洞,而且最短的代码当然是成功的。

(由于这是我的第一个问题,请让我知道我可以做些什么来改进它。)


鲍勃可能不会制作任何类型的电子表格吗?
xnor

1
@xnor不,Bob每天都会创建一个电子表格。这些电子表格对公司的运营至关重要,如果鲍勃必须请病假,则将临时派一个人来创建当天的电子表格,而鲍勃在第二天早上组织它们,然后创建自己的电子表格。
史蒂文H.

1
@Dennis当把那个例子放在一起时,我有点累了,我想它表明了。:P固定(均为固定)!
史蒂文H.

6
看起来不错。这是一个非常好的挑战,尤其是考虑到这是您的第一个挑战。:)顺便说一句,如果您不知道,我们有一个沙盒,社区可以在“上线”之前提供反馈。
丹尼斯

1
我根据您的评论在声明中进行了编辑,“ 不,鲍勃每天都会创建一个电子表格 ”,但是既然我尝试优化自己的答案,我已经意识到它的限制可能比您预期的要严格。是否允许尾随空数组?例如可以[[0 0]]输出[[0 0] []]
彼得·泰勒

Answers:



5

Pyth,13个字节

eMM.ghkSs,RVU

         ,RV      vectorized right map of pair, over:
            UQ      [0, …, len(input) - 1] and
              Q     input
                  (this replaces each date with a [date, type] pair)
        s         concatenate
       S          sort
   .ghk           group by first element (date)
eMM               last element (type) of each sublist

在线尝试



4

Brachylog,28个字节

:1f.
cdo:Im:?:2f.
t:.m:Im~h?

说明

  • 主谓词,输入(?)=列表列表

    :1f.              Find all valid outputs of predicate 1 with ? as input
    
  • 谓词1:

    c                 Concatenate the list of lists into a single list
     do               Remove duplicates and sort
       :Im            Take the Ith element of that sorted list
          :?:2f.      Find all valid outputs of predicate 2 with [Element:?] as input
    
  • 谓词2:

    t:.m              Take the (Output)th element of the list of lists
        :Im           Take the Ith element of that list
           ~h?        This element is the element of the input [Element:List of lists]
    


3

JavaScript(ES6),58个字节

a=>a.map((b,i)=>b.map(j=>(r[j]=r[j]||[]).push(i)),r=[])&&r

3

CJam(30 29字节)

Mq~{W):W;{:X)Me]_X=W+X\t}/}/`

在线演示

奇怪的是,使用它W比使用它要短ee,主要是因为我还是希望索引以变量结尾。e]首先找到最大元素m并初始化一个m+1空数组,节省了两个字节。

感谢Martin通过将值存储在其中X而不是在堆栈中随意摆弄节省了一个字节。

注意:如果允许使用尾随的空数组,则可以使用另一种29字节的方法,方法是初始化与电子表格一样多的空天数组:

q~_e_,Ma*\{W):W;{_2$=W+t}/}/`

3

CJam,20个字节

感谢Peter Taylor让我将此代码基于他的解决方案并节省了3个字节。

{ee::f{S*\+S/}:~:.+}

一个未命名的块,其期望输入位于堆栈的顶部,并将其替换为输出。

在这里测试。

说明

ee    e# Enumerate the input. E.g. if the input is 
      e#   [[3 5 6 2] [0 0 0] [1 0 3 4]]
      e# this gives:
      e#   [[0 [3 5 6 2]] [1 [0 0 0]] [2 [1 0 3 4]]]
::f{  e# The exact details of how this works are a bit tricky, but in effect
      e# this calls the subsequent block once for every spreadsheet and
      e# its correspond index, so the first time we'll have 0 and 3 on the
      e# stack, the next time 0 5, and at the last iteration 2 and 4.
      e# Note that this is a map operation, so we'll end up with an array
      e# on the stack.
  S*  e#   So the stack holds [... index date] now. We start by creating
      e#   a string of 'date' spaces, so "   " on the first iteration.
  \+  e#   We swap this with the index and append the index.
  S/  e#   Now we split this thing on spaces, which gives us 'date' empty
      e#   lists and a list containing the index, e.g. [[] [] [] [0]].
}
:~    e# This flattens the first level of the result, so that we get a list
      e# of all those lists we just created. This is simply one list for
      e# every spreadsheet with its type in the last element.
:.+   e# Finally we fold pairwise concatenation over this list. All the 
      e# empty lists won't affect the result so we'll just end up with all
      e# the types in lists for the correct date.

2

Python 2,82个字节

r=[];i=0
for t in input():
 for v in t:r+=[()]*-(~v+len(r));r[v]+=i,
 i+=1
print r

Ideone上进行测试


2

Mathematica,35个字节

Table[#&@@@#~Position~i,{i,Max@#}]&

接受并返回参差不齐的列表的未命名函数。使用基于1的索引。

值得庆幸的是,它会Max自动拉平所有输入,因此即使输入是一个参差不齐的列表,它也可以为我们提供最后一天的索引。然后,我们仅计算一#&@@@#~Position~i整天指数的列表i。该表达式本身可以找到i参差不齐的列表内部的位置(返回为连续深度处的索引数组),因此我们想要的值是每个列表中的第一个值。#&@@@这是一种标准的打高尔夫球技巧,通过应用#&到每个子列表来从每个子列表中检索第一个元素,这是一个未命名的函数,可返回其第一个参数。

另外,我们可以为相同的字节数定义一个一元运算符(假设使用ISO 8859-1编码的源文件):

±n_:=#&@@@n~Position~#&~Array~Max@n

2

Java,314个字节

int[][]f(int[][]n){int w=0;Map<Integer,List<Integer>>m=new TreeMap<>();for(int i=0;i<n.length;i++)for(Integer x:n[i]){if(m.get(x)==null)m.put(x,new ArrayList<>());m.get(x).add(i);w=x>w?x:w;}int[][]z=new int[w+1][];for(int i=0,j;i<w+1;i++){z[i]=new int[m.get(i).size()];j=0;for(int x:m.get(i))z[i][j++]=x;}return z;

详细

public static Integer[][] f(Integer[][]n)
{
    int w=0;
    Map<Integer,List<Integer>>m=new TreeMap<>();

    for(int i=0;i<n.length;i++)
    {
        for(Integer x : n[i])
        {
            if(m.get(x)==null) m.put(x,new ArrayList<Integer>());
            m.get(x).add(i);
            w=x>w?x:w;
        }
    }

    Integer[][]z=new Integer[w+1][];
    for(int i=0,j; i<w+1; i++)
    {
        z[i]=new Integer[m.get(i).size()];
        j=0;for(Integer x : m.get(i))z[i][j++]=x;
    }

    return z;
}

1
您真的需要Map吗?
Leaky Nun

0

Perl 5,48个字节

子程序:

{for$i(0..@_){map{push@{$b[$_]},$i}@{$_[$i]}}@b}

看到这样的动作:

perl -e'print "@$_$/" for sub{for$i(0..@_){map{push@{$b[$_]},$i}@{$_[$i]}}@b}->([3,2,5,0],[1,3],[2,1,0,4],[4,5,3],[6,6])'
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.