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
intwith all the positions that it “danced over” (including the initial one)modifies
Lby assigning astr'.'to all positions traversed by dancing overLfrom the initial positioni0until it shifts onto a position whose value has already been changed to ‘.’ or it falls outside the range ofL.
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.