from itertools import combinations
from collections import Counter
RANKS='23456789TJQKA'
SUITS='shdc'
DECK=[(r,s) for r in range(2,15) for s in range(4)]
SEQUENCES=[(14,13,12),(14,3,2)]+[(r,r-1,r-2) for r in range(13,3,-1)]
def evaluate(hand,ace_first=False):
 ranks=sorted([x[0] for x in hand],reverse=True); c=Counter(ranks)
 groups=sorted(((n,r) for r,n in c.items()),reverse=True)
 flush=len({x[1] for x in hand})==1
 seqs=list(SEQUENCES)
 if ace_first:seqs[0],seqs[1]=seqs[1],seqs[0]
 straight=tuple(ranks) in seqs
 if groups[0][0]==3:return (5,ranks[0])
 if straight:return (4 if flush else 3,12-seqs.index(tuple(ranks)))
 if flush:return (2,*ranks)
 if groups[0][0]==2:return (1,groups[0][1],groups[1][1])
 return (0,*ranks)
def parse(s):return tuple((RANKS.index(x[0])+2,SUITS.index(x[1])) for x in s.split())
def equity(s):
 h=parse(s);score=evaluate(h);left=[c for c in DECK if c not in h];d=Counter()
 for other in combinations(left,3):
  v=evaluate(other);d['win' if score>v else 'tie' if score==v else 'lose']+=1
 return dict(d)
if __name__=='__main__':
 counts=Counter(evaluate(h)[0] for h in combinations(DECK,3))
 print('category_counts',dict(sorted(counts.items())))
 for s in ['Qs Qh 5d','As Kh Qd','7s 4h 2d','As Ah Ad','2s 3s 4s','As Ks Qs']:
  e=equity(s);print(s,e,{k:round(v/18424*100,6) for k,v in e.items()})
 print('AK47_joker_count',sum(any(c[0] in [14,13,4,7] for c in h) for h in combinations(DECK,3)))
