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.
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:
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.
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:
Write a function card_count that takes a stack of cards. The function also has an optional parameter color. If no argument is explicitly passed to the parameter color, the function must return the number of cards in the given stack. If the string red is passed to the parameter color, the function must return the number of red cards in the given stack. If the string black is passed to the parameter color, the function must return the number of black cards in the given stack. For all other arguments passed to the parameter color, the function must raise an AssertionError with the message invalid color.
Write a function redblack that takes a stack of cards. The given stack of cards must have an equal number of red and black cards. If this is not the case, the function must raise an AssertionError with the message different number of red and black cards. The function also has an optional parameter bottom that takes a Boolean value (default value: False). The function must perform the procedure outlined in the introduction to the given stack of cards and return a tuple (tuple) containing the obtained red and black stacks. The stacks themselves must also be represented as a tuple (tuple). If the value False is passed to the parameter bottom, the cards from the given stack must be put on top of the red and black stacks. If the value True is passed to the parameter bottom, the cards from the given stack must be inserted at the bottom of the red and black stacks.
Write a function swap that takes two stacks of cards. The function also has an optional parameter count that takes a number $$n \in \mathbb{N}$$ (default value: 0). The number $$n$$ must be less than the size of the smaller of the two given stacks. If this is not the case, the function must raise an AssertionError with the message invalid number of cards. The function has another optional parameter bottom that takes a Boolean value (default value: False). The function must create two new stacks by choosing $$n$$ cards from each of the two given stacks, and swap those chosen cards from one stack to the other. If the value False is passed to the parameter bottom, the $$n$$ top cards must be chosen from each stack and put on top of the other stack. If the value True is passed to the parameter bottom, the $$n$$ bottom cards must be chosen from each stack and put at the bottom of the other stack. The function must return a tuple (tuple) containing the two newly created stacks, with each newly created stack also represented as a tuple (tuple).
>>> 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)
24
>>> card_count(stack, color='red')
12
>>> card_count(stack, color='black')
12
>>> 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')
2
>>> card_count(stack4, color='black')
2
>>> 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')
4
>>> card_count(stack6, color='black')
4
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.
The same is demonstrated below for the two bottom cards of the stacks.