Ranking Poker Hands Python

Ranking Poker Hands Python Rating: 7,7/10 7784 reviews

Last updated: January 1, 2018

I recently took a Hackerrank challenge for a job application that involved poker. I'm not a poker player, so I had a brief moment of panic as I read over the problem the description. In this article I want to do some reflection on how I approached the problem.

The hackerrank question asked me to write a program that would determine the best poker hand possible in five-card draw poker. We are given 10 cards, the first 5 are the current hand, and the second 5 are the next five cards in the deck. We assume that we can see the next five cards (they are not hidden). We want to exchange any n number of cards (where n <= 5) in our hand for the next n cards in the deck. For example, we can take out any combination of 2 cards from the hand we are given, but we must replace these two cards with the next two cards from the deck (we can't pick any two cards from the deck).

This command-line python program generates (5-52 card) poker hands and compares their strengths using Texas Hold'Em hand ranking rules. The user can specify how many cards are in each hand, as well as how many decks should be generated and how many hands to draw. The program mimics an N-card Stud game, where N is the number of cards per hand. What hands are rank highest in Poker. ABOUT CARDPLAYER, THE POKER AUTHORITY CardPlayer.com is the world's oldest and most well respected poker magazine and online poker guide.Since 1988.

Create a program to parse a single five card poker hand and rank it according to this list of poker hands. A poker hand is specified as a space separated list of five playing cards. Each input card has two characters indicating face and suit. Poker is an exciting game of luck and sheer skills. In Poker, each player creates a set of five playing’s, i.e., Poker hands. Each Poker hand in the game has a Poker hand ranking that is compared against the competitor’s rank in order to decide who is the winner. This code is intended to choose the best poker hand of five out of a set of cards. The cards are represented by a list of strings, where each string contains the rank and suit (e.g., 'AC' represent.

Suit and value make up the value of playing cards. For example, you can have a 3 of clubs. 3 is the value, clubs is the suit. We can represent this as 3C.

Suits

Clubs CSpades SHeart HDiamonds D

Value (Rank)

2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace

Hands

Here are the hands of poker

  1. Royal flush (the problem didn't ask me to consider Royal Flush)

    A, K, Q, J, 10, all the same suit.

  2. Straight flush

    Five cards in a sequence, all in the same suit. Ace can either come before 2 or come after King.

  3. Four of a kind

    All four cards of the same rank.

  4. Full house

    Three of a kind with a pair.

  5. Flush

    Any five cards of the same suit, but not in a sequence.

  6. Straight

    Five cards in a sequence, but not of the same suit.

  7. Three of a kind

    Three cards of the same rank.

  8. Two pair

    Two different pairs.

  9. Pair

    Two cards of the same rank.

  10. High Card

    When you haven't made any of the hands above, the highest card plays.In the example below, the jack plays as the highest card.

Evaluating a hand of cards

A hand is five cards. The first thing I did was write out functions to evaluate if a group of 5 cards satisfies the conditions of one of the ten hands.

Here's a sample hand:

To write functions, I reached for using 2 important python features: set and defaultdict.

Here's an example of a simple function to detect a flush, a hand with cards of all the same suit:

Checking a flush

This function creates a list of the suits in our hand, and then counts the unique elements in that list by making it a set. If the length of the set is 1, then all the cards in the hand must be of the same suit.

But wait, what if we have a straight flush? Also, a hand that satisfies a flush could also be described as a two pair hand. The problem asked me to find the highest possible hand for a given set of cards, so I tried to keep things simple by writing a check_hand() function that checks each hand starting from straight flush down to high card. As soon as a condition for a hand was satisfied, I returned a number that corresponded to the strength of the hand (1 for high card up to 10 for straight flush). The problem didn't include Royal flush, so I will not include that here.

Here's the check_hand function:

This function starts checking the most valuable hands. After it checks the second to lowest hand (pair), it returns a value of 1. This value of 1 corresponds to the 'highest card' hand. Since I'm not comparing the relative value of hands, it doesn't matter what the highest card is, so the number just represents the type of hand that is the strongest.

Other hands

Here are the all of the functions I used to detect hands:

Ranking poker hands python tutorial

defaultdict is a great built-in that is good to use when you don't know what elements will be in your dictionary, but you know what the initial values of any key that could be added should be. We don't need it here, but the alternative would be to write a very long dictionary where keys are the possible card values and the values of each key is 0.

It would certainly be cleaner and more efficient to write out the above functions into one large function, but I wanted to keep things simple as I was under time constraints.

The next step in the problem is to determine the best possible hand we can get given the hand we are dealt and the 5 cards on top of the deck. I decided to first solve this problem with brute force. Here was my logic for this part: use itertools to get all combinations of groups of 0, 1, 2, 3, 4 and 5 cards from my hand and add the first 5 - n cards from the deck so we get a five card deck. For each combination of cards we can run check_hand() and keep track of the highest rank hand, and then return that hand as the best hand. Here's the code I wrote for this part of the problem:

Lastly, I need to check each hand and print out the best hand possible. Here's the loop I wrote to do this:

This will accept one round of cards per line:

and it will output the following:

This was an interesting problem to deal with as the solution contained several parts that worked together. While solving the problem I aimed worked through to the end leaving some parts to come back to that I felt confident in solving. Instead of writing each function to check differnt hands at the beginning, I filled most of these functions with pass and moved on to write the next part that involves checking each different combination of cards. Recently having worked through python's itertools exercises on Hackerrank, the combinations functions was fresh in my mind.

While I was able to arrive at a solution that satisfied the test cases, I did not have time to think about the efficiency or Big O analysis of the problem.

There is obviously some refactoring that I could do to make things cleaner. With more time I would take an object oriented approach by making classes for cards and hands, and adding class methods to evaluate the hands.

For each round, we have to run check_hand() on each hand combination. Let's think about how many hands we have to evaluate:

We have to consider combinations of cards formed by taking out groups of 0, 1, 2, 3, 4 and 5 cards and adding the next number of cards in the deck that bring the total card count to 5, which means we have to do 5C0 + 5C1 + 5C2 + 5C3 + 5C4 + 5C5 calls to check_hand(). So the sum of total calls is 1 + 5 + 10 + 10 + 5 + 1 = 32.

For each of these 32 calls that happen when we run play(), check_hands() runs through each of the check_ functions starting with the highest value hand. As soon as it finds a 'match', check_hands() returns a number value (hand_value) corresponding to straight flush, four of a kind, etc. This value is then compared with the highest value that has been previously found (best_hand) and replaces that value if the current hand's hand rank has a higher value.

I'm not sure if there is faster way to find the best hand than the brute force method I implemented.

🗣
5How many Poker Hands are there?

There are 10 different hands ranks in Texas Hold’em – from a Royal Flush to a Straight to a lousy High Card. Here’s a comprehensive list of all Texas Hold’em poker hand rankings:

You can also print and download the Official Texas Hold’em hand ranking as a PDF file.

Chart: Poker Hand Ranking

K♥️
J♥️
Royal FlushHighest Straight Flush
7♣️
5♣️
Straight Flush5 suited cards in a row
9
9
Quads4 cards of the same rank
A
Q
Full House3 and 2 cards of the same rank
♠️
♠️
Flush5 suited cards
5
3
Straight5 cards in a row
J
Trips3 cards of the same rank
Q
8
Two Pair2 cards of the same rank twice
2
Pair2 cards of the same rank
High CardHighest cards

Download

Download the poker hand ranking charts image or PDF:

  • Official Poker Hand Rankings Image
  • Print: Poker Hand Rankings PDF

Official Poker Hand Rankings

  • Royal flush: A straight from a ten to an ace with all five cards in the same suit.
  • Straight Flush: Any straight with all five cards in the same suit.
  • Four of a Kind or Poker or Quads: Any four cards of the same rank. If two players share the same four of a kind (on the board), the larger fifth card (the “kicker”) decides who wins the pot.
  • Full House or Boat: Three cards of the same rank along with two cards of the same rank. In short: trips and a pair.
  • Flush: All five cards of the same suit (not necessarily consecutive). The highest card determines the rank of the flush.
  • Straight: Five consecutive cards (not necessarily the same suit). Aces can count as either high or low cards, but not as both at once. Meaning, a straight cannot go “around the corner”.
  • Trips: Three cards of the same value. If two players have the same trips the highest kicker decides who wins the pot.
  • Two Pair: Any two cards of the same rank together with two other cards of the same rank.
  • One Pair: Any two cards of the same rank.
  • High Card: Any hand that is none of the above hands.

Best Online Poker Sites

If you want to start playing poker online, check our online poker sites comparison:

More Information

Poker Hand Rankings Explained

  • If two players have a Straight or Straight Flush, the higher Straight or Straight Flush wins.
  • If two players have a quads, the player with the highest quad wins. If they are identical, the highest kicker wins.
  • If two players have a flush, the player with the highest card in the flush wins. If they are identical, the second highest card decides, then the third highest, and so on. The suit of the flush does not matter.
  • If two players have a full house, the player with the higher trips wins. If they are identical, the player with the higher pair wins.
  • If two players have two pairs, the player with the bigger pair wins. If they are identical, the player with the higher pair wins. If they are also identical, the player with the highest kicker wins.
  • If two players have a pair, the player with the higher pair wins. If they are identical, the highest kicker wins, then the second highest, then the third highest.
  • If two players have a high card, the highest card wins. If they are identical, the second highest card decides, etc.

How many Poker Hands are there?

There are only 10 distinct poker hand ranks, but if you randomly deal 5 cards from a deck of 52 cards there are exactly 2,598,960 possible card combinations.

Poker Hand Odds for 5-Card-Poker

The poker hand ranking charts are based on the probability for each distinct hand rank. More unlikely combinations are ranked higher. Those are the probabilities and odds for all 5-card poker hands:

Poker Hand Odds for Texas Hold’em

If you’re playing Texas Hold’em, you have 7 cards to chose your hand from. There are 133,784,560 to deal 7 random cards. This changes the odds and probabilities for all poker hands a bit. Those are the probabilities and odds for all Texas Hold’em Poker hands:

Technically it’s more likely that you’re dealt at least a pair in Texas Hold’em than holding only high card. But “High Card” still remains the lowest rank.

FAQ: Poker Hand Rankings

Does 2 pairs beat a straight?

When playing Texas Hold’em (or any other popular poker variant) 2 pairs are always ranked below a straight.

Does 3 Aces beat a straight?

3 Aces are just trips (or three of a kind) in poker. When playing regular Texas Hold’em a straight is ranked above trips. There are however rule variations where trips can bet a straight, namely Short Deck Hold’em, a poker variant where all cards below 5 are removed.

Does 5 of a kind beat a royal flush?

In regular poker variants there are is no 5-of-kind rank. When playing with wildcards (joker) 5 of a kind are possible. In this case 5 of a kind are the highest possible poker hand and beat a royal flush.

Does a full house beat 3 aces?

Every full house always beats trips, no matter the rank of the trips. Even trip aces are always ranked below every possible full house.

Does Royal Straight beat flush?

A Royal Flush is the best possible poker hand and of course always beats any other flush.

Does straight beat a full house?

Every common poker variant, including Texas Hold’em, ranks a Full House above a straight. So no, a Straight never beats a Full House in Poker.

What beats a royal flush?

In all regular modern poker variations (including Texas Hold’em and Omaha) a Royal Flush is always the highest possible hand rank. A higher rank is only possible when playing with a Joker. In this case 5 of a kind (4 Aces plus Joker) beats a Royal Flush.

What can beat a flush in poker?

A Flush is a very strong hand in poker. The only hands that beat a Flush are Full House, Quads, Straight Flush, and Royal Flush.

How rare is a royal flush?

A Royal Flush is extremely rare. When playing Texas Hold’em you’ll only get one every 31,000 hands. And that assumes you never fold. The hand is so rare that most poker players can remember all Royal Flushes they have been dealt in their life time.

What are the odds of hitting a straight flush?

Ranking Poker Hands Python Games

Straight Flushes are almost as rare as Royal Flushes. When playing Texas Hold’em you will hit a Straight Flush roughly every 3,600 hands (assuming you never fold any hand that can make a Straight Flush).

Can you have 3 pairs in poker?

There is no “3 pair” hand rank in poker. When playing Texas Hold’em it’s technically possible to have three pairs, but since a poker hand only consists of 5 cards only the 2 highest pairs are in play. For example, if you hold Q-J and the board reads Q-J-6-A-A you only have two pair: Aces and Queens.

Does Royal Flush have to be spades?

A Royal Flush can be any of the 4 suits, spades, hearts, diamonds, or clubs. It’s just that usually a Royal Flush is depicted in spades or hearts. Nevertheless, it doesn’t matter which suit, a Royal Flush is always the best Texas Hold’em Poker Hand.

How many kickers can you have in poker?

A poker hand can consist of up to 5 kickers. A player with no pair only has kickers. A player with one pair has 3 kickers, a player with trips has 2 kickers, and a player with 2 pair or quads has 1 kicker.

Is Ace a 1 in poker?

When building a straight an Ace can be used as a virtual “1” in poker. Meaning, A-2-3-4-5 is a straight. There are also lowball poker variations where the Ace counts as the lowest card.

Is an Ace 2 3 4 5 a straight?

Yes, the ace can count as the lowest card in a straight and function as a “1” when combined with 2-3-4-5.

Is JQKA 2 a straight?

A straight cannot go “around the corner”, the Ace can only be either the highest or the lowest card, not a card in the middle. So no, J-Q-K-A-2 is no straight in poker.

Is Queen King Ace 2 3 a straight?

A straight cannot go “around the corner”, the Ace can only be either the highest or the lowest card, not a card in the middle. So no, Q-K-A-2-3 is no straight in poker.

Is there a kicker on a straight?

For a straight you need to use all 5 cards. There are no cards left for a kicker. The rank of the straight is determined by the highest card. E.g. an ace-high straight beats a queen-high straight.

Ranking Poker Hands Python Cheat

What is a flush in poker?

A flush in poker is hand which consists of 5 cards of the same suit. The same color (red or black) is not enough. It has to 5 spades, hearts, diamonds, or clubs.

What is the highest royal flush in poker?

There are no distinctions between the 4 possible Royal Flushes in poker. A Royal Flush in spades is as good as a Royal Flush in hearts, diamonds, or clubs.

What is the highest suit in Texas Hold’em poker?

Only in very rare occasions (for example when dealing for the button) the suits are ranked in poker. In this case the ranking is: 1. spades, 2. hearts, 3. diamonds, 4. clubs. Suits are otherwise generally not ranked in poker. A Flush in spades is as good as a flush in any other suit, only the ranks of the cards matter.

What is the lowest pair in a game of poker?

In poker the lowest possible pair is a pair of Deuces (twos).

How do you win bad beat jackpots in poker?

To win a bad beat jackpot in poker you need to lose with a very strong hand, usually a strong Full House (Aces Full). It’s also necessary that both, the winning hand losing player, user both of their hole cards. E.g. losing with quads on the board does not count.

What are the odds of hitting a bad beat jackpot in poker?

The odds of hitting a bad beat jackpot in poker depend on the rules for the jackpot. If you have to lose with Aces Full or better your odds of hitting the bad beat jackpot are 1:58,948. If you have to lose with quads or better your odds are 1:624,609 (assuming a 10 player table where nobody ever folds).

What is a bad beat in poker?

If you lose with a very strong hand against an even stronger hand this is called a “bad beat”. It is also a bad beat if you lose an all-in while being far ahead and you opponent wins by catching some miracle cards.

How many 5 stud poker hands are there?

5 Card Stud is one of the oldest poker variants where each player is dealt 5 cards. There are exactly 2,598,960 different 5 stud poker hands possible.

How many poker hands are there?

There are only 10 distinct poker hand ranks, but if you randomly deal 5 cards from a deck of 52 cards there are exactly 2,598,960 possible card combinations. If you’re playing Texas Hold’em, you have 7 cards to chose your hand from. There are 133,784,560 to deal 7 random cards.

What happens if two hands tie in poker?

It’s possible (and not too uncommon) for two players to have the same hand in poker. In this case the pot is split and both players receive half the pot.

What happens if two people have a royal flush?

When playing Texas Hold’em it’s almost impossible for two players to have a Royal Flush. For that to happen the 5 community cards need to form a Royal Flush. In that case all players in the hand win and split the pot.

What happens if two poker hands are the same?

If two players have the same hand, the pot is split and both players win half of it. This can happen for example if both players have the same cards (e.g. Ace-King) and nobody makes a Flush.

Ranking Poker Hands Python Tutorial

How do you hit a royal flush on video poker?

In Video Poker you can win the jackpot when you hit a Royal Flush. To maximize your chances you should always keep all suited cards 10 or above (if you have at least 2) and discard the rest. You will see a Royal Flush roughly once every 40,000 spins.

Ranking poker hands python tutorial

What are the odds of hitting a royal flush on a video poker machine?

The odds of hitting a royal flush directly are only 1 in 649,739. But since you can draw one time your odds increase. If you play perfectly your odds of hitting a royal flush are roughly 1 in 40,000.

Relevant Resources

Article Rating

Ranking Poker Hands Python Game