| 1 | | def best_hands(hands: list[str]) -> list[str]: # each hand is a string like "4S 5S 7H 8D JC" |
| 2 | | pass |
| 1 | def best_hands(hands: list[str]) -> list[str]: |
| 2 | """Return the best hand(s) from a list of poker hands.""" |
| 3 | if not hands: |
| 4 | return [] |
| 5 | |
| 6 | # Edge Case: Single hand provided |
| 7 | if len(hands) == 1: |
| 8 | return hands |
| 9 | |
| 10 | ranked_hands = [] |
| 11 | for hand in hands: |
| 12 | ranked_hands.append((hand, rank_hand(parse_hand(hand)))) |
| 13 | |
| 14 | # Find the maximum rank |
| 15 | max_rank = max(rank for _, rank in ranked_hands) |
| 16 | |
| 17 | # Filter hands with maximum rank |
| 18 | best = [hand for hand, rank in ranked_hands if rank == max_rank] |
| 19 | |
| 20 | return best |
| 21 | |
| 22 | |
| 23 | def parse_hand(hand_str: str) -> list[tuple]: |
| 24 | """Parse a hand string into a list of tuples (value, suit).""" |
| 25 | # Edge Case: Empty hand string |
| 26 | if not hand_str.strip(): |
| 27 | return [] |
| 28 | |
| 29 | cards = hand_str.split() |
| 30 | parsed_cards = [] |
| 31 | |
| 32 | for card in cards: |
| 33 | # Edge Case: Malformed card string |
| 34 | if len(card) < 2: |
| 35 | raise ValueError(f"Invalid card: {card}") |
| 36 | |
| 37 | suit = card[-1] |
| 38 | value_str = card[:-1] |
| 39 | |
| 40 | # Edge Case: Invalid suit |
| 41 | if suit not in 'SHDC': |
| 42 | raise ValueError(f"Invalid suit: {suit}") |
| 43 | |
| 44 | # Convert value to number |
| 45 | if value_str == 'A': |
| 46 | value = 14 |
| 47 | elif value_str == 'K': |
| 48 | value = 13 |
| 49 | elif value_str == 'Q': |
| 50 | value = 12 |
| 51 | elif value_str == 'J': |
| 52 | value = 11 |
| 53 | else: |
| 54 | try: |
| 55 | value = int(value_str) |
| 56 | # Edge Case: Invalid card value range |
| 57 | if value < 2 or value > 10: |
| 58 | raise ValueError(f"Invalid card value: {value}") |
| 59 | except ValueError: |
| 60 | raise ValueError(f"Invalid card value: {value_str}") |
| 61 | |
| 62 | parsed_cards.append((value, suit)) |
| 63 | |
| 64 | # Edge Case: Hand must have exactly 5 cards |
| 65 | if len(parsed_cards) != 5: |
| 66 | raise ValueError(f"Hand must contain exactly 5 cards, got {len(parsed_cards)}") |
| 67 | |
| 68 | return parsed_cards |
| 69 | |
| 70 | |
| 71 | def rank_hand(hand: list[tuple]) -> tuple: |
| 72 | """Rank a poker hand and return a tuple for comparison. |
| 73 | |
| 74 | Optimized version using efficient pattern matching and reduced redundant calculations. |
| 75 | """ |
| 76 | # Extract values and suits in single pass |
| 77 | values = [] |
| 78 | suits = [] |
| 79 | for card in hand: |
| 80 | values.append(card[0]) |
| 81 | suits.append(card[1]) |
| 82 | |
| 83 | # Sort values once for all comparisons |
| 84 | values.sort(reverse=True) |
| 85 | original_values = values.copy() |
| 86 | |
| 87 | # Check for flush (single set operation) |
| 88 | is_flush = len(set(suits)) == 1 |
| 89 | |
| 90 | # Check for straight with optimized logic |
| 91 | is_straight = False |
| 92 | # Regular straight check (vectorized comparison) |
| 93 | if all(values[i] == values[i+1] + 1 for i in range(4)): |
| 94 | is_straight = True |
| 95 | # Ace-low straight check (single comparison) |
| 96 | elif values == [14, 5, 4, 3, 2]: |
| 97 | is_straight = True |
| 98 | values = [5, 4, 3, 2, 1] # Renumber Ace as 1 for consistency |
| 99 | |
| 100 | # Build value counts efficiently using dictionary comprehension |
| 101 | value_counts = {} |
| 102 | for value in values: |
| 103 | value_counts[value] = value_counts.get(value, 0) + 1 |
| 104 | |
| 105 | # Sort counts once for pattern matching |
| 106 | sorted_items = sorted(value_counts.items(), key=lambda x: (-x[1], -x[0])) |
| 107 | counts = [count for _, count in sorted_items] |
| 108 | sorted_values = [value for value, _ in sorted_items] |
| 109 | |
| 110 | # Optimized pattern matching using count patterns |
| 111 | count_pattern = tuple(counts) |
| 112 | |
| 113 | # Determine hand rank using efficient pattern matching |
| 114 | if is_straight and is_flush: |
| 115 | # Straight flush (including Royal Flush) |
| 116 | return (8, values) |
| 117 | elif count_pattern == (4, 1): |
| 118 | # Four of a kind - use pre-sorted values |
| 119 | return (7, [sorted_values[0], sorted_values[1]]) |
| 120 | elif count_pattern == (3, 2): |
| 121 | # Full house |
| 122 | return (6, [sorted_values[0], sorted_values[1]]) |
| 123 | elif is_flush: |
| 124 | # Flush - use original sorted values for proper comparison |
| 125 | return (5, original_values) |
| 126 | elif is_straight: |
| 127 | # Straight |
| 128 | return (4, values) |
| 129 | elif count_pattern == (3, 1, 1): |
| 130 | # Three of a kind |
| 131 | return (3, [sorted_values[0]] + sorted_values[1:]) |
| 132 | elif count_pattern == (2, 2, 1): |
| 133 | # Two pair |
| 134 | return (2, [sorted_values[0], sorted_values[1], sorted_values[2]]) |
| 135 | elif count_pattern == (2, 1, 1, 1): |
| 136 | # One pair |
| 137 | return (1, [sorted_values[0]] + sorted_values[1:]) |
| 138 | else: |
| 139 | # High card |
| 140 | return (0, original_values) |
| 141 | |
| 142 | # Handled Edge Cases: Empty hands list, single hand, empty hand string, malformed card string, invalid suit, invalid card value range, hand with incorrect number of cards |