project_euler.problem_054.sol1¶
Problem: https://projecteuler.net/problem=54
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. But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared; if the highest cards tie then the next highest cards are compared, and so on.
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?
Resources used: https://en.wikipedia.org/wiki/Texas_hold_%27em https://en.wikipedia.org/wiki/List_of_poker_hands
Similar problem on codewars: https://www.codewars.com/kata/ranking-poker-hands https://www.codewars.com/kata/sortable-poker-hands
Classes¶
Create an object representing a Poker Hand based on an input of a |
Functions¶
|
Module Contents¶
- class project_euler.problem_054.sol1.PokerHand(hand: str)¶
Create an object representing a Poker Hand based on an input of a string which represents the best 5-card combination from the player’s hand and board cards.
- Attributes: (read-only)
hand: a string representing the hand consisting of five cards
- Methods:
- compare_with(opponent): takes in player’s hand (self) and
opponent’s hand (opponent) and compares both hands according to the rules of Texas Hold’em. Returns one of 3 strings (Win, Loss, Tie) based on whether player’s hand is better than the opponent’s hand.
- hand_name(): Returns a string made up of two parts: hand name
and high card.
- Supported operators:
Rich comparison operators: <, >, <=, >=, ==, !=
- Supported built-in methods and functions:
list.sort(), sorted()
- __eq__(other)¶
- __ge__(other)¶
- __gt__(other)¶
- __hash__()¶
- __le__(other)¶
- __lt__(other)¶
- __repr__()¶
- __str__()¶
- _get_hand_type() int ¶
- _internal_state() tuple[list[int], set[str]] ¶
- _is_five_high_straight() bool ¶
- _is_flush() bool ¶
- _is_same_kind() int ¶
- _is_straight() bool ¶
- compare_with(other: PokerHand) str ¶
Determines the outcome of comparing self hand with other hand. Returns the output as ‘Win’, ‘Loss’, ‘Tie’ according to the rules of Texas Hold’em.
Here are some examples: >>> player = PokerHand(“2H 3H 4H 5H 6H”) # Stright flush >>> opponent = PokerHand(“KS AS TS QS JS”) # Royal flush >>> player.compare_with(opponent) ‘Loss’
>>> player = PokerHand("2S AH 2H AS AC") # Full house >>> opponent = PokerHand("2H 3H 5H 6H 7H") # Flush >>> player.compare_with(opponent) 'Win'
>>> player = PokerHand("2S AH 4H 5S 6C") # High card >>> opponent = PokerHand("AD 4C 5H 6H 2C") # High card >>> player.compare_with(opponent) 'Tie'
- hand_name() str ¶
Return the name of the hand in the following format: ‘hand name, high card’
Here are some examples: >>> PokerHand(“KS AS TS QS JS”).hand_name() ‘Royal flush’
>>> PokerHand("2D 6D 3D 4D 5D").hand_name() 'Straight flush, Six-high'
>>> PokerHand("JC 6H JS JD JH").hand_name() 'Four of a kind, Jacks'
>>> PokerHand("3D 2H 3H 2C 2D").hand_name() 'Full house, Twos over Threes'
>>> PokerHand("2H 4D 3C AS 5S").hand_name() # Low ace 'Straight, Five-high'
- _CARD_NAME = ('', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack',...¶
- _HAND_NAME = ('High card', 'One pair', 'Two pairs', 'Three of a kind', 'Straight', 'Flush', 'Full house',...¶
- _first_pair = 0¶
- _hand¶
- _hand_type¶
- _high_card¶
- _second_pair = 0¶
- property hand¶
Returns the self hand
- project_euler.problem_054.sol1.solution() int ¶