深度搜索列表


19

对于此挑战,当且仅当列表完全由整数和有效列表(递归定义\ o /)组成时,列表才被视为有效。对于此挑战,给定一个有效列表和一个整数,返回可以找到该整数的所有深度的列表。

让我们考虑list [1, [2, [3, [1, 2, 3], 4], 1], 1]和integer 1。然后,我们可以像这样绘制列表:

Depth 0 1 2 3
Num   1
        2
          3
            1
            2
            3
          4
        1
      1

您会注意到,它1显示在深处0, 1, 3。因此,您的输出应采用0, 1, 3某种合理的格式(顺序无关紧要)。

深度可以是0或1的索引,但是请在提交的内容中指定是哪一个。

测试用例(0索引)

对于列表[1,[2,[3,4],5,[6,7],1],[[[[5,2],4,[5,2]]],6],3]

1 -> [0, 1]
2 -> [1, 4]
3 -> [0, 2]
4 -> [2, 3]
5 -> [1, 4]
6 -> [1, 2]
7 -> [2]

对于列表[[[[[1],0],1],0],1]

0 -> 1, 3
1 -> 0, 2, 4

对于列表[11,22,[33,44]]

11 -> [0]
22 -> [0]
33 -> [1]
44 -> [1]

如果搜索词不存在于列表中的任何地方,则返回一个空列表。

负值和零值在输入列表和项中有效。


如果整数多次出现在一个深度处,我们是否只需要返回一次该深度数?
朱塞佩

@Giuseppe是的,没错。
HyperNeutrino

1
@Adám好吧,考虑到我的一个测试用例有零,没有。我还要补充一点,负整数是公平的游戏。
HyperNeutrino

1
如果可能会在测试案例中添加多位数的数字。
Zgarb

1
@KevinCruijssen是的,是的,不是,是的。因此,您可以将输入都当作字符串,并且可以按任何顺序显示深度,但不能多次显示。
HyperNeutrino

Answers:


7

Mathematica,25个字节

Tr/@Union[1^Position@##]&

(返回1-索引输出)

说明

                         test  {1, {2, {3, {1, 2, 3}, 4}, 1}, 1}
             Position[test,1]  {{1}, {2, 2, 2, 1}, {2, 3}, {3}}
           1^Position[test,1]  {{1}, {1, 1, 1, 1}, {1, 1}, {1}}
    Union[1^Position[test,1]]  {{1}, {1, 1}, {1, 1, 1, 1}}
Tr/@Union[1^Position[test,1]]  {1, 2, 4}

7

Haskell102 93 80 76字节

感谢Bruce Forte保存了一些字节,并感谢Laikoni保存了更多字节。

感谢4castle保存4个字节。

Haskell没有此类列表的数据类型,因此我创建了自己的列表。

这个解决方案是 1-indexed

import Data.List
data T=E Int|L[T]
E n%x=[0|x==n]
L s%x=nub$map(+1).(%x)=<<s

在线尝试!

首先,我定义(递归)数据类型 T

T具有类型E Int(类型的单个元素Int)或L[L](类型的列表T)。

(%)是函数,有2参数,在类型T,通过它,我们搜索的列表,x中,Int我们所期待的。

每当(%)发现单个元素时E n,它就会检查n是否相等,x并返回0True。

(%)应用于时L ss类型为[T]),它将(%)在的所有元素上运行s并递增结果(由于深度在不断增加,因为我们在内部查看s),并将结果串联起来。

nub 然后从列表中删除重复项

注意 import Data.List仅用于nub


我提出了一个非常相似的81字节解决方案:在线尝试!
Laikoni

@Laikoni非常好,您要亲自发布它,还是建议我更新我的?
H.PWiz

随时更新您的答案。:)
Laikoni

关于NB:我试图摆脱导入,但是消耗了88个字节:在线尝试!
Laikoni

2
您可以删除括号周围E nL s
4castle



4

果冻11 8字节

WẎÐĿċ€IT

在线尝试!

怎么运行的

WẎÐĿċ€IT  Main link. Left argument: A (array). Right argument: n (integer)

W         Wrap; yield [A].
  ÐĿ      Repeatedly apply the link to the left until the results are no longer
          unique. Yield the array of all unique results.
 Ẏ          Concatenate all elements at depth 1 in the array.
          The last array of the array of results is completely flat.
    ċ€    Count the occurrences of n in each intermediate result.
      I   Compute all forward differences.
       T  Truth; yield the array of all indices of non-zero differences.

运行示例

对于左参数

[1, [2, [3, [1, 2, 3], 4], 1], 1]

W 首先产生以下数组。

[[1, [2, [3, [1, 2, 3], 4], 1], 1]]

ẎÐĿ重复将深度为1的所有元素连接在一起,从而在每个步骤中将数组的深度减小1。这将产生以下一系列中间结果。

[
 [[1, [2, [3, [1, 2, 3], 4], 1], 1]],
 [ 1, [2, [3, [1, 2, 3], 4], 1], 1 ],
 [ 1,  2, [3, [1, 2, 3], 4], 1,  1 ],
 [ 1,  2,  3, [1, 2, 3], 4,  1, 1  ],
 [ 1,  2,  3,  1, 2, 3,  4,  1, 1  ]
]

对于右参数1ċ€计算每个中间结果中出现1的次数。

[0, 2, 3, 3, 4]

I 现在采取所有前向差异。

[2, 1, 0, 1]

非零差异对应于其中至少一个其他1被添加到深度1的步骤。因此,索引k处的非零差异表示深度k处存在1。查找所有真实元素的索引,产生所需结果:T

[1, 2, 4]

\ o /这是将Jelly与Python比较时的确切解决方案。好极了!:P
HyperNeutrino

4

R101 95 92100字节

f=function(L,n,d=0)unique(unlist(Map(function(x)if(n%in%unlist(x))"if"(is.list(x),f(x,n,d+1),d),L)))

在线尝试!

递归解;它的字节效率很低,但是R lists非常烦人。

基本上,需要L,并且对于每个元件xL,(其可以是一个listatomic一个元素的矢量),如果检查n%in% x,这时如果检查x是一个list。如果不是,那么x==n我们返回深度d;否则,我们递归调用fx,递增d

此,当然,返回list,这是我们unlistunique以确保正确的输出(返回整数深度的矢量); 传回NULL(空白清单)的invalid n

显然,%in%它不会list像我想的那样递归搜索,所以我必须要unlist(x)+8个字节:(


3

APL(Dyalog),39字节*

完整程序。提示输入列表,然后输入编号。将基于1的列表打印到STDOUT。

2÷⍨⍸∨⌿⍞⍷⎕FMTJSON'Compact'0⊢⎕

在线尝试!

 提示输入清单

 产生(分离0

⎕JSON⍠'Compact'0 用换行符转换为缩进的JSON字符串

⎕FMT 转换为矩阵(每行用换行符分隔的一行)

⍞⍷ 提示输入数字作为字符串,并指出其起始位置

∨⌿ 垂直OR缩减(即,它从哪一列开始)

 这些开始的索引

2÷⍨ 减半(级别以两个空格缩进)

 向下舍入(因为第一个数据列是第3列)


*在Dyalog Classic中,⎕U2378和计⎕OPT



2

JavaScript(ES6),79 68字节

f=(a,n,r=new Set,d=0)=>a.map(e=>e.map?f(e,n,r,d+1):e-n||r.add(d))&&r

返回一个Set。如果这是不可接受的,则以&&[...r]5字节为代价使用。


1

果冻 17  16 字节

⁴e®;©ȧ⁸ḟ⁴ẎµÐĿȧ®T’

一个完整的程序,它使用两个命令行参数列出该列表和一个要检查的元素,并打印出该元素存在的深度(如果有)。结果以1为索引。

在线尝试!

怎么样?

⁴e®;©ȧḟ⁴ẎµÐĿȧ®T’ - Main link: list, L
          µÐĿ    - loop, collecting updated values of L, until a fixed point is reached:
⁴                -   4th argument (2nd program input) = the number
 e               -   exists in (the current version of) L?
  ®              -   recall value from the register (initially 0)
   ;             -   concatenate the two
    ©            -   (copy this result to the register)
       ⁴         -   4th argument (2nd program input) again
      ḟ          -   filter out (discard any instances of the number)
     ȧ           -   logical and (non-vectorising)
        Ẏ        -   tighten (flatten the filtered L by one level to create the next L)
             ®   - recall value from the register
            ȧ    - logical and (non-vectorising)
              T  - truthy indexes (1-indexed)
               ’ - decrement (account for the leading zero from the initial register)

真好!有趣的事实:通过使用非常类似的方法,但是通过稍微改变事物的顺序,您可以获得8个字节。编辑方法实际上有点不同,nvm
HyperNeutrino


嗯,在撰写本文时发现了错误...目前正在删除。
乔纳森·艾伦,

嗯,我以某种方式改变了串联的顺序:/现在应该工作了
乔纳森·艾伦

1

JavaScript(ES6),73 74字节

f=(a,n,i=0,o={})=>a.map(e=>e.pop?f(e,n,i+1,o):e-n||o[i]++)&&Object.keys(o)

说明:

f=(a,                             //input array
   n,                             //input number to search
   i=0,                           //start at first level
   o={}                           //object to store the finds
  )=>
    a.map(                        //loop through the array
      e => e.pop ?                //is this element an array?
             f(e, n, i+1, o) :    //if so, recurse on it to the next level
             e-n || o[i]++        //otherwise, update o if element equals the number
    ) &&
    Object.keys(o)                //return o's keys

测试用例


尽管还没有测试用例,但我对问题的阅读表明,将其e[0]设为零是有效的,这将使您的测试无法进行。
尼尔,

@Neil,很好。现在更改e.pop为丢失一个字节。
里克·希区柯克,



0

八度126122字节

function n=r(p,t,l)n=[];if nargin<3
l=0;end
for x=p
if iscell(q=x{1})a=r(q,t,l+1);else
a=l*find(q==t);end
n=union(n,a);end

在线尝试!

为了提高可读性,我;在可能的地方用行尾替换了空格或。非高尔夫代码说明:

function n=r(p,t,l) % Declare function with list p, integer t and optional recursion depth l
n=[];
if nargin<3
    l=0;            % If l is not given (first iteration), set l to zero (or one for 1-indexing)
end
for x=p             % Loop over list
if iscell(q=x{1})   % If loop variable x is a cell, we must go down one level.
     a=r(q,t,l+1);  % So recurse to l+1.
else
    a=l*find(q==t); % Empty if q~=t (because find(false)==[], and l*[]==[]), else equal to l*1==l.
end
n=union(n,a);       % Append to list of levels, make sure we only get distinct values.
end

0

Java,154 + 19 = 173字节

import java.util.*;

Set<Long>f(List l,long n){Set s=new HashSet();if(l.contains(n))s.add(0l);for(Object o:l)if(o instanceof List)for(long d:f((List)o,n))s.add(d+1);return s;}

在线试用

脱胶方法

Set<Long> f(List l, long n) {
    Set s = new HashSet();
    if (l.contains(n))
        s.add(0l);
    for (Object o : l)
        if (o instanceof List)
            for (long d : f((List) o, n))
                s.add(d + 1);
    return s;
}
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.