# Hand sort

A card-rearranging problem on the Riddler blog. Here it goes:

You play so many card games that you’ve developed a very specific organizational obsession. When you’re dealt your hand, you want to organize it such that the cards of a given suit are grouped together and, if possible, such that no suited groups of the same color are adjacent. (Numbers don’t matter to you.) Moreover, when you receive your randomly ordered hand, you want to achieve this organization with a single motion, moving only one adjacent block of cards to some other position in your hand, maintaining the original order of that block and other cards, except for that one move.

Suppose you’re playing pitch, in which a hand has six cards. What are the odds that you can accomplish your obsessive goal? What about for another game, where a hand has N cards, somewhere between 1 and 13?

Here is my solution:
[Show Solution]

## 10 thoughts on “Hand sort”

1. Dave Moran says:

Laurent,

Nice work, as always! But in looking at your bar graph of the results, I don’t see why the probability for N=2 and N=3 wouldn’t be 1. If you have two cards, don’t they automatically meet the goal without any movement necessary? If you have three cards, I think you could always move one card (if necessary) to achieve the goal of uniting two cards of the same suit.

1. Not quite — One of the criteria is that adjacent cards of a different suit must also be a different color. So if a two-card hand consists of a heart and a diamond or a spade and a club, it can never be sorted…

Ah but now I see what you mean. My interpretation was that the “different colors” requirement was a hard constraint, as opposed to “it’s only a hard constraint if it’s possible”. Words are hard.

1. Dave Moran says:

As a law professor, I couldn’t agree more with you that words are hard! Making the shift from math and physics grad student to law student was a challenge, and it still is.

2. Jim Crimmins says:

Actually, aren’t all hands for N=1-4 sortable in one move? I am assuming hands with only red or black suits are counted as sortable (HHDD, CCSS, etc). But maybe that’s not that clear from the puzzle.

3. Jim Crimmins says:

“When you’re dealt your hand, you want to organize it such that the cards of a given suit are grouped together and, if possible, such that no suited groups of the same color are adjacent.

I took this to mean that single color hands could be sorted. But it is confusing.

1. Dave Moran says:

(1) if the hand is single-colored, this condition doesn’t apply; but
(2) if the hand is bi-colored, then suited groups of the same color must be separated.

1. I updated my blog post to reflect the two possible interpretations.
I included a plot for both cases.
Thank you all for the feedback!

4. Guy D. Moore says:

Hi Laurent,
I think there is a small error in your solutions.
You have treated each suit as equally likely. But in fact, as soon as I draw one card from a suit, drawing another is less likely, since there are fewer left. For instance, I find that the probability of failing, in the “strict” reading, when there are 2 cards, is 13/51, whereas your result is 13/52=1/4. The reason is that, if I first draw a heart (say), there are now 12 hearts, 13 diamonds, and 26 black cards I can draw. The chance of drawing a diamond, and thereby failing, is 13/51, not 1/4. Similarly, with 3 cards, I lose precisely when I draw two from one suit and a third from the other suit, in any order. The probability to do so is not 3/16=.1875, it is 3*13*12/(51*50) = .183529..
Other than that, it’s a nice solution!

1. Ah you’re absolutely correct. Treating the hands as multisets by collapsing all the numbers doesn’t work. I could probably fix my approach if I weighted each multiset by its probability of occurrence. e.g. if my suits are (S,H,C,D) and we take the 2-card case, the multiset approach says there are 16 possible hands: {SS, HH, CC, DD, SH, HS, SC, CS, SD, DS, HC, CH, HD, DH, CD, DC}. Exactly 4 of those have adjacent suits of the same color: {SC, CS, HD, DH}. That leads to 4/16 = 1/4, which is incorrect as you pointed out. There are 13*12 ways of getting SS, HH, ect, but 13*13 ways of getting SC, CS, etc… If we weight each one by its respective likelihood, that yields:
$\frac{4\cdot 13 \cdot 13}{4\cdot 13 \cdot 12 + 12\cdot 13 \cdot 13} = \frac{13}{51}$which is the correct probability. If I can find the time over the next few days I’ll see if I can fix my solution to incorporate these weightings.

Thanks!!!

1. I edited my blog post once more. This time I think the solution should be correct. As Guy Moore pointed out, I had ignored the fact that not all hands are equally likely to occur. Re-weighing each hand by its likelihood of occurrence fixed the issue. Thanks for the comments!