patternpythonModerate
Optimizing "Poker hands" challenge solution
Viewed 0 times
pokerchallengehandsoptimizingsolution
Problem
I'm trying to solve the project Euler problem 54.
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following hand dealt to two players:
Player 1 (5H 5C 6S 7S KD), Player 2 (2C 3S 8S 8D TD), Winner: Player 2
The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
Following is the code I have written - the code works alright, but I'm interested in learning how I can improve its quality in any possible way.
```
from collections import Counter
suites = {
'C' : "Club",
'D' : "Diamond",
'H' : "Heart",
'S' : "Spade",
}
In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:
- High Card: Highest value card.
- One Pair: Two cards of the same value.
- Two Pairs: Two different pairs.
- Three of a Kind: Three cards of the same value.
- Straight: All cards are consecutive values.
- Flush: All cards of the same suit.
- Full House: Three of a kind and a pair.
- Four of a Kind: Four cards of the same value.
- Straight Flush: All cards are consecutive values of same suit.
- Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.
The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.
If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.
Consider the following hand dealt to two players:
Player 1 (5H 5C 6S 7S KD), Player 2 (2C 3S 8S 8D TD), Winner: Player 2
The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.
How many hands does Player 1 win?
Following is the code I have written - the code works alright, but I'm interested in learning how I can improve its quality in any possible way.
```
from collections import Counter
suites = {
'C' : "Club",
'D' : "Diamond",
'H' : "Heart",
'S' : "Spade",
}
Solution
Naming:
Playing card's have suits, not suites (fancy hotel rooms), so adjust your code accordingly.
Also, why does
Documentation:
You have no documentation. Quality code needs documentation, which in the case of Python, is a docstring for every class and function that looks like this:
Code:
First off, it's good that you made some new exceptions, however, they are of minor use to the programmer without a corresponding message. It would've been better if you had given an already existing exception with a message such as
Secondly, you have a big nasty rank function that we can clean up a bit.
-
In your check for "Straight Flush" you use
-
This leads to another code simplification: instead of checking 1 suit and royal flush, 1 suit and straight, and 1 suit, have an outer condition for 1 suit, and inner conditions for the others like so:
-
Then, go through the counts of the values. But instead of recalculating it every time, calculate the number of distinct values once, and store in a variable.
-
I also would add extra helper functions to determine the pair and high card and so on to help trim down the rank function to a decent size.
Finally, let's use some of Python's magic (specifically, magic methods). Instead of manually retrieving values and so on, let's make the
How do we do this, you ask? Easy, override these 3 methods in
That should help you trim down most of your hand/hand-ranking code
Playing card's have suits, not suites (fancy hotel rooms), so adjust your code accordingly.
Also, why does
hands[0].cards_by_hand return a list of values and not a list of cards? By the name of it it sounds like it should be hands[0].card_values insteadDocumentation:
You have no documentation. Quality code needs documentation, which in the case of Python, is a docstring for every class and function that looks like this:
def factorial(n):
"""Returns n! if n > 0 and if it can be converted to type int"""Code:
First off, it's good that you made some new exceptions, however, they are of minor use to the programmer without a corresponding message. It would've been better if you had given an already existing exception with a message such as
ValueError("Hand has more than 5 cards") rather than an InvalidHand exception with no message because the first gives more info than the second. Ideally of course you would use InvalidHand("Hand has more than 5 cards").Secondly, you have a big nasty rank function that we can clean up a bit.
-
In your check for "Straight Flush" you use
len(counter_suits).keys() which works for your purposes (because 1 is taken to be True) but this is not what a straight flush is, since it relies on having just 1 suit (while this condition works for 2, 3, 4 suits), so this should be changed to: len(counter_suits.keys()) == 1-
This leads to another code simplification: instead of checking 1 suit and royal flush, 1 suit and straight, and 1 suit, have an outer condition for 1 suit, and inner conditions for the others like so:
if len(counter_suits.keys()) == 1:
if set(values) == set(range(10, 15)):
# Royal Flush
elif set(values) == set(range(values[0], values[0] + 5)):
# Straight Flush
else:
# Flush-
Then, go through the counts of the values. But instead of recalculating it every time, calculate the number of distinct values once, and store in a variable.
-
I also would add extra helper functions to determine the pair and high card and so on to help trim down the rank function to a decent size.
Finally, let's use some of Python's magic (specifically, magic methods). Instead of manually retrieving values and so on, let's make the
Card type work like this:Card('5H') > Card('4H') # produces True
Card('5H') < Card('4H') # produces False
Card('5H') == Card('5H') # produces TrueHow do we do this, you ask? Easy, override these 3 methods in
Card:__eq__(self, other):
if isinstance(other, Card):
return self.value == other.value
return False
__lt__(self, other):
if isinstance(other, Card):
return self.value other.value
return FalseThat should help you trim down most of your hand/hand-ranking code
Code Snippets
def factorial(n):
"""Returns n! if n > 0 and if it can be converted to type int"""if len(counter_suits.keys()) == 1:
if set(values) == set(range(10, 15)):
# Royal Flush
elif set(values) == set(range(values[0], values[0] + 5)):
# Straight Flush
else:
# FlushCard('5H') > Card('4H') # produces True
Card('5H') < Card('4H') # produces False
Card('5H') == Card('5H') # produces True__eq__(self, other):
if isinstance(other, Card):
return self.value == other.value
return False
__lt__(self, other):
if isinstance(other, Card):
return self.value < other.value
return False
__gt__(self, other):
if isinstance(other, Card):
return self.value > other.value
return FalseContext
StackExchange Code Review Q#60738, answer score: 11
Revisions (0)
No revisions yet.