49
4
Given an Integer array:
- Start from the first number
- Jump forward n positions where n is the value of the current position
- Delete the current position, making what was the next position the current position.
- Goto step 2 until there is one number remaining
- Print that number
Rules
The array wraps-around (the next number after the last number in the array is the first number).
A zero removes itself (Obviously).
Negative numbers are not allowed as input.
Test Cases
[1] => 1
[1,2] => 1
[1,2,3] => 3
[1,2,2] => 1
[1,2,3,4] => 1
[6,2,3,4] => 4
[1,2,3,4,5] => 5
[0,1] => 1
[0,0,2,0,0] => 0
Step-by-step example
[1,4,2,3,5]
^ start from the first position
^ jump 1 position (value of the position)
[1, 2,3,5] remove number in that position
^ take next position of the removed number (the 'new' 'current' position)
^ jump 2 positions
[1, 2,3 ] remove number in that position
^ take next position (looping on the end of the array)
^ jump 1 position
[1, 3 ] remove number in that position
^ take next position (looping)
^ jump 3 positions (looping on the end of the array)
[ 3 ] remove number in that position
print 3
Example #2
[4,3,2,1,6,3]
^ start from the first position
^ jump 4 positions
[4,3,2,1, 3] remove number in that position
^ take next position
^ jump 3 positions
[4,3, 1, 3] remove number in that position
^ take next position
^ jump 1 positions
[4,3, 1 ] remove number in that position
^ take next position
^ jump 4 positions
[4, 1 ] remove number in that position
^ take next position
^ jump 1 position
[ 1 ] remove number in that position
print 1
This is code-golf, the shortest answer in bytes wins!
14Nice first challenge! – Luis Mendo – 2017-12-12T13:49:20.387
2@LuisMendo Yes.. the "skip like a..." challenges – J42161217 – 2017-12-12T13:58:37.770
2@Jenny_mathy I didn't think there would be a similar one, but as Luis said, the wraparound array makes an interesting challenge for golfing. I think :/ – workoverflow – 2017-12-12T14:00:15.497
1Related, but I don't think it's a duplicate – Luis Mendo – 2017-12-12T14:09:59.270
1closely related (dupe?) – Erik the Outgolfer – 2017-12-12T14:11:46.790
3@EriktheOutgolfer Not really a dupe. The elements there are indistinguishable and the step size is fixed. Luis's is much closer, but still sufficiently different I think. – Martin Ender – 2017-12-12T14:12:16.947
3Does it need to actually print the final number, or can it just return it? Does it need to actually return the number, or can it just operate on the array in-place so the after the function is run, the array contains just the number? – Reinstate Monica -- notmaynard – 2017-12-12T16:48:10.297
1
@iamnotmaynard, see this consensus.
– Shaggy – 2017-12-12T21:35:27.223