Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Texas Referee solution in Uncategorized category for Texas Referee by capback250
# migrated from python 2.7
CARDS = ['A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2']
SUITS = ['h', 'd', 'c', 's']
def straight_flush(hand):
for suit in set([x[1] for x in hand]):
sting_with_suit = ''.join([x[0] for x in hand if x[1] == suit])
for i in range(len(CARDS)-4):
if ''.join(CARDS[i:i+5]) in sting_with_suit:
return ','.join([x+suit for x in CARDS[i:i+5]])
return False
def four_of_a_king(cards):
if counter_cards(cards)[0][1] == 4:
return ','.join([counter_cards(cards)[0][0]+i for i in SUITS])+','+sorted([x for x in cards if x[0] != counter_cards(cards)[0][0]], key=lambda x: (CARDS.index(x[0]), SUITS.index(x[1])))[0]
else:
return False
def full_house(cards):
if counter_cards(cards)[0][1] == 3 and counter_cards(cards)[1][-1][1] == 2:
s = sorted([x[0] for x in counter_cards(cards)[1] if x[1] == 2], key=lambda x:CARDS.index(x[0]))[0]
return ','.join([x for x in cards if x[0] == counter_cards(cards)[0][0]] + [x for x in cards if x[0] == s])
return False
def flush(cards):
for suit in set([x[1] for x in cards]):
if len([x for x in cards if x[1] == suit]) >= 5:
return ','.join([x for x in cards if x[1] == suit][:5])
return False
def straight(cards):
s = []
for i in range(len(CARDS)-4):
if ''.join(CARDS[i:i+5]) in ''.join(sorted(set([x[0] for x in cards]), key=lambda x: CARDS.index(x))):
for x in CARDS[i:i+5]:
for y in cards:
if y[0] == x:
s.append(y)
break
return ','.join(s)
return False
def three_of_a_kind(cards):
if counter_cards(cards)[0][1] == 3:
return ','.join([x for x in cards if x[0] == counter_cards(cards)[0][0]] + filter(lambda x:x[0] != counter_cards(cards)[0][0], cards)[:2])
return False
def two_pair(cards):
if counter_cards(cards)[0][1] == counter_cards(cards)[1][-1][1] == 2:
paits = sorted([counter_cards(cards)[0][0], counter_cards(cards)[1][-1][0]], key=lambda x: CARDS.index(x[0]))
a = [x for x in cards if x[0] == paits[0]] + [x for x in cards if x[0] == paits[1]] + filter(lambda x:x[0] != paits[0] and x[0] != paits[1], cards)[:1]
return ','.join(sorted(a, key=lambda x: (CARDS.index(x[0]), SUITS.index(x[1]))))
return False
def one_pair(cards):
if counter_cards(cards)[0][1] == 2:
a = filter(lambda x:x[0] != counter_cards(cards)[0][0], cards)[:3] + [x for x in cards if x[0] == counter_cards(cards)[0][0]]
return ','.join(sorted(a, key=lambda x: (CARDS.index(x[0]), SUITS.index(x[1]))))
return False
def hight_card(cards):
return ','.join(cards[:5])
def counter_cards(cards):
s = []
only_cards = [x[0] for x in cards]
for x in CARDS:
s.append([x, only_cards.count(x)])
finded = [x for x in sorted(s,key=lambda x:x[1]) if x[1] != 0]
maxed = finded.pop()
return [maxed, finded]
def texas_referee(cards):
hand = sorted(cards.split(','), key=lambda x: (CARDS.index(x[0]), SUITS.index(x[1])))
flag = False
ranks = [straight_flush, four_of_a_king, full_house, flush, straight, three_of_a_kind, two_pair, one_pair, hight_card]
for func in ranks:
if not flag:
flag = func(hand)
return flag
Dec. 25, 2015