def _validate(self): all_cards = set() for hand in self.hands: for suit in SUITS: for card in hand.cards_in_suit(suit): card_identifier = Card.identifier_for_card(suit, card) assert card_identifier not in all_cards, ("Already seen %s" % Card.card_name(suit, card)) all_cards.add(card_identifier) assert len(all_cards) == 52
def old_identifier(self): # We're constructing a 52 digit number in base 4, # converted to base-10, its our identifier. identifier = 0 for position, hand in enumerate(self.hands): for suit, cards in enumerate(hand.cards_by_suit): for card in cards: card_identifier = Card.identifier_for_card(suit, card) identifier += position * pow(4, card_identifier) return str(identifier)
def identifier(self): position_for_card = [None for _ in range(52)] for position, hand in enumerate(self.hands): for suit, cards in enumerate(hand.cards_by_suit): for card in cards: card_identifier = Card.identifier_for_card(suit, card) position_for_card[card_identifier] = position # position_for_card represents a 52-digit number in base 4 # We're going to split it into 4-digit hunks and convert to base 16. identifier = "" hex_chars = '0123456789abcdef' for offset in range(26): # A single hex digit encodes 4 bits where as our previous encoding was 2. hex_index = position_for_card[offset * 2 + 0] * 4 + position_for_card[offset * 2 + 1] identifier += hex_chars[hex_index] return identifier