Mind boggling card trick

From Rosetta Code
Revision as of 12:43, 25 August 2018 by rosettacode>Paddy3118 (New draft task with python example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Mind boggling card trick is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Matt Parker of the Stand Up Maths channel on you tube has a video of a card trick that creates a semblance of order from chaos.

The task is to simulate the trick in a way that mimicks the steps shown in the video.

1. Cards
  1. Create a pack of 52 cards, half red - half black.
  2. Give the pack a good shuffle.
2. Deal from the randomised pack into three stacks
  1. Assemble the cards face down.
    1. Turn up the top card,
      1. if it is black then add the next card, unseen, to the black-stack.
      2. If it is red then instead add that next card, unseen, to the red-stack.
    2. Add the card you turned over to see what colour it was above, to the discard-stack.
      (You might optionally show these discard cards to give an idea of the randomness).
  2. Repeat the above for the whole of the assembled pack.
3. Swap the same, random, number of cards between the two stacks.
  1. Randomly choose the number of cards to swap.
  2. Randomly choose that number of cards out of each stack to swap.
    (Without knowing those cards - they could be red or black cards from the stacks, we don't know).
4. Order from randomness?
  1. Check the mathematicians assertion that: The number of black cards in the black pile equals the number of red cards in the red pile.

Show output on this page.

Python

The code is layed out to follow the task decription, leading to some deviations from the PEP8 guidelines <lang python>import random

    1. 1. Cards

n = 52 Black, Red = 'Black', 'Red' blacks = [Black] * (n // 2) reds = [Red] * (n // 2) pack = blacks + reds

  1. Give the pack a good shuffle.

random.shuffle(pack)

    1. 2. Deal from the randomised pack into three stacks

black_stack, red_stack, discard = [], [], [] while pack:

   top = pack.pop()
   if top == Black:
       black_stack.append(pack.pop())
   else:
       red_stack.append(pack.pop())
   discard.append(top)

print('(Discards:', ' '.join(d[0] for d in discard), ')\n')

    1. 3. Swap the same, random, number of cards between the two stacks.
  1. We can't swap more than the number of cards in a stack.

max_swaps = min(len(black_stack), len(red_stack))

  1. Randomly choose the number of cards to swap.

swap_count = random.randint(0, max_swaps) print('Swapping', swap_count)

  1. Randomly choose that number of cards out of each stack to swap.

def random_partition(stack, count):

   "Partition the stack into 'count' randomly selected members and the rest"
   sample = random.sample(stack, count)
   rest = stack[::]
   for card in sample:
       rest.remove(card)
   return rest, sample

black_stack, black_swap = random_partition(black_stack, swap_count) red_stack, red_swap = random_partition(red_stack, swap_count)

  1. Perform the swap.

black_stack += red_swap red_stack += black_swap

    1. 4. Order from randomness?

if black_stack.count(Black) == red_stack.count(Red):

   print('Yeha! The mathematician assertion is correct.')

else:

   print('Whoops - The mathematician (or my card manipulations) are flakey')</lang>

A run.

Output:
(Discards: R B R R B B R R R B B B B R R R B R R B B B B R B R )

Swapping 11
Yeha! The mathematician assertion is correct.

A second run:

Output:
(Discards: R B B R B B R B R R R B R R B B B B R R B R R B B R )

Swapping 2
Yeha! The mathematician assertion is correct.