Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Using sets to compute unique elements
Set operations
Simulating machines
10. Simulating the pawn's moves


Sets are also used when working with so-called state machines. Sets can help us detect cycles when a machine is operating, i.e. situations in which a given machine configuration appears more than once.

Suppose we have a pawn on an infinite board. The initial position of the pawn is (0, 0). The pawn can move left, right, up, or down each time it moves:



Create a function named next_position(current_position, move) that returns the position of the pawn after making a move. The current_position is a tuple representing the position of the pawn before making a move, i.e. (0, 2). The move is a string representing the move direction ('down', 'up', 'right', or 'left').

The result of next_position((0, 2), 'down') should be (0, 1).