MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / compare_with

Method compare_with

project_euler/problem_054/sol1.py:141–184  ·  view source on GitHub ↗

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 = PokerH

(self, other: PokerHand)

Source from the content-addressed store, hash-verified

139 return self._hand
140
141 def compare_with(self, other: PokerHand) -> str:
142 """
143 Determines the outcome of comparing self hand with other hand.
144 Returns the output as 'Win', 'Loss', 'Tie' according to the rules of
145 Texas Hold'em.
146
147 Here are some examples:
148 >>> player = PokerHand("2H 3H 4H 5H 6H") # Stright flush
149 >>> opponent = PokerHand("KS AS TS QS JS") # Royal flush
150 >>> player.compare_with(opponent)
151 'Loss'
152
153 >>> player = PokerHand("2S AH 2H AS AC") # Full house
154 >>> opponent = PokerHand("2H 3H 5H 6H 7H") # Flush
155 >>> player.compare_with(opponent)
156 'Win'
157
158 >>> player = PokerHand("2S AH 4H 5S 6C") # High card
159 >>> opponent = PokerHand("AD 4C 5H 6H 2C") # High card
160 >>> player.compare_with(opponent)
161 'Tie'
162 """
163 # Breaking the tie works on the following order of precedence:
164 # 1. First pair (default 0)
165 # 2. Second pair (default 0)
166 # 3. Compare all cards in reverse order because they are sorted.
167
168 # First pair and second pair will only be a non-zero value if the card
169 # type is either from the following:
170 # 21: Four of a kind
171 # 20: Full house
172 # 17: Three of a kind
173 # 16: Two pairs
174 # 15: One pair
175 if self._hand_type > other._hand_type:
176 return "Win"
177 elif self._hand_type < other._hand_type:
178 return "Loss"
179 elif self._first_pair == other._first_pair:
180 if self._second_pair == other._second_pair:
181 return self._compare_cards(other)
182 else:
183 return "Win" if self._second_pair > other._second_pair else "Loss"
184 return "Win" if self._first_pair > other._first_pair else "Loss"
185
186 # This function is not part of the problem, I did it just for fun
187 def hand_name(self) -> str:

Callers 6

__eq__Method · 0.95
__lt__Method · 0.95
solutionFunction · 0.80
test_compare_simpleFunction · 0.80
test_compare_randomFunction · 0.80
test_euler_projectFunction · 0.80

Calls 1

_compare_cardsMethod · 0.95

Tested by 3

test_compare_simpleFunction · 0.64
test_compare_randomFunction · 0.64
test_euler_projectFunction · 0.64