16
1
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
SHCDorshcdfor 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.
thanks for letting me finally understand how the trick works .-. – RedClover – 2018-02-20T21:24:26.370
2@labela--gotoa there's FAR too many variations of this haha. I remember my dad blowing my mind using vertically symmetric cards and doing a different trick, but making me think it was this one. – Magic Octopus Urn – 2018-02-20T21:35:26.640
13"Explosive llamas riding segways through a futuristic tube" - I'm looking forward to your next ascii-art challenge... – Level River St – 2018-02-20T22:05:55.040
@LevelRiverSt ahhh crap!!! That's the FIRST time I've let a sarcastic sandbox comment get through to main off sandbox :( – Magic Octopus Urn – 2018-02-20T22:11:58.630
Are mixed type lists OK for input and output (i.e. 2C =
[2,'C']while JS =['J','S'])? – Jonathan Allan – 2018-02-21T00:17:39.5303Rotating the entire deck by a random offset from 0 to 51 inclusive satisfies the condition that "all cards have an equal chance of appearing anywhere in the shuffled deck", but should probably not be considered a random shuffling. Do you mean that all (52!) orders are approximately equally likely? – aschepler – 2018-02-21T01:02:21.550
1Just to extend on what @aschepler said: with the period length of the default PRNG in many languages, most of the 52! possible shuffles have a probability of appearing exactly equal to zero (although it might be better or worse depending on the shuffling algorithm). – Arnauld – 2018-02-21T11:47:37.850
1
Is a dalai lama riding a lama on a dolly also acceptable? I'm out of segways and explosives, but I have got candy worms... https://i.imgur.com/gEkVR5P.gif
– Tschallacka – 2018-02-21T12:01:46.087@JonathanAllan yessir. – Magic Octopus Urn – 2018-02-21T14:04:33.973
@Tschallacka I really want to know why that Image exists. – Magic Octopus Urn – 2018-02-21T15:33:35.040
@MagicOctopusUrn went through google and it seems that this site is the origin of that somewhere between 2013 and 2014 gif http://www.weirdlyawesome.com/ Note i'm not responsible for heartburn at sight of that site(at least that site is not as horrible as https://www.lingscars.com/ [note i'm not responsible for permanent eye damage])
– Tschallacka – 2018-02-21T16:11:12.160