New Jersey (United States) magician Karl Fulves1 invented this extrasensory perception trick. Hand a friend an ordinary die and turn your back. Ask her to place the die on a table. Now ask her to give the die a quarter turn. If the top number is even, she must turn it to the east (to her right), and if the top number is odd, she must turn it north (away from her). This exposes a new top number, and she can turn the die again according to the same rule, turning it east if the number is even and north if it's odd.

After she has continued in this way for several turns, you ask her to stop when the top number is 1, then to give the die one final turn and to concentrate on the top number. It would seem as though the final number might be any one of four possibilities, but you can name it correctly with your back turned. How?

die

Explanation — after at most three moves, the die enters a recurring loop: 1 4 5 6 3 2. So after the first few turns, it's safe to predict that 4 will follow 1. Because the whole cycle is predictable, the trick can be repeated a few times to produce various "final" numbers.

Assignment

Define a class Die that can be used to represent dice that have been put in a certain way on a table in front of you. It must be possible to turn these dice in a given direction, according to the model described in the figure below. According to this model, dice are represented schematically such that each side of a die is made visible. For example, the central scheme represents a die with side 6 on top, side 3 to the back, side 4 to the front, side 2 to the left, side 5 to the right and side 1 at the bottom. The other four schemes indicate the position of the die after it has been turned from the central position to the north (top), to the south (bottom), to the east (right) or to the west (left).

turning dice

The class Die must support at least the following methods:

Example

>>> die = Die()
>>> print(die)
  3
2 6 5 1
  4
>>> die.topNumber()
6
>>> die.turn('E')
2
>>> print(die)
  3
1 2 6 5
  4
>>> die.turn('W')
6
>>> print(die)
  3
2 6 5 1
  4
>>> die.turn('N')
4
>>> print(die)
  6
2 4 5 3
  1
>>> die.turn('S')
6
>>> print(die)
  3
2 6 5 1
  4
>>> die.turn('X')
Traceback (most recent call last):
AssertionError: invalid direction
>>> die.sequence(1)
[2, 1, 4, 5, 6, 3, 2, 1, 4]
>>> print(die)
  1
5 4 2 3
  6
>>> die.sequence(3)
[5, 6, 3, 2]
>>> print(die)
  3
1 2 6 5
  4