In the last stage of a two players Texas hold 'em, given a two-card hand and five cards on table, determine your probability to win versus an opponent by the standard ranking of poker hands.


Seven cards (sdtin or arguments). The first two cards are your hand while the last five are on the table. Each card will be a two letter string of the form RS where R is rank and S is suit. The ranks range from 2-9, T for ten, and J, Q, K, and A for Jack, Queen, King, and Ace respectively. The suits are H, D, C, S for Hearts, Diamonds, Clubs, and Spades respectively.


The probability to win (stdout or return): from '0.00' to '1.00'.

Input to Output Examples:

AH 7C  KH QH JH TH 6C -> 1.00

Explanation: You have a royal flush, 100% winning chances.

9H 7C  KH QH JH TH 6C -> 0.96

Explanation: You have a straight flush, your opponent can only win with an AH, 95.6% winning chances.

Rules and Clarifications:

  • There are 52 different cards. You can refer to Texas hold 'em to get the idea.
  • Your opponent has 2 unknown cards (out of 45 left). That's 990 possibilities with equal probability.
  • You are free to decide if Ties count or don't count in the calculation of the probability.
  • As this is code golf, the shortest answer wins.
  • \$\begingroup\$ How should a potential tie be handled in the calculation? And given that you're asking about probabilities, it would be clearer if you made explicit your prior that the opponent holds each of the 990 possible pairs with equal probability. \$\endgroup\$ Jul 9, 2014 at 7:06
  • \$\begingroup\$ @PeterTaylor I made "ties" free so you can shorten your code appropriately. \$\endgroup\$
    – Cœur
    Jul 9, 2014 at 7:15
  • \$\begingroup\$ @Cœur - I just want to give you a kudos on the challenge... I've spent about 2.5 hours so far trying to solve this. If I do solve it I think I'll need gzip to make it small! ;-) \$\endgroup\$
    – scunliffe
    Jul 13, 2014 at 14:31
  • \$\begingroup\$ @scunliffe I was thinking of simplifying the challenge by only outputting 0 if you have less than 50% chances winning, or 1 if you have more than 50% chances winning, then I realized some people would hardcode the combinations somehow, compacting it with a hash-table maybe. \$\endgroup\$
    – Cœur
    Jul 15, 2014 at 12:13


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.