Boom

Finished
1def best_hands(hands: list[str]) -> list[str]: # each hand is a string like "4S 5S 7H 8D JC"
2 pass
1def 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
23def 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
71def 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
Test NameStatus
test_a_straight_beats_three_of_a_kind
Pass
test_a_tie_has_multiple_winners
Pass
test_aces_can_end_a_straight_10_j_q_k_a
Pass
test_aces_can_end_a_straight_flush_10_j_q_k_a
Pass
test_aces_can_start_a_straight_a_2_3_4_5
Pass
test_aces_can_start_a_straight_flush_a_2_3_4_5
Pass
test_aces_cannot_be_in_the_middle_of_a_straight_flush_q_k_a_2_3
Pass
test_aces_cannot_be_in_the_middle_of_a_straight_q_k_a_2_3
Pass
test_both_hands_have_a_flush_tie_goes_to_high_card_down_to_the_last_one_if_necessary
Pass
test_both_hands_have_a_full_house_tie_goes_to_highest_ranked_triplet
Pass
test_both_hands_have_a_straight_flush_tie_goes_to_highest_ranked_card
Pass
test_both_hands_have_four_of_a_kind_tie_goes_to_high_quad
Pass
test_both_hands_have_the_same_pair_high_card_wins
Pass
test_both_hands_have_three_of_a_kind_tie_goes_to_highest_ranked_triplet
Pass
test_both_hands_have_two_identically_ranked_pairs_tie_goes_to_remaining_card_kicker
Pass
test_both_hands_have_two_pairs_highest_ranked_pair_wins
Pass
test_both_hands_have_two_pairs_that_add_to_the_same_value_win_goes_to_highest_pair
Pass
test_both_hands_have_two_pairs_with_the_same_highest_ranked_pair_tie_goes_to_low_pair
Pass
test_both_hands_with_a_straight_tie_goes_to_highest_ranked_card
Pass
test_even_though_an_ace_is_usually_high_a_5_high_straight_flush_is_the_lowest_scoring_straight_flush
Pass
test_even_though_an_ace_is_usually_high_a_5_high_straight_is_the_lowest_scoring_straight
Pass
test_flush_beats_a_straight
Pass
test_four_of_a_kind_beats_a_full_house
Pass
test_full_house_beats_a_flush
Pass
test_highest_card_out_of_all_hands_wins
Pass
test_highest_pair_wins
Pass
test_multiple_hands_with_the_same_high_cards_tie_compares_next_highest_ranked_down_to_last_card
Pass
test_one_pair_beats_high_card
Pass
test_single_hand_always_wins
Pass
test_straight_flush_beats_four_of_a_kind
Pass
test_three_of_a_kind_beats_two_pair
Pass
test_two_pairs_beats_one_pair
Pass
test_two_pairs_first_ranked_by_largest_pair
Pass
test_winning_high_card_hand_also_has_the_lowest_card
Pass
test_with_multiple_decks_both_hands_have_a_full_house_with_the_same_triplet_tie_goes_to_the_pair
Pass
test_with_multiple_decks_both_hands_with_identical_four_of_a_kind_tie_determined_by_kicker
Pass
test_with_multiple_decks_two_players_can_have_same_three_of_a_kind_ties_go_to_highest_remaining_cards
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.