Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Texas Referee by bourbaka
import itertools
RANKS = "23456789TJQKA"
SUITS = "scdh"
def texas_referee(cards_str):
cards=[int(str(RANKS.index(c[:-1])+2)+str(SUITS.index(c[-1])))for c in cards_str.split(',')]
combi=list(itertools.combinations(cards, 5))
maxi=(0,)
for c in combi:
hand=list(c)
hand.sort(reverse=True)
if testhand(hand)>maxi:
maxi=testhand(hand)
besthand=hand
print( tostring(besthand))
return(tostring(besthand))
def testhand(hand):
flush=(len(set([c%10 for c in hand]))==1)
strait=isstrait([c//10 for c in hand])
if flush and strait:
return(8,hand[0])
if kind(4, hand):
return (7, kind(4,hand)[4][0],kind(4,hand)[1][0])
if kind(3,hand) and kind(2,hand):
testdic=kind(3, hand)
b=tuple(testdic[3])
c=tuple(testdic[2])
return (6,)+b+c
if flush:
return(5,)+tuple(hand)
if strait:
return(4,)+tuple(hand)
if kind(3,hand) and kind(1,hand):
testdic=kind(3, hand)
b=tuple(testdic[3])
c=tuple(testdic[1])
return (3,)+b+c
if kind(2,hand):
testdic=kind(2,hand)
if len(testdic[2])==4:
a=(2,)
else :
a=(1,)
return a+tuple(testdic[2])+tuple(testdic[1])
return (0,)+tuple(hand)
def tostring(hand):
delnums=[RANKS[c//10-2]+SUITS[c%10] for c in hand]
return (',').join(delnums)
def isstrait(worth):
for i in range(4):
if worth[i]-worth[i+1]!=1:
return False
return True
def kind(n,hand):
pictures=[c//10 for c in hand]
countdic={r:pictures.count(r) for r in set(pictures)}
counts =set(list(countdic.values()))
if n not in counts:
return None
reverse={}
for c in counts:
reverse[c]=[]
# print (reverse, 'rev0',hand,'xxxxxxxx')
for h in hand:
oft=countdic[h//10]
reverse[oft].append(h)
# reverse={c:[x for x in hand if countdic[x//10]==c].sort() for c in counts}
return reverse
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert texas_referee("Kh,Qh,Ah,9s,2c,Th,Jh") == "Ah,Kh,Qh,Jh,Th", "High Straight Flush"
assert texas_referee("Qd,Ad,9d,8d,Td,Jd,7d") == "Qd,Jd,Td,9d,8d", "Straight Flush"
assert texas_referee("5c,7h,7d,9s,9c,8h,6d") == "9c,8h,7h,6d,5c", "Straight"
assert texas_referee("Ts,2h,2d,3s,Td,3c,Th") == "Th,Td,Ts,3c,3s", "Full House"
assert texas_referee("Jh,Js,9h,Jd,Th,8h,Td") == "Jh,Jd,Js,Th,Td", "Full House vs Flush"
assert texas_referee("Js,Td,8d,9s,7d,2d,4d") == "Td,8d,7d,4d,2d", "Flush"
assert texas_referee("Ts,2h,Tc,3s,Td,3c,Th") == "Th,Td,Tc,Ts,3c", "Four of Kind"
assert texas_referee("Ks,9h,Th,Jh,Kd,Kh,8s") == "Kh,Kd,Ks,Jh,Th", "Three of Kind"
assert texas_referee("2c,3s,4s,5s,7s,2d,7h") == "7h,7s,5s,2d,2c", "Two Pairs"
assert texas_referee("2s,3s,4s,5s,2d,7h,8h") == "8h,7h,5s,2d,2s", "One Pair"
assert texas_referee("3h,4h,Th,6s,Ad,Jc,2h") == "Ad,Jc,Th,6s,4h", "High Cards"
July 23, 2017