Skip to content

Instantly share code, notes, and snippets.

@aldanor
Created December 3, 2012 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldanor/4193087 to your computer and use it in GitHub Desktop.
Save aldanor/4193087 to your computer and use it in GitHub Desktop.
canonical cards sorting / suit wildcards
def sort_cards(cards, mask_suits=False):
if isinstance(cards, str):
cards = cards.split(' ')
suits, ranks = 'cdhs', '23456789TJQKA'
ranks_rev = dict((ranks[i], i) for i in range(13))
cards = [(s, tuple(sorted(ranks_rev[c[0]] for c in cards
if c[1] is s))) for s in suits]
cards = [c for c in cards if c[1] != ()]
cards.sort(key=lambda c: (-len(c[1]), c[1]))
if mask_suits:
return [ranks[r] + str(i + 1) for (i, c)
in enumerate(cards) for r in c[1]]
else:
return [ranks[r] + c[0] for c in cards for r in c[1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment