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)?
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.
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:
ranks: 2, 3, 4, 5, 6, 7, 8, 9, 10, J (jack), Q (queen), K (king) and A (ace)
suits: C (clubs; ♣), D (diamonds; ♦), H (hearts; ♥) and S (spades; ♠
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:
Write a function group that can be used to split a pile of cards into groups. The function takes two arguments: i) a pile of cards and ii) a description of the group sizes. This description is either a natural number (int) or a sequence (list or tuple) of natural numbers (int). If the description is a number $$n \in \mathbb{N}_0$$, all cards in the pile must be split into groups that all contain $$n$$ cards. If the description is a sequence $$(s_1, s_2, \ldots, s_m)$$ with $$s_i \in \mathbb{N}_0 (i=1, \ldots, m)$$, all cards in the pile must be split into groups that respectively contain $$s_1, s_2, \ldots, s_m$$ cards. The function must return the grouped pile of cards. In case the description is not an int or a list/tuple of ints, or in case the pile cannot be split into groups according to the description of the group sizes, an AssertionError must be raised with the message invalid grouping.
Write a function riffle_shuffle that takes two grouped piles of cards. The piles must have an equal number of groups. If this is not the case, an AssertionError must be raised with the message different number of groups. Otherwise, the function must return the new pile of cards obtained after a riffle-shuffle of the two grouped piles of cards.
Write a function mixed_pairs that takes a pile containing an even number of cards. The function must return a Boolean value (bool) that indicates whether all pairs contain one black and one red card (the order doesn't matter) if we repeatedly remove cards from the top of the pile in pairs. If the pile has an odd number of cards, an AssertionError must be raised with the message odd number of cards.
None of these functions may modify its arguments.
>>> 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
Gardner M (August 1960). Mathematical Recreation column. Scientific American.
Gardner M (1966). New Mathematical Diversions from Scientific American.
Gilbreath N (1958). Magnetic colors. The Linking Ring 38(5), 60.