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_index, cards in enumerate(hand.cards_by_suit_index): for card in cards: suit = Suit.from_index(suit_index) 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_index, hand in enumerate(self.hands): for suit_index, cards in enumerate(hand.cards_by_suit_index): for card in cards: suit = Suit.from_index(suit_index) card_identifier = Card.identifier_for_card(suit, card) position_for_card[card_identifier] = position_index # 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