Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
A bit of OOP solution in Clear category for Texas Referee by swagg010164
from itertools import permutations
RANKS = "23456789TJQKA"
SUITS = "scdh"
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.r_int = RANKS.index(self.rank)
self.suit = suit
self.suit_int = SUITS.index(self.suit)
self.priority = RANKS.index(self.rank) + SUITS.index(self.suit)
def __repr__(self):
return "{}{}".format(self.rank, self.suit)
def combin(a):
f = True
for k in range(4):
if a[k].r_int - a[k + 1].r_int != 1 or a[k].suit != a[k + 1].suit:
f = False
break
if f:
if a[0].rank == 'A':
return [10, a]
else:
return [9, a]
all_suits = set(a[k].suit for k in range(5))
straight = True
for k in range(4):
if a[k].r_int - a[k + 1].r_int != 1:
straight = False
break
k = [0]*14
for i in a:
k[i.r_int] += 1
if 4 in k and 1 in k:
return [8, a]
elif 3 in k and 2 in k:
return [7, a]
elif len(all_suits) == 1:
return [6, a]
elif straight:
return [5, a]
elif 3 in k and 2 not in k:
return [4, a]
elif k.count(2) == 2 and 1 in k:
return [3, a]
elif 2 in k and k.count(1) == 3:
return [2, a]
return [1, a]
def texas_referee(cards_str):
cards = [Card(i[0], i[1]) for i in cards_str.split(',')]
current, answer = 0, []
t = list(permutations(cards, 5))
for k in t:
k = list(k)
k.sort(key = lambda x: (x.r_int, x.suit_int), reverse = True)
c = combin(k)
point, res = c[0], c[1]
if point > current:
current = point
answer = res
elif point == current:
f = False
for j in range(len(res)):
if res[j].r_int > answer[j].r_int:
f = True
break
elif res[j].rank == answer[j].rank:
if res[j].suit_int > answer[j].suit_int:
f = True
break
else:
break
if f:
current = point
answer = res
return ",".join(str(k) for k in answer)
Dec. 8, 2018