Shapes of bridge hands

Definition of "shape"

A bridge hand consists of 13 cards, drawn from a pack of 52 cards with 13 in each of four suits. Bridge hands can be classified by "shape". The shape of a hand is the set of lengths of the suits, in order by length. Thus a hand with "6-4-3-0" shape might have three spades, no heart, six diamonds and four clubs.

There are 39 shapes of bridge hand

There are 39 possible shapes of bridge hand. They are shown to the right. You can click on the diagram to see a larger version.

The diagram shows the shapes in their "partial order" by flatness, as indicated by descending green lines. Hand B is regarded as flatter than hand A if

Hand A can be converted to hand B by moving one card of hand A 
to another, shorter suit
    or
Hand B is flatter than some hand which is flatter than hand A.
Note that this defintion is recursive.

How to calculate the number of shapes

To use a computer program to calculate the number of shapes of bridge hand, we can use four, or better three, nested loops. Or, more elegantly, we can use the insight

A hand either has a void (so we have dealt with one of the suits) 
    or 
it doesn't (so we have placed as many cards as there were suits)
to break the problem down into two smaller ones, and use double recursion. This has the advantage that the values "4" and "13" need not be hard-coded, they can be variables supplied to the program.

The calculation can be done by code like this Javascript

function shapes( suits, cards ) {
  if ( suits < 2 ) { return 1; }
  if ( cards < 0 ) { return 0; }
  if ( cards < 2 ) { return 1; }
  return shapes( suits, cards-suits ) + shapes( suits-1, cards );
}
The long line is the critical one, splitting the problem into two smaller problems. The three lines above it ensure that the recursion terminates when recursing deeper will be unhelpful.

Similar code can do the same calculation in any other recursive language. If this function is called with suits=4 and cards=13, it returns the value 39. It also works for other inputs. You can test it below.

Number of suits
Number of cards in a hand
 

This is one of several miscellaneous pages.