Write a function @jumps(a,k)@ that receives a list of strictly positive integers and an integer , and returns the list of jumped elements in starting from position .
The list of jumped elements in list
starting from position
is defined as the list formed by element a[k] followed by
the list of jumped elements starting from position k+a[k].
If
is larger than the size of
,
the result is an empty list.
For instance, given the list
[2 1 3 5 7 2 9 5 2 4 8]
and starting position
,
the resulting list is [3 2 5]. That is, we start at
position
finding element
.
We advance
positions and reach element
.
We thus advance
more positions and land on element
.
We try to advance
positions but reach the end of the list, so we stop.
If you want to test your program locally, remember to include the following lines at the end of the file:
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
>>> jumps([2, 1, 3, 5, 7, 4, 9, 5, 2, 5, 8], 0) [2, 3, 4, 5] >>> jumps([2, 1, 3, 5, 7, 4, 9, 5, 2, 5, 8], 1) [1, 3, 4, 5] >>> jumps([2, 1, 3, 5, 7, 4, 9, 5, 2, 5, 8], 3) [5, 2, 8] >>> jumps([2, 1, 3, 5, 7, 4, 9, 5, 2, 5, 8], 20) [] >>> jumps([2, 1, 3], 1) + jumps([2, 1, 3, 5, 7], 4) [1, 3, 7]