Skip to content

Instantly share code, notes, and snippets.

@dooglus
Created October 31, 2016 19:41
Show Gist options
  • Save dooglus/351c25605a60a0c9b3af41d14004e4ff to your computer and use it in GitHub Desktop.
Save dooglus/351c25605a60a0c9b3af41d14004e4ff to your computer and use it in GitHub Desktop.
def evaluate(hand):
ranks = '23456789TJQKA'
if len(hand) > 5: return max([evaluate(hand[:i] + hand[i+1:]) for i in range(len(hand))])
score, ranks = zip(*sorted((cnt, rank) for rank, cnt in {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items())[::-1])
if len(score) == 5: # if there are 5 different ranks it could be a straight or a flush (or both)
if ranks[0:2] == (12, 3): ranks = (3, 2, 1, 0, -1) # adjust if 5 high straight
score = ([1,(3,1,2)],[(3,1,3),(5,)])[len({suit for _, suit in hand}) == 1][ranks[0] - ranks[4] == 4] # high card, straight, flush, straight flush
return score, ranks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment