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)
defrandom_partition(stack,count):"Partition the stack into 'count' randomly selected members and the rest"sample=random.sample(stack,count)rest=stack[::]forcardinsample:rest.remove(card)returnrest,sampleblack_stack,black_swap=random_partition(black_stack,swap_count)red_stack,red_swap=random_partition(red_stack,swap_count)
Perform the swap.
Order from randomness?
The mathematician asserts that:
The number of black cards in the black pile equals the number of red cards in the red pile
ifblack_stack.count(Black)==red_stack.count(Red):print('Yea! The mathematician is right.')else:print('Woops - That mathematician (or my card manipulations) are flakey')