Drunken Knight¶
Consider a non-empty list of positive int where each number represents a position in the same list. Let’s define a “jump” from position i over a list L as moving to the position L[i]. Thus, jumping over a list L from a given initial position i0 is doing jumps starting at i0 until it jumps outside the range of L or enters a neverending loop.
For example, given the list l = [4, 2, 0, 1] and initial position 3 the jumping will do the following sequence: 3, 1, 2, 0, 4. If the list was l = [3, 2, 0, 1] it would the neverending sequence 3, 1, 2, 0, 3, 1, 2, …
Write the following function in the module with the same name (file drunken_knight.py):
- drunken_knight(L, i0)¶
such that given
Per exemple:
>>> l = [3, 2, 0, 1] >>> drunken_knight(l, 3) >>> l == [-1,-1,-1,-1] True >>> l = [1, 2, 3, 4] >>> drunken_knight(l, 0) >>> l == [-1,-1,-1,-1] True >>> l = [2, 3, 4, 3, 0] >>> drunken_knight(l, 2) >>> l == [-1, 3, -1, 3, -1] True
Disposes de doctests al fitxer drunken_knight-test.txt.