Create a stack of cards, half red, half black, and shuffle them. With the cards face down, turn over the top card. If it is red, add the next card, unseen, to the red stack. If it is black, add the next card, unseen, to the black stack. Add the top card that you have turned to the discard stack. Repeat these steps for all cards in the original stack.

Now, randomly choose a number less than the size of the smaller of the black and red stacks. Choose that many cards randomly from each of the two stacks, and swap those randomly-chosen cards from one stack to the other. The number of black cards in the black stack will always equal the number of red cards in the red stack.

mind-boggling card trick
At the end of this card trick, the number of black cards in the black stack will always equal the number of red cards in the red stack.


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: two through ten (with each card depicting that many symbols of its suit), jack, queen, king (each depicted with a symbol of its suit) and ace (depicting a single symbol 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.

A stack of cards is represented as a list (list) or a tuple (tuple) whose elements are the representations of the individual cards in the stack, listed from top to bottom. A stack of cards does not necessarily need to contain all 52 cards of a standard deck. Your task:


>>> stack = ['4C', '6C', 'JC', '4D', '8D', '9C', '9D', '8S', '2H', '10C', '5D', '7C', '8H', '10H', 'AH', 'KD', 'AC', 'QH', '5C', 'AS', '5H', 'JS', '8C', 'JD']
>>> card_count(stack)
>>> card_count(stack, color='red')
>>> card_count(stack, color='black')
>>> card_count(stack, color='spades')
Traceback (most recent call last):
AssertionError: invalid color

>>> stack1, stack2 = redblack(stack)
>>> stack1
('JS', 'KD', '10H', '7C', '10C', '8S', '9C')
>>> stack2
('JD', 'AS', 'QH', '4D', '6C')

>>> stack3, stack4 = redblack(stack, bottom=True)
>>> stack3
('9C', '8S', '10C', '7C', '10H', 'KD', 'JS')
>>> stack4
('6C', '4D', 'QH', 'AS', 'JD')

>>> stack5, stack6 = swap(stack3, stack4, count=3)
>>> stack5
('6C', '4D', 'QH', '7C', '10H', 'KD', 'JS')
>>> stack6
('9C', '8S', '10C', 'AS', 'JD')
>>> card_count(stack3, color='red')
>>> card_count(stack4, color='black')

>>> stack7, stack8 = swap(stack3, stack4, count=2, bottom=True)
>>> stack7
('9C', '8S', '10C', '7C', '10H', 'AS', 'JD')
>>> stack8
('6C', '4D', 'QH', 'KD', 'JS')
>>> card_count(stack5, color='red')
>>> card_count(stack6, color='black')

The picture below demonstrates how the three top cards are swapped between the two stacks on which the function swap is called in the example.

swap top cards
Swap three top cards between two stacks.

The same is demonstrated below for the two bottom cards of the stacks.

swap bottom cards
Swap two bottom cards between two stacks.