Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
OOP, Counter, combinations, lambdas solution in Creative category for Texas Referee by Phil15
from itertools import combinations
from collections import Counter
RANK = '23456789TJQKA'
SUIT = 'scdh'
class PokerHand:
def __init__(self, five_cards):
self.hand = sorted(((RANK.index(card[0]), SUIT.index(card[1]))
for card in five_cards),
reverse = True)
self.suits = [suit for rank, suit in self.hand]
self.ranks = sorted(rank for rank, suit in self.hand)
self.count_suits = Counter(self.suits).values()
self.count_ranks = Counter(self.ranks).values()
__str__ = lambda self: ','.join(RANK[r] + SUIT[s] for r, s in self.hand)
straight_flush = lambda self: self.flush() and self.straight()
four_of_a_kind = lambda self: 4 in self.count_ranks
full_house = lambda self: set(self.count_ranks) == {2, 3}
flush = lambda self: 5 in self.count_suits
straight = lambda self: all(r1-r0==1 for r0, r1 in zip(self.ranks,
self.ranks[1:]))
three_of_a_kind = lambda self: 3 in self.count_ranks
two_pair = lambda self: list(self.count_ranks).count(2) == 2
one_pair = lambda self: 2 in self.count_ranks
high_card = lambda self: True
def best_hand(self):
categories = ('straight_flush', 'four_of_a_kind', 'full_house',
'flush', 'straight', 'three_of_a_kind',
'two_pair', 'one_pair', 'high_card')
for n, hand in enumerate(categories):
if getattr(self, hand)():
return -n, self.hand, str(self)
texas_referee = lambda hand: max(PokerHand(five).best_hand()
for five in combinations(hand.split(','),5))[2]
Sept. 28, 2018
Comments: