| 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 | """ |
| 3 | Determine the best poker hand(s) from a list of poker hands. |
| 4 | |
| 5 | Args: |
| 6 | hands: List of strings, each representing a poker hand with 5 cards |
| 7 | |
| 8 | Returns: |
| 9 | List of strings representing the best hand(s) |
| 10 | """ |
| 11 | if not hands: |
| 12 | return [] |
| 13 | |
| 14 | # Edge Case: Single hand provided |
| 15 | if len(hands) == 1: |
| 16 | return hands |
| 17 | |
| 18 | ranked_hands = [] |
| 19 | for hand in hands: |
| 20 | ranked_hands.append((hand, rank_hand(hand))) |
| 21 | |
| 22 | # Find the maximum rank |
| 23 | max_rank = max(rank for _, rank in ranked_hands) |
| 24 | |
| 25 | # Filter hands with maximum rank |
| 26 | best = [hand for hand, rank in ranked_hands if rank == max_rank] |
| 27 | |
| 28 | return best |
| 29 | |
| 30 | |
| 31 | def rank_hand(hand_str: str) -> tuple: |
| 32 | """ |
| 33 | Rank a poker hand and return a tuple that can be used for comparison. |
| 34 | |
| 35 | Returns: |
| 36 | Tuple representing the rank of the hand |
| 37 | """ |
| 38 | # Edge Case: Empty hand string |
| 39 | if not hand_str: |
| 40 | return (0,) |
| 41 | |
| 42 | cards = hand_str.split() |
| 43 | |
| 44 | # Edge Case: Invalid number of cards |
| 45 | if len(cards) != 5: |
| 46 | return (0,) |
| 47 | |
| 48 | # Parse cards |
| 49 | values = [] |
| 50 | suits = [] |
| 51 | |
| 52 | # Edge Case: Invalid card format |
| 53 | for card in cards: |
| 54 | if len(card) < 2 or len(card) > 3: |
| 55 | return (0,) |
| 56 | |
| 57 | if card[-1] in 'SHDC': |
| 58 | suit = card[-1] |
| 59 | value_str = card[:-1] |
| 60 | else: |
| 61 | return (0,) |
| 62 | |
| 63 | # Convert value to number |
| 64 | if value_str == 'A': |
| 65 | value = 14 |
| 66 | elif value_str == 'K': |
| 67 | value = 13 |
| 68 | elif value_str == 'Q': |
| 69 | value = 12 |
| 70 | elif value_str == 'J': |
| 71 | value = 11 |
| 72 | elif value_str.isdigit() and 2 <= int(value_str) <= 10: |
| 73 | value = int(value_str) |
| 74 | else: |
| 75 | return (0,) # Invalid value |
| 76 | |
| 77 | values.append(value) |
| 78 | suits.append(suit) |
| 79 | |
| 80 | # Sort values for easier comparison |
| 81 | values.sort() |
| 82 | |
| 83 | # Check for flush |
| 84 | is_flush = len(set(suits)) == 1 |
| 85 | |
| 86 | # Check for straight |
| 87 | is_straight = False |
| 88 | # Normal straight |
| 89 | if all(values[i] + 1 == values[i+1] for i in range(4)): |
| 90 | is_straight = True |
| 91 | # Wheel straight (A-2-3-4-5) |
| 92 | elif values == [2, 3, 4, 5, 14]: |
| 93 | is_straight = True |
| 94 | values = [1, 2, 3, 4, 5] # Treat Ace as 1 for ranking |
| 95 | values.sort() |
| 96 | |
| 97 | # Count occurrences of each value |
| 98 | value_counts = {} |
| 99 | for value in values: |
| 100 | value_counts[value] = value_counts.get(value, 0) + 1 |
| 101 | |
| 102 | counts = sorted(value_counts.values(), reverse=True) |
| 103 | unique_values = sorted(value_counts.keys(), reverse=True) |
| 104 | |
| 105 | # Determine hand rank |
| 106 | if is_straight and is_flush: |
| 107 | # Straight flush |
| 108 | return (8, values[-1]) # Rank 8, high card of straight |
| 109 | elif 4 in counts: |
| 110 | # Four of a kind |
| 111 | four_kind_value = [v for v, c in value_counts.items() if c == 4][0] |
| 112 | kicker = [v for v, c in value_counts.items() if c == 1][0] |
| 113 | return (7, four_kind_value, kicker) |
| 114 | elif 3 in counts and 2 in counts: |
| 115 | # Full house |
| 116 | three_kind_value = [v for v, c in value_counts.items() if c == 3][0] |
| 117 | pair_value = [v for v, c in value_counts.items() if c == 2][0] |
| 118 | return (6, three_kind_value, pair_value) |
| 119 | elif is_flush: |
| 120 | # Flush |
| 121 | return (5,) + tuple(reversed(values)) |
| 122 | elif is_straight: |
| 123 | # Straight |
| 124 | return (4, values[-1]) |
| 125 | elif 3 in counts: |
| 126 | # Three of a kind |
| 127 | three_kind_value = [v for v, c in value_counts.items() if c == 3][0] |
| 128 | kickers = sorted([v for v, c in value_counts.items() if c == 1], reverse=True) |
| 129 | return (3, three_kind_value) + tuple(kickers) |
| 130 | elif counts.count(2) == 2: |
| 131 | # Two pair |
| 132 | pairs = sorted([v for v, c in value_counts.items() if c == 2], reverse=True) |
| 133 | kicker = [v for v, c in value_counts.items() if c == 1][0] |
| 134 | return (2, pairs[0], pairs[1], kicker) |
| 135 | elif 2 in counts: |
| 136 | # One pair |
| 137 | pair_value = [v for v, c in value_counts.items() if c == 2][0] |
| 138 | kickers = sorted([v for v, c in value_counts.items() if c == 1], reverse=True) |
| 139 | return (1, pair_value) + tuple(kickers) |
| 140 | else: |
| 141 | # High card |
| 142 | return (0,) + tuple(reversed(values)) |
| 143 | |
| 144 | # Handled Edge Cases: empty hands list, single hand, empty hand string, invalid number of cards, invalid card format, invalid card values |