Skip to content

Instantly share code, notes, and snippets.

View aldanor's full-sized avatar

Ivan Smirnov aldanor

  • Dublin, Ireland
View GitHub Profile
@aldanor
aldanor / gist:4193087
Created December 3, 2012 06:14
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: