12
1
Given an array of non-negative integers, your task is to only keep certain elements of it, as described below.
Let's say the array is
[1, 3, 2, 4, 11, 5, 2, 0, 13, 10, 1].First get the first element of the array,
n. Keep the firstnelements and discard the next one (discard then+1th). The new array is[1, 2, 4, 11, 5, 2, 0, 13, 10, 1].Then, you grab the element following the one removed and do the exact same thing. Reapplying the process, we get
[1, 2, 11, 5, 2, 0, 13, 10, 1]You repeat the process until you arrive outside the array's bounds / there are no elements left in the array. We stop because
11is higher than the length of the array.Now you should output the result.
Input / output may be taken / provided in any standard form. The array will never be empty, and will only contain non-negative integers. All standard loopholes are forbidden.
This is code-golf so the shortest code in bytes wins!
Test Cases
Input --> Output [1, 2, 3, 4, 5] --> [1, 3, 4] [6, 1, 0, 5, 6] --> [6, 1, 0, 5, 6] [1, 3, 2, 4, 11, 5, 2, 0, 13, 10, 1] --> [1, 2, 11, 5, 2, 0, 13, 10, 1] [2, 2, 2, 2, 2, 2] --> [2, 2] [1, 2, 3, 1, 2, 3, 1, 2, 3] -> [1, 2] [3, 1, 2, 4, 0] --> []*
* The last test case involves 0, so I decided to post the process such that it is clearer:
[3, 1, 2, 4, 0] --> [3, 1, 2, 0] --> [1, 2, 0] --> [1, 0] --> [0] --> [] )
I have written all the test cases completely by hand, please notify me if you think there is a mistake! – None – 2017-08-21T19:06:19.193
1Why is
2removed in the first step instead of3? – Leaky Nun – 2017-08-21T19:09:26.227@LeakyNun My mistake. Correcting. Ping me if I made any other mistakes – None – 2017-08-21T19:10:06.280
suggested test case :
[1, 2, 3, 1, 2, 3, 1, 2, 3]– Rod – 2017-08-21T19:55:18.543@Rod Added. Thanks for the suggestion. – None – 2017-08-21T20:11:38.603
1So to clarify, when you move to your new "
n", you always start from the beginning of the array to keepnelements? Not (as I thought at first glance) keepnelements where the first element is thenyou are evaluating? – Brian J – 2017-08-22T13:10:48.990@BrianJ Yes, you always remove the element from the beginning. – None – 2017-08-22T15:50:09.103
Are you downgoats sock? – Christopher – 2017-09-14T21:35:30.470
@Christopher2EZ4RTZ No, that's Evil Sheep. :P (Or TF2Goat)
– totallyhuman – 2017-09-14T21:39:29.867