Created
October 31, 2016 19:41
-
-
Save dooglus/351c25605a60a0c9b3af41d14004e4ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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