The first magic card trick I ever learned as a child was the following:
- Have 1 deck of cards where the pattern on the back is not vertically symmetric.
- Organize all cards to be facing one direction.
- Ask an individual to, "pick a card, any card, memorize it and give it back to you".
- Proceed to put it into the deck (in the wrong direction).
- Shuffle vigorously, giving the illusion that you won't know where their card is.
- Produce their card to their amazement.
This trick is obviously a little bit lack-luster in nature now-a-days, however it makes for a good challenge. Write a program, that when given no input, outputs a randomly shuffled deck of cards with one of the cards, chosen at random, reversed. However, when the input is a deck of cards with one card reversed, you must output the reversed card (in the correct order).
The Deck of Cards
A deck of cards is defined to be:
[2S,3S,4S,5S,6S,7S,8S,9S,TS,JS,QS,KS,AS,
2D,3D,4D,5D,6D,7D,8D,9D,TD,JD,QD,KD,AD,
2H,3H,4H,5H,6H,7H,8H,9H,TH,JH,QH,KH,AH,
2C,3C,4C,5C,6C,7C,8C,9C,TC,JC,QC,KC,AC]
A card is defined as it's number, then the first letter of its suit. The reverse of a card is the exact opposite, the first letter of its suit followed by a number.
The Drawn Card
Example, if the card we randomly chose to reverse was the 4 of Clubs (4C)
, we'd end up with (without shuffling, obviously):
[2S,3S,4S,5S,6S,7S,8S,9S,TS,JS,QS,KS,AS,
2D,3D,4D,5D,6D,7D,8D,9D,TD,JD,QD,KD,AD,
2H,3H,4H,5H,6H,7H,8H,9H,TH,JH,QH,KH,AH,
2C,3C,C4,5C,6C,7C,8C,9C,TC,JC,QC,KC,AC]
The Shuffling
Then, after shuffling:
[2H,2C,6S,4D,QH,6C,TD,8C,7H,5H,C4,3D,7S,7C,KC,QD,QC,JS,7D,6D,2S,5C,KD,3C,3S,2D,8H,KH,6H,AH,8S,JH,TS,AD,5D,9H,4H,JD,QS,4S,JC,3H,8D,TC,AS,TH,KS,AC,9C,9S,5S,9D]
This is a valid output given empty input.
The Deck Input
However, conversely, when our program receives the above output as input, it should output 4C
. That is to say, for an input of:
[2H,2C,6S,4D,QH,6C,TD,8C,7H,5H,C4,3D,7S,7C,KC,QD,QC,JS,7D,6D,2S,5C,KD,3C,3S,2D,8H,KH,6H,AH,8S,JH,TS,AD,5D,9H,4H,JD,QS,4S,JC,3H,8D,TC,AS,TH,KS,AC,9C,9S,5S,9D]
You iterate through until you find the reversed card, and return it, reversed back to the normal state. So here we'd find C4
, know that C isn't a number, and return it as 4C
, which is correct.
Rules
- You may not load the deck from any external sources.
- An empty input should result in a randomly shuffled deck with 1 random card reversed.
- A deck of cards with 1 card reversed as input should result in the reversed card.
- Any other inputs can result in explosive llamas riding segways through a futuristic tube.
- Or anything else, for that matter.
- Both the chosen card and the shuffling order must be uniformly random.
- I.E. all cards have equal chance of being selected to be reversed.
- I.E. all combinations of cards have an equal chance of appearing.
- You may use
SHCD
orshcd
for the suits, but be consistent:- If you choose uppercase suits (
SHCD
) you must also useTJQKA
. - If you choose lowercase suits (
shcd
) you must also usetjqka
.
- If you choose uppercase suits (
- This is code-golf, winner is lowest bytes.