Derek loves dominoes. He has his own default domino set which consists of 28 square stones. Every stone is divided in two squares. Each square is marked with zero to six dots.
In order to play the game, at least two players are needed. Layla — Derek's sister — refuses to play, because she always loses when she plays against her brother. Out of boredom, Derek thought of two games he can play by himself.
Every game starts the same way. Derek randomly chooses a number of domino stones and lays them in a sequence one after another. This sequence doesn't necessarily contain all stones of his domino set. In this assignment, we represent a domino stone as a tuple of two integers, that indicate the number of dots that are on both halves of the stone. A sequence of stones is represented as a list or a tuple of domino stones. In a sequence of domino stones, two stones follow each other if the right side of the left stone is equal to the amount of dots on the left side of the right stone.
Write a function beginsequence to which a sequence of stones must be given. The function must print the maximum amount of stones that can follow up after each other at the beginning of the sequence. The function has an optional parameter turn to which a Boolean value can be given (default value: False). This parameter indicates whether the stone may be turned 180° in order to make it follow the stone before. The first stone of a sequence may never be turned.
Write a function dominosequence to which a sequence of domino stones must be given. The function has an optional parameter turn to which a Boolean value can be given (default value: False). This parameter indicates whether the stone may be turned 180° in order to make it follow the stone before. The function must print a new sequence of domino stones that is built as follows:
take the first stone of the given sequence and use it as the first stone of a new sequence; this first stone may never be turned
run through the remaining stones of a given sequence from left to right and search the first stone that can be put next to the last stone of a new sequence (possibly after turning it 180° if this is permitted by the parameter turn)
depending on whether a stone was found in step 2, there are two possibilities:
if a stone was found, delete it from the given sequence of stones and expand the new sequence with the new stone on the right-hand side, so that is follows the last stone; after this procedure, repeat it from step 2
if no stone was found, the procedure ends and the newly formed sequence must be printed
The functions above don't depend on each other. It isn't necessary to call on the function beginsequence when implementing the function dominosequence.
>>> sequence = ((3, 0), (0, 1), (1, 2), (0, 2), (0, 0), (3, 3), (1, 1), (2, 3), (3, 1), (2, 2))
>>> beginsequence(reeks)
3
>>> beginsequence (sequence, turn=True)
5
>>> sequence = ((1, 3), (2, 0), (1, 0), (0, 3), (1, 1), (0, 0), (3, 3), (2, 2), (3, 2), (1, 2))
>>> dominosequence(sequence)
[(1, 3), (3, 3), (3, 2), (2, 0), (0, 3)]
>>> dominosequence(sequence, turn=True)
[(1, 3), (3, 0), (0, 2), (2, 2), (2, 3), (3, 3)]