像兔子一样跳过!


41

给定任何合理格式的非负整数列表,请对其进行迭代,并跳过与您踩到的每个整数一样多的元素。


这是一个工作示例:

[0, 1, 0, 2, 5, 1, 3, 1, 6, 2] | []
 ^ First element, always include it
[0, 1, 0, 2, 5, 1, 3, 1, 6, 2] | [0]
    ^ Skip 0 elements
[0, 1, 0, 2, 5, 1, 3, 1, 6, 2] | [0, 1]
          ^ Skip 1 element
[0, 1, 0, 2, 5, 1, 3, 1, 6, 2] | [0, 1, 2]
                   ^ Skip 2 elements
[0, 1, 0, 2, 5, 1, 3, 1, 6, 2] | [0, 1, 2, 3]
Skip 3 elements; you're done

另一个可行的示例,不是所有等式的增量:

[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] | []
 ^ First element, always include it
[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] | [4]
                ^ Skip 4 elements
[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] | [4, 3]
                            ^ Skip 3 elements
[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] | [4, 3, 3]
                                        ^ Skip 3 elements
[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] | [4, 3, 3, 4]
Skip 4 elements; you're done

一个越界示例:

[0, 2, 0, 2, 4, 1, 2] | []
^ First element, always include it
[0, 2, 0, 2, 4, 1, 2] | [0]
    ^ Skip 0 elements
[0, 2, 0, 2, 4, 1, 2] | [0, 2]
             ^ Skip 2 elements
[0, 2, 0, 2, 4, 1, 2] | [0, 2, 4]
Skip 4 elements; you're done (out of bounds)

规则

  • 这些当中,您可能不会使用任何无聊的作弊方法,它们会使挑战变得无聊而无趣。
  • 您应该只退还/打印最终结果。STDERR输出被忽略。
  • 您可能无法以任何底数的数字字符串形式获得输入(例如,第一种情况为“ 0102513162”)。
  • 您必须使用从左到右的顺序进行输入。
  • 就像在工作示例中一样,如果您超出范围,则执行将终止,就像在其他情况下一样。
  • 您应该使用0跳过0个元素。
  • 给定空列表([]),您应该返回[]

测试用例

[]                                                     => []
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]                     => [0, 1, 3, 7]
[5, 1, 2, 3, 4, 5, 2, 1, 2, 1, 0, 0]                   => [5, 2, 1, 0]
[0, 1, 0, 2, 5, 1, 3, 1, 6, 2]                         => [0, 1, 2, 3]
[4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2] => [4, 3, 3, 4]
[0, 2, 0, 2, 4, 1, 2]                                  => [0, 2, 4]

这是,所以最短的答案会成功!


1
在数组中尾随零可以吗?将节省我
〜18

@EriktheOutgolfer我们可以输出一个字符串数组并包含尾随的空字符串吗?
TheLethalCoder

1
@TheLethalCoder对不起,我说不,因为那是不合理的imo ...你不能删除尾随的""s吗?
大公埃里克(Erik the Outgolfer)'17年

2
@RomanGräf很抱歉,但是不能,因为在某些情况下您0的输出中应该包含尾随s,所以这太含糊了。
大公埃里克(Erik the Outgolfer)'17年

Answers:



13

Python 2中49个44 * 41字节

划掉44仍然是常规44 :(

* -3感谢@ ASCII-only

l=input()
while l:print l[0];l=l[l[0]+1:]

在线尝试!

按照聊天中允许的OP来打印结果,并以换行符分隔。我认为作为非递归完整程序,它不会变得更短。


这是如何运作的?

  • l=input() -从标准输入中读取列表。

  • while l: -滥用空列表在Python中是虚假的事实,一直循环到列表为空。

  • print l[0]; -打印列表的第一个元素。

  • l=l[l[0]+1:]-“像兔子一样跳”-修剪l[0]+1列表中的第一个。

让我们举个例子

给定列表[5, 1, 2, 3, 4, 5, 2, 1, 2, 1, 0, 0]作为输入,代码将执行以下操作(根据上述说明)-打印数组的第一项:5,剪裁前6个:[2, 1, 2, 1, 0, 0]。然后,我们打印2和修剪前3: [1,0,0]。同样,我们输出1,裁剪第一个2,得到[0]。当然,0将打印并终止程序。




9

JavaScript(ES6),42 39 35字节

a=>a.map((n,i)=>a.splice(i+1,n))&&a

let f = 
a=>a.map((n,i)=>a.splice(i+1,n))&&a

console.log(f([]))                                                     // => []
console.log(f([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))                     // => [0, 1, 3, 7]
console.log(f([5, 1, 2, 3, 4, 5, 2, 1, 2, 1, 0, 0]))                   // => [5, 2, 1, 0]
console.log(f([0, 1, 0, 2, 5, 1, 3, 1, 6, 2]))                         // => [0, 1, 2, 3]
console.log(f([4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2])) // => [4, 3, 3, 4]
console.log(f([0, 2, 0, 2, 4, 1, 2]))                                  // => [0, 2, 4]

旧解决方案39字节

a=>a.map(n=>i--||r.push(i=n),r=i=[])&&r

-3个字节,感谢@ThePirateBay


39字节a=>a.map(n=>i--||r.push(i=n),r=i=[])&&r


8

Mathematica,46 44字节

SequenceCases[#,{x_,y___}/;Tr[1^{y}]<=x:>x]&

备择方案:

SequenceCases[#,{x_,y___}/;x>=Length@!y:>x]&
SequenceCases[#,l:{x_,___}/;x>Tr[1^l]-2:>x]&

7

C#,68个字节

a=>{for(int i=0;i<a.Count;i+=a[i]+1)System.Console.Write(a[i]+" ");}

在线尝试!

完整/格式化版本:

namespace System
{
    class P
    {
        static void Main()
        {
            Action<Collections.Generic.List<int>> f = a =>
            {
                for (int i = 0; i < a.Count; i += a[i] + 1)
                    System.Console.Write(a[i] + " ");
            };

            f(new Collections.Generic.List<int>() { });Console.WriteLine();
            f(new Collections.Generic.List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });Console.WriteLine();
            f(new Collections.Generic.List<int>() { 5, 1, 2, 3, 4, 5, 2, 1, 2, 1, 0, 0 });Console.WriteLine();
            f(new Collections.Generic.List<int>() { 0, 1, 0, 2, 5, 1, 3, 1, 6, 2 });Console.WriteLine();
            f(new Collections.Generic.List<int>() { 4, 5, 1, 3, 8, 3, 0, 1, 1, 3, 1, 2, 7, 4, 0, 0, 1, 2 });Console.WriteLine();
            f(new Collections.Generic.List<int>() { 0, 2, 0, 2, 4, 1, 2 });Console.WriteLine();

            Console.ReadLine();
        }
    }
}

返回列表的时间更长,为107个字节。

a=>{var l=new System.Collections.Generic.List<int>();for(int i=0;i<a.Count;i+=a[i]+1)l.Add(a[i]);return l;}

2
为什么有人对此表示反对?
TheLethalCoder

舍入分数并达到完美的5k?
托马斯·阿尤布

@ThomasAyoub我们只能假设它是强迫症患者。
TheLethalCoder

6

外壳8 6字节

←TU¡Γ↓

在线尝试!

-2个字节(还有一个全新的解决方案),感谢Leo!

说明

我正在使用列表模式匹配功能Γ。它需要一个功能f,并与头列表x和尾部xs,并适用fxxs。如果列表为空,则Γ返回与其类型一致的默认值,在这种情况下为空列表。我们认为f,从中删除了x元素xs。然后对该函数进行迭代,并将得到的元素收集在一个列表中。

←TU¡Γ↓  Implicit input, e.g. [0,2,0,2,4,1,2]
    Γ↓  Pattern match using drop
   ¡    iterated infinitely: [[0,2,0,2,4,1,2],[2,0,2,4,1,2],[4,1,2],[],[],[],...
  U     Cut at first repeated value: [[0,2,0,2,4,1,2],[2,0,2,4,1,2],[4,1,2],[]]
 T      Transpose: [[0,2,4],[2,0,1],[0,2,2],[2,4],[4,1],[1,2],[2]]
←       First element: [0,2,4]

您可以删除ø的默认值,并且一切仍然可以神奇地进行:)
Leo


@Leo哦,哇,这很聪明!
Zgarb

你为什么要这么做?
暴民埃里克(Erik the Outgolfer)

@ErikTheOutgolfer那是一个错误(我在电话上,显然是无意中推了些东西)。我正在尝试撤消它
Zgarb


5

Pyth,22个字节

VQ aY.(Q0VeY .x.(Q0 ;Y

删除了一个无用的字节


我在那里看到23个字节。
暴民埃里克(Erik the Outgolfer)'17年

错别字:)对不起...
Dave

3
我不确定您为什么要投反对票。当您编辑修正答案时,这可能会触发“自动投反对票”。这种自动降票的原因令人困惑和可怕,但是如果系统根据其启发式方法将您的答案视为“低质量”,则会发生这种情况。也可能有人不喜欢您的答案,但目前我没有发现任何问题,因此我不确定为什么会这样。
小麦巫师

我很高兴您正在使用Pyth!
isaacg


3

视网膜,36字节

字节数假定为ISO 8859-1编码。

.+
$*
((1)*¶)(?<-2>1*¶)*
$1
%M`.
0$

输入和输出以换行符分隔,后跟换行符。

在线尝试!(使用逗号代替换行符,以方便使用测试套件。)


3

Brain-Flak,64位元组

([]){{}(({})<>)<>{({}[()]<{}>)}{}([])}{}<>([]){{}({}<>)<>([])}<>

在线尝试!

([]){{}                          ([])}{}                         # Until the stack is empty
       (({})<>)<>                                                # Copy TOS to off stack
                 {({}[()]<{}>)}{}                                # Pop TOS times
                                        <>([]){{}({}<>)<>([])}<> # Reverse off stack

7
哇靠!我写了一个解决方案,然后向下滚动以发布它,但事实证明,我们逐字节编写了完全相同的解决方案!甚至像({}[()]<{}>)vs 这样的小细节({}<{}>[()])都一样!真是巧合!
DJMcMayhem

@DJMcMayhem偷走了所有著名的XD
Christopher

我还为字节相同的解决方案制作了一个字节,但我将其压缩了4个字节。只是一些延迟的比赛而已:)
小麦巫师

2

Mathematica,64个 50字节

±x_List:=Prepend[±Drop[x,1+#&@@x],#&@@x]
±_=±{}={}

我忍不住进一步打高尔夫球这个整洁的代码。我的答案如下。
威兹德先生

2

C#(.NET Core),68字节

n=>{var t="";for(int i=0;i<n.Length;i+=n[i]+1)t+=n[i]+" ";return t;}

在线尝试!

将输入作为整数数组,返回包含非跳过值的字符串。


做到这一点的好方法,并且与打印的数量相同。
TheLethalCoder

我喜欢简单的解决方案。还是得学习LINQ不过,因为我已经看到,缩短了这么多的C#lambda表达式..
jkelm

缩短它是因为您可以在大多数时间隐式返回。虽然这是隐式返回using System.Linq;和正常循环之间的折腾。
TheLethalCoder

2

R,58个字节

f=function(x,p=1){cat(z<-x[p]);if(p+z<sum(x|1))f(x,p+z+1)}

递归函数。以向量x为参数并初始化一个指针p。这将打印的相应条目x,检查是否p+x[p]超出范围,否则,调用新指针的函数。

f=function(x,p=1,s=x[1])`if`((z<-x[p]+p+1)>sum(x|1),s,f(x,z,c(s,x[z])))

这是一个可比的解决方案,它返回适当的向量而不是打印数字。


输入numeric(0)什么呢?亦称空数组。
朱塞佩

@Giuseppe当我在电脑后面时,我会看看一下
JAD


2

Java(OpenJDK 8),53字节

感谢@ PunPun1000和@TheLethalCoder

a->{for(int n=0;;n+=1+a[n])System.out.println(a[n]);}

在线尝试!


像在C#答案中那样打印结果是否可以为您节省任何费用?
TheLethalCoder

@TheLethalCoder生病尝试
罗马格拉夫

您可以n进入循环来保存一个字节吗?
TheLethalCoder

另外,目前看来这似乎行不通。
TheLethalCoder

您错过了paren (a[n+=1+a[n]]。函数在输出正确的值后也会引发错误,我不知道是否允许这样做(这个问题确实对标准错误没有说明)。如果这是故意的,那么您可以n<a.length在for循环中删除。最终,即使有了括号,TIO代码也无法按原样运行。该功能应为a Consumer<int[]>并使用func.accept(test)
PunPun1000 '17

2

爱丽丝,15字节

/$.. \h&
\I@nO/

在线尝试!

输入和输出以换行符分隔的十进制整数列表。

说明

/   Switch to Ordinal mode.
I   Read a line.
.   Duplicate it.
n   Logical NOT (gives truthy if we're at EOF).
/   Switch to Cardinal.
    The IP wraps around to the left.
\   Switch to Ordinal.
$@  Terminate the program if we're at EOF.
.   Duplicate the input line again.
O   Print it.
\   Switch to Cardinal.
h   Increment the value.
&   Store the result in the iterator queue.
    The program wraps around to the beginning.

在迭代器队列中存储整数n会使下一个命令执行n次。像/这样的镜像不是命令,因此下一个命令将是I。因此,如果我们只是读取并打印一个值x,那么我们将在下一次迭代时读取x + 1个值,其中最后一个值最终位于堆栈顶部。这将跳过所需的数字列表元素。


2

Mathematica,37(30?)

user202729的优良方法的进一步打法。

±{a_,x___}={a}~Join~±{x}~Drop~a
±_={}

该规则似乎并未明确指定输出格式,因此可能:

±{a_,x___}=a.±{x}~Drop~a
±_={}

第二个函数的输出看起来像:0.2.4.{}—特别{}是仍然返回一个空集,符合最终规则。


1
±Drop[{x},a]可能是±{x}~Drop~a因为±优先级低于Infix
JungHwan Min

@JungHwanMin我错过了;谢谢!
威兹德先生


2

Brain-Flak64 60字节

根据0的想法保存4个字节

([]){{}(({})<>())<>{({}[()]<{}>)}{}([])}{}<>{({}[()]<>)<>}<>

在线尝试!

带注释

([]){{}            #{Until the stack is empty}
  (({})<>())<>     #{Put n+1 to the offstack}
  {({}[()]<{}>)}{} #{Remove n items from the top}
([])}{}            #{End until}
<>                 #{Swap stacks}
{({}[()]<>)<>}<>   #{Move everything back onto the left stack decrementing by 1}


1

Python 2.4,85个字节

没有机会用它赢得python的胜利,但是我喜欢oneliners,而这可能对其他人很有趣。
事实证明,在理解内访问建筑列表是一个奇妙的魔术,但它仅在2.4中有效,并且在<= 2.3中具有某些编辑
locals()['_[1]']功能。Python创建_[1]列表的秘密名称,同时创建它并将其存储在中locals。同样的名字_[2]_[3]...用于嵌套列表。

lambda n:[j for i,j in enumerate(n)if i==len(locals()['_[1]'])+sum(locals()['_[1]'])]

因此,它计算已添加元素的数量及其总和。结果是下一个所需元素的索引。
我认为,应该有一种避免枚举的方法。像由索引直接访问输入数组:[ n[len(locals()['_[1]'])+sum(locals()['_[1]'])] for ... ]。但是我想不出一种紧凑的方法来保护它免受超出范围的索引的影响(同时保持它的线性)

在此处输入图片说明


1

迅捷,63字节

func a(d:[Int]){var i=0;while i<d.count{print(d[i]);i+=d[i]+1}}

这是我第一次参加比赛,因此我对规则没有100%的把握,但希望这个答案就足够了。我不太确定如何将输入输入系统的规则。如果允许我在某个地方可以返回输入的函数,我的回答更短。


欢迎来到PPCG!默认规则是您既可以将代码用作完整程序,则可以在STDIN中输入(通常),通常将其输出到STDOUT,也可以是函数,因此可以(通常)从函数参数输入,而通常从函数返回。
斯蒂芬

@StepHen-谢谢!我想那会使我的其他版本无效。期待贡献更多!
AnonymousReality

1

Perl 6,31个字节

{(@_,{.[1+.[0]..*]}...^0)[*;0]}

测试一下

展开:

{  # bare block lambda with implicit parameter 「@_」
  (
    # generate a sequence

    @_,

    {
      .[ # index into previous value in the sequence
        1 + .[0]  # start by skipping one plus the first element
                  # of the previous value in the sequence
        ..  *     # use that to create a Range with no end
      ]
    }

    ...^  # keep doing that until: (and throw away last value)
    0     # it generates an empty list

  )[ *; 0 ]  # from every value in the sequence, get the first element
}

为了帮助理解代码的工作方式,如果没有[*;0]这样的话,将生成如下序列:

[0, 1, 0, 2, 5, 1, 3, 1, 6, 2],
   (1, 0, 2, 5, 1, 3, 1, 6, 2),
         (2, 5, 1, 3, 1, 6, 2),
                  (3, 1, 6, 2)

1

果冻,8 字节

ḢṄ‘ṫ@µL¿

一个完整的程序,打印结果,每个结果后跟换行符(空列表不产生任何输出)。

在线尝试!

怎么样?

ḢṄ‘ṫ@µL¿ - Main link: list of non-negative integers  e.g. [2,5,4,0,1,2,0]
       ¿ - while:           Iteration:  1                  2             3          4        5
      L  -   length (0 is falsey)       7                  4             3          1        0
     µ   - ...do:                                                                            stop
Ḣ        -   head (pop & modify)        2 ([5,4,0,1,2,0])  0 ([1,2,0])   1 ([2,0])  0 ([0])
 Ṅ       -   print it (and yield it)   "2\n"              "0\n"         "1\n"      "0\n"
  ‘      -   increment                  3                  1             2          1
   ṫ@    -   tail from index            [0,1,2,0]          [1,2,0]      [0]         []
         -
         -                       i.e. a resulting in the printing of: '''2
                                                                         0
                                                                         1
                                                                         0
                                                                         '''

最后一个果冻的答案!顺便说一句,我可以做到7字节。
暴民埃里克'17

而且我还有一个18字节的列表返回函数。
暴民埃里克(Erik the Outgolfer)

1

Python 3,35个字节

f=lambda h=0,*t:t and[h,*f(*t[h:])]

在线尝试!

用您的输入f(*l)在哪里运行它l。可以说可以扩展输入规则,但是我只是喜欢高级拆包。




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.