Dancing Rook

Consider a list of int where each number represents a shift of position over the list. Let’s define a “dance move” from position i over a list L as moving to the position i+L[i]. Thus, dacing over a list L from a given initial position i0 is doing dance moves starting at i0 until it shifts outside the range of L (being the range the interval of valid indexes of a Python list).

Write the following function in the module with the same name (file dancing_rook.py):

dancing_rook(L, i0)

such that given

does two things:

  • returns a list of int with all the positions that it “danced over” (including the initial one)

  • modifies L by assigning a str '.' to all positions traversed by dancing over L from the initial position i0 until it shifts onto a position whose value has already been changed to ‘.’ or it falls outside the range of L.

Per exemple:


>>> l = [1, -2, 2, -1]
>>> dl = dancing_rook(l, 0)
>>> dl == [0, 1, -1, -2] and l == ['.', '.', '.', '.']  
True

>>> l = [1, -2, 2, -1]
>>> dl = dancing_rook(l, 2)
>>> dl == [2] and l == [1, -2, '.', -1]
True

Doctests are available at the file dancing_rook-test.txt.