Choose an equal number of black and red cards from a standard 52-card deck (you may also take the entire deck). Put the selected cards into one pile that alternates between black and red cards. It doesn't matter whether the top card is black or red. Now cut the pile into two smaller piles of merely equal size, so that the bottom card of one pile is black and the other is red. This is only possible if the two piles have an odd number of cards, and if the top and bottom cards of each pile have different physical colors.

Put the two smaller piles next to each other on a table. Use your thumbs to lift up the inner sides of the piles, then release the cards by the thumbs so that they fall to the table interleaved: first some cards from the first pile, then some cards from the second pile, again some cards from the first pile, …, until all cards have dropped on the table. Slide all cards together into a new pile. This way of shuffling the cards is called a riffle-shuffle1 (or a dovetail shuffle). Now remove cards from the top of the new pile in pairs. How many of these pairs should we expect to contain cards of different physical colors (one black and one red card)?

riffle-shuffle
Riffle-shuffle two piles of cards.

Surprisingly, all of them will. During the shuffle, suppose a black card falls first. It must be followed by either the next card in its own pile (which is red), or the first card from the other pile (which is red as well). Either way, the first pair will contain one black and one red card, and by the same principle so will each of the other pairs produced by the shuffle. This effect was first identified by mathemagician Norman Gilbreath in 1958.

Assignment

A standard card deck includes 52 different cards, evenly distributed over four suits: 13 clubs (♣), 13 diamonds (), 13 hearts () and 13 spades (♠). Clubs and spades are black, whereas diamonds and hearts are red. Each suit includes thirteen ranks: an ace (depicting a single symbol of its suit), a king, queen, jack (each depicted with a symbol of its suit), and ranks two through ten (with each card depicting that many symbols of its suit). Commercial card decks may also include anywhere from one to six jokers, but we do not take these into account here.

Each card of a standard 52-card deck is represented by a string (str) that contains the rank of the card, followed by its suit:

As such, the ace of spades is for example represented as AS, the ten of hearts as 10H and the king of clubs as KC.

We represent a pile of cards as a list containing the representations of the cards in the pile, where the first element of the list is the top card in the pile. A pile of cards does not necessarily contain all 52 cards of a deck.

In a riffle-shuffle all cards in a pile drop in groups of successive cards. We represent such a grouped pile of cards as a list in which all cards are grouped in tuples (tuple). This way, the outer list represents the pile, and the tuples of cards in that list represent the groups. As such

[('KS', '4H'), ('KH', 'JD'), ('10S', '2D'), ('9C', 'JH')]

represents a grouped pile in which eight cards are grouped in four groups of two cards. If the same eight cards are grouped in three groups that respectively contain 2, 4 and 2 cards, we represent this as the following grouped pile

[('KS', '4H'), ('KH', 'JD', '10S', '2D'), ('9C', 'JH')]

A riffle-shuffle then works with two grouped piles of cards that contain the same number of groups. The two piles do not need to contain the same number of cards, and the cards should not be evenly and equally distributed over the groups. A riffle-shuffle results in a new pile of cards that is composed of the cards in the first group of the first pile, followed by the cards in the first group of the second pile, followed by the cards in the second group of the first pile, followed by the cards in the second group of the second pile, …

Your task:

None of these functions may modify its arguments.

Example

>>> cards = ['KS', '4H', 'KH', 'JD', '10S', '2D', '9C', 'JH']
>>> group(cards, 2)
[('KS', '4H'), ('KH', 'JD'), ('10S', '2D'), ('9C', 'JH')]
>>> group(cards, 4)
[('KS', '4H', 'KH', 'JD'), ('10S', '2D', '9C', 'JH')]
>>> group(cards, 3)
Traceback (most recent call last):
AssertionError: invalid grouping
>>> group(cards, [2, 4, 2])
[('KS', '4H'), ('KH', 'JD', '10S', '2D'), ('9C', 'JH')]
>>> group(cards, (3, 2, 3))
[('KS', '4H', 'KH'), ('JD', '10S'), ('2D', '9C', 'JH')]
>>> group(cards, [3, 1, 3])
Traceback (most recent call last):
AssertionError: invalid grouping

>>> pile1 = [('7C', '2D', 'JC'), ('7H', '10S', '9D'), ('5C', 'AH', '3C')]
>>> pile2 = [('AD', '9C'), ('KD', '10C', 'QH', 'JS'), ('4D', 'AS', '8D')]
>>> pile3 = [('AD',), ('9C', 'KD', '10C'), ('QH', 'JS'), ('4D', 'AS', '8D')]
>>> riffle_shuffle(pile1, pile2)
['7C', '2D', 'JC', 'AD', '9C', '7H', '10S', '9D', 'KD', '10C', 'QH', 'JS', '5C', 'AH', '3C', '4D', 'AS', '8D']
>>> riffle_shuffle(pile1, pile3)
Traceback (most recent call last):
AssertionError: different number of groups

>>> new_pile = riffle_shuffle(pile1, pile2)
>>> mixed_pairs(new_pile)
True
>>> mixed_pairs(['7C', '2D', 'JC', '7H', '10S', '9D', '5C', 'AH', '3C'])
Traceback (most recent call last):
AssertionError: odd number of cards

Resources