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 code-golf, so shortest answer wins!
""
s? \$\endgroup\$0
s in the output. \$\endgroup\$