Skip to content

Instantly share code, notes, and snippets.

@sahuguet
Created March 13, 2017 16:08
Show Gist options
  • Save sahuguet/53b0c4848bb8a8e687db42278d33ece4 to your computer and use it in GitHub Desktop.
Save sahuguet/53b0c4848bb8a8e687db42278d33ece4 to your computer and use it in GitHub Desktop.
Code to enumerate poker hands with two pairs from 5 cards out of a 52 card deck
# Function that checks if 5 cards build a two-pairs hand
# A card is a number between 0 and 51.
# The value of a card is its number modulo 13.
# The suit of a card is its number div 4.
def is_two_pairs(c1, c2, c3, c4, c5):
assert len({c1, c2, c3, c4, c5}) == 5
counts = dict()
for i in (c1, c2, c3, c4, c5):
counts[i % 13] = counts.get(i % 13 , 0) + 1
return len(counts) == 3 and sorted(counts.values())[-1] == 2
DECK = 13 * 4
def compute():
all_hands = 0
two_pairs = 0
for i in range(0, DECK):
for j in [x for x in range(i, DECK) if x not in [i]]:
for k in [x for x in range(j, DECK) if x not in [i,j]]:
for l in [x for x in range(k, DECK) if x not in [i,j,k]]:
for m in [x for x in range(l, DECK) if x not in [i,j,k,l]]:
all_hands += 1
if is_two_pairs(i, j, k, l, m):
two_pairs += 1
print all_hands, two_pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment