Mathematicians Steve Humble and Yutaka Nishiyama invented a randomness game to highlight a surprising result in probability, based on a principle discovered by Walter Penney.

The game is played with an ordinary deck of 52 cards, containing 26 red cards and 26 black cards. The cards are dealt out in a row, one after another. Before dealing begins, the first player claims an ordered sequence of colors that might turn up while dealing the cards. Then the second player claims another ordered sequence of colors that might turn up while dealing the cards, which must have the same length as the sequence of the first player. As the cards are dealt, if one of the claimed sequences of successive card colors turns up, the player who claimed it wins a point. Then all cards are removed from the table, and the dealing continues with the remaining cards in the deck. When all 52 cards have been dealt, the player who has collected the most points wins (in a typical game, 7 to 9 points are won).

For example, let's say you and I are playing the game, and you choose the sequence red-black-red. I then choose red-red-black. The following six card colors are dealt:

Humble-Nishiyama randomness game

So far, no one has won yet. A sixth card is put down, which turns out to be a red card. Ah! red-black-red is your sequence. You've one, which gives you one point. You gather up the seven cards and put them by your side, and the dealing continues with the remaining 45 cards.

This sounds like a perfectly even game, but in fact the second player has a strategy that will given him a significant advantage. When the first player has chosen his sequence, say red-black-red (or RBR for short), the second player changes the middle color (in this case he obtains red), adds it to the start of the sequence, discards the last color (in this case he is left with red-black), and claims the resulting sequence (in this case RRB). Believe it or not, but this gives the second player a 82.7% chance to win the game if 7 points are score in total. In general, this strategy gives a decided advantage to the second player no matter which sequence his opponent has chosen. In a computer simulation of 1,000 games, Humble and Nishiyama got these results:

player #1 player #2 results
ZZZ RZZ RZZ wins 955 times, 4 draws, ZZZ wins 1 times
ZZR RZZ RZZ wins 930 times, 40 draws, ZZR wins 30 times
ZRZ ZZR ZZR wins 805 times, 79 draws, ZRZ wins 116 times
RZZ RRZ RRZ wins 890 times, 68 draws, RZZ wins 42 times
ZRR ZZR ZZR wins 872 times, 65 draws, ZRR wins 63 times
RZR RRZ RRZ wins 792 times, 85 draws, RZR wins 123 times
RRZ ZRR ZRR wins 922 times, 51 draws, RRZ wins 27 times
RRR ZRR ZRR wins 988 times, 6 draws, RRR wins 6 times

Assignment

We will represent the color of a card by an uppercase letter: R for red and B for black. This way, we can represent an ordered sequence of colors as a string that only contains the letters R and B. Your task:

The functions score and winner must raise an AssertionError with the message invalid sequences, if the sequences passed to the function do not meet the following conditions:

Example

>>> counter('RRR')
'BRR'
>>> counter('RBB')
'RRB'
>>> counter('BBB')
'RBB'
>>> counter('RBRBRBRB')
Traceback (most recent call last):
AssertionError: invalid sequence
>>> counter('RGB')
Traceback (most recent call last):
AssertionError: invalid sequence

>>> score('BRR', 'RRR', 'RBRBRRBBBBBRRRRRRRRBRRRBRRRBRBBBBBBRRBBBRRRBRBBBBBBR')
(6, 2)
>>> score('RBR', 'BRR', 'BBBBRBRBRBRRBRRBBBRBRBBRRRBBBBRRRBBBRRRRRBBRRBBRRBRR')
(4, 6)
>>> score('BRR', 'RRB', 'BRRRRBRRBRRBBBBRBBRBRBBRRBRRRRRBBBBBRBRBBRRRBRBRBBBR')
(4, 4)
>>> score('BBR', 'BRB', 'BBRBBRRBBRBBRBRBBBBBRBRRRBRBBBRBRRRBRRRBBRRRRRRBBRRR')
Traceback (most recent call last):
AssertionError: invalid sequences
>>> score('RGB', 'BRB', 'RRBBBBRRRRBBRRBRBRRRBBRRBRRBBRBBBRRBBBBRBBRBRRBRRRBB')
Traceback (most recent call last):
AssertionError: invalid sequences

>>> winner('BRR', 'RRR', 'RBRBRRBBBBBRRRRRRRRBRRRBRRRBRBBBBBBRRBBBRRRBRBBBBBBR')
1
>>> winner('RBR', 'BRR', 'BBBBRBRBRBRRBRRBBBRBRBBRRRBBBBRRRBBBRRRRRBBRRBBRRBRR')
2
>>> winner('BRR', 'RRB', 'BRRRRBRRBRRBBBBRBBRBRBBRRBRRRRRBBBBBRBRBBRRRBRBRBBBR')
0
>>> winner('BBR', 'BRB', 'BBRBBRRBBRBBRBRBBBBBRBRRRBRBBBRBRRRBRRRBBRRRRRRBBRRR')
Traceback (most recent call last):
AssertionError: invalid sequences
>>> winner('RGB', 'BRB', 'RRBBBBRRRRBBRRBRBRRRBBRRBRRBBRBBBRRBBBBRBBRBRRBRRRBB')
Traceback (most recent call last):
AssertionError: invalid sequences

Epilogue

Give it a try to beat the computer in the Humble-Nishiyama randomness game. After having read this assignment, you may decide yourself whether you or the computer goes first in claiming an ordered sequence of three cards.

BBB
BBR
BRR
BRB
RRR
RRB
RBB
RBB

#cards:
Your choice and the computers' choice are highlighted.
Human: 0
Computer: 0
 

Resources