Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Texas Referee by Moff
from itertools import combinations, tee, groupby
class Card(object):
faces = {'T': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}
suits = {"h": 3, "d": 2, "c": 1, "s": 0}
def __init__(self, s):
self.face, self.suit = s
@property
def value(self):
return int(self.faces.get(self.face, self.face))
@property
def suit_rank(self):
return {"h": 4, "d": 3, "c": 2, "s": 1}.get(self.suit)
def __repr__(self):
return '{0.face}{0.suit}'.format(self)
@property
def score(self):
return self.value * 4 + self.suit_rank
class Hand(object):
def __init__(self, cards):
self.cards = cards
def highest_card(self):
return max(c.value for c in self.cards)
def one_pair(self):
pairs = self.groups(2)
if len(pairs) == 1:
return 1e2 + 2e1 * max(pairs)
return 0
def two_pair(self):
pairs = self.groups(2)
if len(pairs) == 2:
return 1e3 + 2e2 * max(pairs)
return 0
def three_of_a_kind(self):
threes = self.groups(3)
if len(threes) == 1:
return 1e4 + 2e3 * max(threes)
return 0
def straight(self):
return 1e5 * int(all(b - a == 1 for a, b in self.pairwise()))
def flush(self):
return 1e6 * int(len(self.suits()) == 1)
def full_house(self):
return 1e7 * int(len(self.groups(2)) == 1 and len(self.groups(3)) == 1)
def four_of_a_kind(self):
fours = self.groups(4)
if len(fours) == 1:
return 1e8 + 2e7 * max(fours)
return 0
def straight_flush(self):
return 1e9 * int(len(self.suits()) == 1 and
all(b - a == 1 for a, b in self.pairwise()))
def royal_flush(self):
return 1e10 * int(len(self.suits()) == 1 and
[c.value for c in self.cards] == [10, 11, 12, 13, 14])
def score(self):
return sum(sf() for sf in [
self.highest_card, self.one_pair, self.two_pair, self.three_of_a_kind,
self.straight, self.flush, self.full_house, self.four_of_a_kind,
self.straight_flush, self.royal_flush]) + sum(c.score for c in self.cards)
def groups(self, length):
return [k for k, g in groupby(c.value for c in self.cards)
if len(list(g)) == length]
def pairwise(self):
a, b = tee(c.value for c in self.cards)
next(b, None)
return zip(a, b)
def suits(self):
return list({c.suit for c in self.cards})
def texas_referee(cards):
best_score, best_cards = 0, None
for hand in combinations(cards.split(','), 5):
hand = Hand(sorted((Card(c) for c in hand), key=lambda x: x.value))
current_score = hand.score()
if current_score > best_score:
best_score, best_cards = current_score, hand.cards
return ','.join(map(str, sorted(best_cards, key=lambda x: x.score, reverse=True)))
Sept. 10, 2015