This week the #puzzle is: Can You Swap the Cups? #average #ExpectedValue #montecarlo #coding
My friend has three cups, labeled “A,” “B,” and “C” in a row. She randomly picks two different cups and swaps their positions. Then she does this again and again, picking a random pair each time, until all three cups are back in their original order. For example, here is one such sequence of swaps:
– A, B, C (original) – C, B, A (first and third were swapped) – B, C, A (first and second were swapped) – B, A, C (second and third were swapped) – A, B, C (first and second were swapped)
In this example, the cups returned to their original order after four swaps.
On average, how many swaps would you expect until the cups return to their original order?
And for extra credit:
In total, there are six possible orders of the cups. Without any swaps, one of those orders (A, B, C) has already been achieved.
On average, how many swaps would you expect until the remaining five orders (and thus, all six) are also achieved?
I didn’t read the instructions correctly. I thought the 1st quiz had 4 questions, not 3.
I did notice other answers using strategies involving “if the 1st correct answer was A, there’s a slightly better chance the 3rd answer is A”, but I didn’t understand why this could be until the official answer was out.
Method 1: Ye olde monte carlo. This time I play around with variance and deviation calculations, so that the program will keep going, until I get the number of decimals, I want.
====================================== Number of loops.... 3 * : 102970102 Target deviation........: 0.0010000 Deviations so far, res 1: 0.0003204 ====================================== Final result 1..........: 5.9999515 +/- 0.0003204
Result: 6.00. Maybe simply 6?
Method 2: Look closely at what’s going on. This is a diagram of all states and all changes between states.
And that’s a little… Wait, that can actually be shown in a better way.
“abc” is the beginning state. Each line represents 1 swap. How many steps does it take to get from a state back to the beginning state?
If we’re already in the beginning state, 0 steps.
Here I’ve called “a state on the left that isn’t the beginning state” “left”. 1/3 of the time, take 1 step and get back to the beginning state. 2/3 of the time, take 1 step and reach a left state. And actually, let’s call all the states on the right “right”.
In a left state, I have to take 1 step to get to a right state. Let’s solve this.
There’s always a 1st step, away from the beginning state. And then it will take 5 further steps/swaps to get back. That’s 6. Result confirmed.
And for extra credit:
Same program, I just use the data slightly differently.
====================================== Number of loops.........: 102970102 Target deviation........: 0.0010000 Deviations so far, res 2: 0.0005949 ====================================== Final result 2..........: 12.2144230 +/- 0.0005949
Result: 12.21 – 12.22.
Method 2. Let me see. First some new notation.
How many steps left on average, when there’s x already visited states on “my” side (WLOG the left side) of the diagram and y on the other?
If both sides are already full, there are 0 steps left.
This is the same situation as the fiddler. Being in one of the lower states on the left side and aiming for (WLOG) the top state of the left.
This was the intermediate situation above, taking 1 step away from the coveted state.
Let’s just summarize.
0
5
n/a
6
n/a
n/a
n/a
n/a
n/a
Some of these situations are impossible. Typically, I can’t have visited 2-3 states on one side of the diagram and 0 on the other. Also, x can’t be 0, because I am in a state, that is therefore visited.
Right!
Let’s solve this.
And:
And:
Let’s summarize again.
0
5
8
n/a
6
8
n/a
9
n/a
n/a
n/a
n/a
Solving.
And:
And summarizing:
0
5
8
n/a
6
8
n/a
9
n/a
n/a
n/a
n/a
Almost there!
Solving:
0
5
8
n/a
6
8
n/a
9
n/a
n/a
n/a
n/a
Whew! Only one more.
Solving:
This was the number, we were looking for. And it confirms the previous result.