Here is a card trick that was invented by Martin David Kruskal, an American mathematician and physicist who made his career at Princeton University and Rutgers University. Give a friend a complete set of playing cards and ask them to follow the instructions :
Think of a "secret number" between 1 and 10 (for example: 6)
Shuffle the set and place the cards one by one on the table face up. Start counting from one in your mind while you are going through the cards.
If you have counted up to the secret number, consider the last card you have placed on the table as the next "secret card". Use the value of the secret card as your new secret number. Aces count as 1 and jacks, queens and kings count as 5. (for example: if the sixth card is a four of clubs, then four is your new secret number)
Go through all the playing cards one by one while you start over again, silently start counting from when you encountered a new secret card. Remember the last secret card that you have encountered.
That is the only explanation that you have to give. You then look at how they put the cards on the table one by one. After all the cards are on the table, you can reveal the last secret card, preferably by means of a bet with gigantic stakes.
You can do this because you have played along. While your friend is laying the cards on the table, choose the value of one of the first cards on the table (or choose a secret number between 1 and 10 beforehand ) and silently follow the same procedure. Nine out of ten times your "path" through the cards will intersect and your last secret card will be the same as hers. It's been far from clear to see that this is the case, and that will certainly not change if you refuse to explain what is behind the trick.
In this task, we represent a complete set of playing cards as a list or tuple with 52 strings. Each playing card is hereby represented by a string consisting of two characters: the first character is the value of the card and the second character the color. The following two tables give the representation of the thirteen different values (left) and four different colors (right). A complete set of playing cards contains 52 different cards which are formed by using all combinations of the values with the colors.
value | representation |
---|---|
2, …, 9 | 2, …, 9 |
10 | T |
jack | J |
queen | Q |
king | K |
ace | A |
colour | representation |
---|---|
clubs | c |
diamonds | d |
spades | s |
hearts | h |
Write a function secret_cards to which two arguments should be passed: a secret number $$n \in \mathbb{N}$$ ($$1 \leq n \leq 10$$) and a complete set of playing cards. The function must perform the procedure of the card trick as described above, and return a list with all the secret cards that are put on the table during this procedure, in the order they are placed on the table. The arguments that are passed to the function, determine the secret number that is chosen in step 1 of the procedure, and the order of the cards after they are shuffled in step 2 of the procedure. The function can not shake the cards again.
>>> game = ['3c', '2d', 'Kd', 'Ts', '8d', 'Th', 'Jc', '8c', '6s', '4d', '3h', '5d', '7h', 'Jh', '2c', '2h', '2s', '9h', '5s', '6c', '6h', 'Qh', 'Ah', 'Qd', '9d', 'Kc', 'Tc', 'Td', '5h', '7c', 'As', '5c', 'Ks', '3d', 'Qs', '8h', '4c', 'Js', '7s', '4s', '3s', '6d', 'Qc', '4h', 'Ad', '7d', '8s', 'Ac', '9s', 'Jd', 'Kh', '9c']
>>> secret_cards(6, game)
['Th', '2h', '9h', 'Tc', '4c', '3s', '4h', 'Ac', '9s']
>>> secret_cards(2, game)
['2d', 'Ts', 'Jh', '5s', 'Qd', '5h', '3d', '4c', '3s', '4h', 'Ac', '9s']
>>> secret_cards(10, game)
['4d', 'Jh', '5s', 'Qd', '5h', '3d', '4c', '3s', '4h', 'Ac', '9s']