Poker hand ranking
Write a function that tells you the best scoring for a given poker hand of 5 cards. For instance:
(score [[3 :diamonds] [3 :hearts] [3 :spades] [5 :hearts] [:king :clubs]])
=> :three-of-a-kind ;; three 3s
Cards are represented as a tuple of rank (number or name if it's a face card) and suit. Face card names are the keywords :ace
, :king
, :queen
, :jack
. Suits are :diamonds
, :spades
, :hearts
, :clubs
.
Here are the hands, in order of importance:
- Royal Flush: Ace, King, Queen, Jack, and 10 of the same suit
- Straight Flush: Five consecutive cards of the same suit
- Four of a kind: Four cards of the same rank
- Full house: Three of a kind and a pair
- Flush: Any five cards of the same suit
- Straight: Five consecutive cards, not in same suit
- Three of a kind: Three cards of the same rank
- Two pair: Two different pairs
- Pair: Two cards of the same rank
- High card: No other hand available
Thanks to this site for the challenge idea where this is considered Expert level.
You can leave comments on these submissions in the gist itself. Please leave comments! You can also hit the Subscribe button to keep abreast of the comments. We’re all here to learn.
I forgot that aces can be low in straights e.g. A♣ 2♣ 3♣ 4♣ 5♣. Here's a tweaked version that fixes that: