6.7. Ball Game 2

A group of \(N\) children decides to play a ball game. Here are the rules:

  • Each child gets a number from \(1\) to \(N\) and these numbers are put in a list randomly.

  • The first child in the list has the ball at the beginning of the game.

  • Let’s say that this child got the ball with number \(k\). Then he/she must throw the ball to the child who is in \(k^{th}\) place in the list.

  • For a given number of turns, the game continues like this.

As the children don’t know about programming, they don’t have a clue about zero-based indexing applied in Python (like in almost every programming language). In other words, they don’t know that the indices of the elements in a Python list varies between \(0\) and \((N-1)\) instead of between \(1\) and \(N\).

Write a function named ball_game which takes two arguments. The first argument is the shuffled list of children’s numbers as a list of integers, where the second one is the number of turns as an integer. This function must return an integer as the number of child who has the ball when the game is over.

Note that if the ball is thrown to the child with number \(N\), the function should terminate the game early without giving any error.

Sample I/O:

>>> ball_game([1, 3, 2], 1)
1

>>> ball_game([1, 3, 2], 2)
3

>>> ball_game([1, 3, 2], 3)
3

>>> ball_game([3, 4, 5, 1, 2], 4)
2

>>> ball_game([3, 4, 5, 1, 2], 5)
5

>>> ball_game([3, 4, 5, 1, 2], 6)
5

>>> ball_game([8, 7, 1, 5, 3, 10, 4, 2, 6, 9], 4)
3

>>> ball_game([8, 7, 1, 5, 3, 10, 4, 2, 6, 9], 5)
5

>>> ball_game([8, 7, 1, 5, 3, 10, 4, 2, 6, 9], 6)
10

>>> ball_game([8, 7, 1, 5, 3, 10, 4, 2, 6, 9], 7)
10
def ball_game(list_of_children, number_of_turns):
    t = 0
    i = 0
    while t < number_of_turns:
        try:
            i = list_of_children[i]
        except IndexError:
            break
        t += 1
    return i