Skip like a frog!
JavaScript (ES6), 45 bytes
f=(a,k=0,x=a[k])=>1/x?f(a.splice(x,1)&&a,x):a
Test cases
f=(a,k=0,x=a[k])=>1/x?f(a.splice(x,1)&&a,x):a
console.log(JSON.stringify(f([1, 2, 3, 4, 5]))) // --> [1, 3, 4]
console.log(JSON.stringify(f([6, 1, 0, 5, 6]))) // --> [6, 1, 0, 5, 6]
console.log(JSON.stringify(f([1, 3, 2, 4, 11, 5, 2, 0, 13, 10, 1]))) // --> [1, 2, 11, 5, 2, 0, 13, 10, 1]
console.log(JSON.stringify(f([2, 2, 2, 2, 2, 2]))) // --> [2, 2]
console.log(JSON.stringify(f([1, 2, 3, 1, 2, 3, 1, 2, 3]))) // -> [1, 2]
console.log(JSON.stringify(f([3, 1, 2, 4, 0]))) // --> []
Haskell, 50 bytes
g.pure.(0:)
is an anonymous function taking and returning a list of Int
s, use as (g.pure.(0:))[1,2,3,4,5]
.
g.pure.(0:)
g(a,_:b:c)=g$splitAt b$a++b:c
g(a,_)=a
Try it online!
How it works
- The function
g
takes a tuple argument representing a split list.a
is the list of initial elements kept at the previous step,_
is the element to be discarded,b
is the next element to be used as a length, andc
is the remaining elements.- If there are enough elements in the second part of the tuple to select a
b
, then a new split is performed andg
recurses. Otherwise, it halts witha
as the result.
- If there are enough elements in the second part of the tuple to select a
- The anonymous function
g.pure.(0:)
starts it all by callingg
with the tuple([],0:l)
, wherel
is the input and0
gets immediately discarded byg
.pure
here uses theApplicative
instance for (binary) tuples, and with the result type([Int],[Int])
conveniently puts its argument as the second element in a tuple with[]
as first element.
Python 3, 59 bytes
f=lambda a,i=0:f(a[:a[i]]+a[a[i]+1:],a[i])if i<len(a)else a
Try it online!