# Skip like a rabbit!

Given a list of non-negative integers in any reasonable format, iterate over it, skipping as many elements as every integer you step on says.

Here is a worked example:

[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

Another worked example, not so all-equal-deltas:

[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

An out-of-bounds example:

[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)

# Rules

• You may not use any boring cheat among these ones, they make the challenge boring and uninteresting.
• You should only return/print the final result. STDERR output is ignored.
• You may not get the input as a string of digits in any base (e.g. "0102513162" for the first case).
• You must use left-to-right order for input.
• As in the worked examples, if you go out of bounds, execution terminates as if otherwise.
• You should use 0 for skipping 0 elements.
• Given the empty list ([]) as input, you should return [].

# Test cases

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

This is , so shortest answer wins!

• Is it okay to have trailing zeros in my array? would save me ~18 bytes – Roman Gräf Jul 28 '17 at 11:15
• @EriktheOutgolfer Could we output a string array and have trailing empty strings? – TheLethalCoder Jul 28 '17 at 11:17
• @TheLethalCoder Sorry I'd say no since that's not reasonable imo...can't you just remove trailing ""s? – Erik the Outgolfer Jul 28 '17 at 11:25
• @RomanGräf Sorry but no, that would be too ambiguous since there are cases you should have trailing 0s in the output. – Erik the Outgolfer Jul 28 '17 at 11:25

# Python 2, 36 bytes

f=lambda x:x and x[:1]+f(x[x[0]+1:])

Try it online!

• I was expecting to get outgolfed, but not that badly :) – Mr. Xcoder Jul 28 '17 at 11:31
• Can't you do x[0] instead of x[:1]? – Erik the Outgolfer Jul 28 '17 at 11:32
• @EriktheOutgolfer yes, but it need to be a list, so it would be [x[0]] – Rod Jul 28 '17 at 11:32
• @Rod You're not saving any bytes with x[:1] anyways...f=lambda x:x and[x[0]]+f(x[x[0]+1:]) – Erik the Outgolfer Jul 28 '17 at 11:33

# Python 2, 49 44* 41 bytes

Crossed out 44 is still regular 44 :(

* -3 thanks to @ASCII-only.

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

Try it online!

Prints the results separated by a newline, as the OP allowed in chat. I don't think it can get any shorter as a non-recursive full program.

# How does this work?

• l=input() - Reads the list from the standard input.

• while l: - Abuses the fact that empty lists are falsy in Python, loops until the list is empty.

• print l[0]; - Prints the first element of the list.

• l=l[l[0]+1:] - "Skips like a rabbit" - Trims the first l[0]+1 from the list.

### Let's take an example

Given the list [5, 1, 2, 3, 4, 5, 2, 1, 2, 1, 0, 0] as input, the code performs the following (according to the explanation above) - Prints the first item of the array: 5, trim the first 6: [2, 1, 2, 1, 0, 0]. We then print 2 and trim the first 3: [1,0,0]. Likewise, we output 1, crop the first 2, and we get [0]. Of course, 0 is printed and the program terminates.

# Haskell, 29 27 26 bytes

j(x:y)=x:j(drop x y)
j x=x

Saved 1 byte thanks to Zgarb.

Try it online.

• f x=x on the second line saves a byte. – Zgarb Jul 28 '17 at 12:27

# JavaScript (ES6), 4239 35 bytes

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]

### Old Solution 39 Bytes

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

-3 bytes thanks to @ThePirateBay

• 39 bytes a=>a.map(n=>i--||r.push(i=n),r=i=[])&&r – user72349 Jul 28 '17 at 11:47

# 05AB1E, 10 9 bytes

[¬Dg>#=ƒ¦

Uses the 05AB1E encoding. Try it online!

• Yeah, that is way better than what I was thinking. – Magic Octopus Urn Jul 28 '17 at 13:57

## Mathematica, 46 44 bytes

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

Alternatives:

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

# C#, 68 bytes

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

Try it online!

Full/Formatted version:

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();

}
}
}

Returning a list is longer at 107 bytes.

• Why has someone downvoted this? – TheLethalCoder Jul 28 '17 at 15:15
• To round your score and make a perfect 5k? – Thomas Ayoub Jul 31 '17 at 10:13
• @ThomasAyoub We can only assume it was someone with OCD yes. – TheLethalCoder Jul 31 '17 at 10:20

## Husk, 8 6 bytes

←TU¡Γ↓

Try it online!

-2 bytes (and a completely new solution idea) thanks to Leo!

## Explanation

I'm using the list pattern match function Γ. It takes a function f and a list with head x and tail xs, and applies f to x and xs. If the list is empty, Γ returns a default value consistent with its type, in this case an empty list. We take f to be , which drops x elements from xs. This function is then iterated and the resulting elements are collected in a list.

←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]
• You can drop the default value of ø, and everything will still magically work :) – Leo Jul 28 '17 at 13:36
• Or, for even less bytes, tio.run/##yygtzv7//1HbhJDQQwvPTX7UNvn////RBjpGOiBsomOoYxQLAA – Leo Jul 28 '17 at 15:34
• @Leo Oh wow, that's clever! – Zgarb Jul 28 '17 at 17:32
• Why did you CW this? – Erik the Outgolfer Jul 28 '17 at 17:36
• @ErikTheOutgolfer That was a mistake (I'm on my phone and apparently pushed something by accident). I'm trying to undo it... – Zgarb Jul 28 '17 at 17:42

# Python 2, 59 55 bytes

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

Try it online!

• You can use l[i:i+l[i-1]]=[] instead del l[i:i+l[i-1]] to save a byte – Rod Jul 28 '17 at 11:11
• 56 bytes – ASCII-only Jul 28 '17 at 11:18

# Pyth, 22 Bytes

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

Removed a useless byte

• I see 23 bytes there. – Erik the Outgolfer Jul 28 '17 at 13:16
• Typo :) sorry... – Dave Jul 28 '17 at 13:17
• I'm not sure why you have a down vote. There is a possibility that when you edited fixing your answer this triggered an "automatic down vote". The reasons for this automatic downvote are confusing and terrible but it happens if the system considers your answer to be "low quality" based on it heuristics. Its also possible that someone didn't like your answer, but I don't see anything wrong with it at the moment so I'm not sure why that would be the case. – Wheat Wizard Jul 28 '17 at 13:23
• I'm glad you're using Pyth! – isaacg Jul 31 '17 at 3:01

# Python 2, 6042 41 bytes

-18 bytes thanks to Luis Mendo
-1 byte thanks to Jonathan Frech

x=input()
i=0
while 1:print x[i];i-=~x[i]

Try it online!

• i-=~x[i] is one byte shorter than i+=1+x[i]. – Jonathan Frech Oct 4 '17 at 17:15

## Retina, 36 bytes

Byte count assumes ISO 8859-1 encoding.

.+
$* ((1)*¶)(?<-2>1*¶)*$1
%M.
0$Input and output are linefeed-separated with a trailing linefeed. Try it online! (Uses commas instead of linefeeds to allow for convenient test suites.) # Brain-Flak, 64 bytes ([]){{}(({})<>)<>{({}[()]<{}>)}{}([])}{}<>([]){{}({}<>)<>([])}<> Try it online! ([]){{} ([])}{} # Until the stack is empty (({})<>)<> # Copy TOS to off stack {({}[()]<{}>)}{} # Pop TOS times <>([]){{}({}<>)<>([])}<> # Reverse off stack • Holy crap! I wrote up a solution, and then scrolled down to post it, but it turns out we wrote the exact same solution byte-for-byte! Even minor details like ({}[()]<{}>) vs ({}<{}>[()]) were the same! What a coincidence! – James Jul 28 '17 at 16:54 • @DJMcMayhem stealing all the fame XD – Christopher Jul 28 '17 at 17:08 • I also made a byte for byte identical solution, but I golfed it down 4 bytes. Just some delayed competition :) – Wheat Wizard Oct 5 '17 at 4:21 # Mathematica, 64 50 bytes ±x_List:=Prepend[±Drop[x,1+#&@@x],#&@@x] ±_=±{}={} • I couldn't resist further golfing this neat code; my answer is below. – Mr.Wizard Jul 28 '17 at 18:29 # C# (.NET Core), 68 bytes n=>{var t="";for(int i=0;i<n.Length;i+=n[i]+1)t+=n[i]+" ";return t;} Try it online! Takes input as an array of integers, returns a string containing the non-skipped values. • Nice way to do it and comes in at the same count as printing. – TheLethalCoder Jul 28 '17 at 11:49 • I love the simple solutions. Still gotta learn LINQ though, as I have seen that shorten so many c# lambdas.. – jkelm Jul 28 '17 at 11:52 • Shortens it because you can implicit return most of the time. Though it is a toss up between implicit return with using System.Linq; and a normal loop. – TheLethalCoder Jul 28 '17 at 11:54 # R, 58 bytes f=function(x,p=1){cat(z<-x[p]);if(p+z<sum(x|1))f(x,p+z+1)} Recursive function. Takes a vector x as argument and intiates a pointer p. This prints the corresponding entry of x, checks if p+x[p] would go out of bounds, and if not, calls the function for the new pointer. 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]))) This is a comparable solution that returns a proper vector instead of printing the digits. • what about an input of numeric(0)? aka empty array. – Giuseppe Jul 28 '17 at 17:23 • @Giuseppe I'll take a look at it when I'm behind my pc – JAD Jul 28 '17 at 17:35 • 55 bytes – Giuseppe Oct 4 '17 at 17:24 # Java (OpenJDK 8), 53 bytes Thanks to @PunPun1000 and @TheLethalCoder a->{for(int n=0;;n+=1+a[n])System.out.println(a[n]);} Try it online! • Would printing the results, like in my C# answer, save you anything? – TheLethalCoder Jul 28 '17 at 11:42 • @TheLethalCoder Ill try – Roman Gräf Jul 28 '17 at 11:43 • Can you save a byte by moving n into the loop? – TheLethalCoder Jul 28 '17 at 12:45 • Plus this doesn't seem to work at the moment. – TheLethalCoder Jul 28 '17 at 12:46 • You're missing a paren after the (a[n+=1+a[n]]. Function also throws an error after outputting the correct value, I don't know the concensus on whether this is allowed or not (the question does say anything to standard error is ignore). If that was the intention, then you can remove the n<a.length in the for loop. Finally the TIO code doesn't run as is, even with the paren. The function should be a Consumer<int[]> and use func.accept(test) – PunPun1000 Jul 28 '17 at 12:46 ## Alice, 15 bytes /$.. \h&
\I@nO/

Try it online!

Input and output a linefeed-separated lists of decimal integers.

### Explanation

/   Switch to Ordinal mode.