Woozy Walk

Consider a list of positive int (0 included). Let’s define a “step” from position i over a list L (i is in the range of L) as moving to either the position resulting from adding L[i] to i if L[i] is even, or the position resulting from substracting L[i] to i if L[i] is odd.

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

woozy_walk(L, i0)

such that

given

returns a list with all the positions of L “walked on” by starting at i0 until either it comes to a position through which it have already walked, or it falls outside the positive range of L (i.e. the interval of valid positive indexes of a Python list).

For example:


>>> L = [6, 2, 0, 4, 3, 4, 1, 9]

>>> woozy_walk(L, 0)
[0, 6, 5]

>>> woozy_walk(L, 1)
[1, 3, 7]

>>> woozy_walk(L, 4)          
[4, 1, 3, 7]

Observe the resulting list of the first call:

  • The first value is 0 because that’s the initial position.

  • The next one is 6 since L[0] is 6 which is even and 0+6 is 6.

  • The next one is 5 since L[6] is 1 which is odd and 6-1 is 5.

  • Finally, the next position obtained is 9 since L[5] is 4 which is even and 5+4 is 9.

Doctests are available at the file woozy_walk.test.