trust

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 """
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
31def 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
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.