In poker, you have 5 cards. There are 10 kinds of poker hands (from highest to lowest):
royal flush - ace, king, queen, jack and ten, all in the same suit
straight flush - five cards of the same suit in sequence, such
as 10,9,8,7,6 of clubs; ace can be counted both as the highest card or as the
lowest card - A,2,3,4,5 of hearts is a straight flush. But 4,3,2,A,K of hearts is not a straight flush - it's just a flush.
four of a kind - four cards of the same rank, such as four kings.
full house - three cards of one rank plus two cards of another rank
flush - five cards of the same suit (but not a straight flush)
straight - five cards in order - just like the straight flush, but mixed suits
three of a kind - three cards of one rank and two other cards
two pairs - two cards of one rank, two cards of another rank, and one more card
pair - two cards of the same rank
high card - none of the above
Write a program that will help you play poker by telling you what kind of hand you have.
Input
The first line of input contains the number of test cases (no more than 20). Each test case consists of one line - five space separated cards. Each card is represented by a two-letter (or digit) word. The first character is the rank (A,K,Q,J,T,9,8,7,6,5,4,3 or 2), the second character is the suit (S,H,D,C standing for spades, hearts, diamonds and clubs). The cards can be in any order (but they will not repeat).
Output
For each test case output one line describing the type of a hand, exactly like in the list above.
We need to write a program that reads multiple test cases, each containing five cards, and determines the poker hand type. The output must match exactly one of the ten hand types as listed. We need to handle up to 20 test cases.
We need to parse cards: rank and suit. Ranks: A, K, Q, J, T, 9,8,7,6,5,4,3,2. Suits: S,H,D,C.
We need to identify the hand exactly according to definitions. The hands from highest to lowest: royal flush, straight flush, four of a kind, full house, flush, straight, three of a kind, two pairs, pair, high card.
We must output exactly the string: "royal flush", "straight flush", "four of a kind", "full house", "flush", "straight", "three of a kind", "two pairs", "pair", "high card". Note: "two pairs" (plural) and "high card". There's a space in "royal flush", "straight flush", "full house", "high card", etc.
Implementation steps for each hand:
Given five cards:
Extract ranks and suits.
We need to handle rank ordering: for straight checking, we need numeric values. Map: A can be high (14) or low (1) for straight (A,2,3,4,5). Other ranks: 2->2, 3->3, 4->4, 5->5,6->6,7->7,8->8,9->9, T->10, J->11, Q->12, K->13, A->14 (or 1). But note: For royal flush, it's specifically A,K,Q,J,10 same suit. That is essentially a straight flush with Ace high (10,J,Q,K,A). So it's a special case of straight flush.
But we need to differentiate royal flush from straight flush. So if we have straight flush and the highest card is Ace and lowest is 10? Actually royal flush is A,K,Q,J,T all same suit. That is a straight flush with ranks 10,J,Q,K,A (i.e., high straight). So we can detect straight flush, then if the highest rank is Ace and lowest is 10? Alternatively if the ranks are exactly T,J,Q,K,A. But note that Ace could also be low for straight flush (A,2,3,4,5). That is not royal flush. So we can check: if straight flush and the highest card is Ace and the lowest card is 10? Actually the sequence for royal flush: 10, J, Q, K, A. So the set of ranks must be {10, J, Q, K, A} i.e., numeric values 10,11,12,13,14. So if we have straight flush and the ranks are exactly 10,11,12,13,14 then it's royal flush.
Alternatively, detect flush (all same suit) and straight (consecutive ranks) and then if the straight's high card is Ace and low card is 10? Actually if flush and straight and high card is Ace and low card is 10? But Ace high straight includes 10->A. So low card = 10. So we can check.
Edge cases: straight with Ace low: A,2,3,4,5; numeric values 1,2,3,4,5 (if we map Ace as 1). That is a straight flush if suits match, else straight.
So to detect straight we need to handle both Ace high and low.
Approach:
Map rank to numeric value: for standard ordering: '2'=2,...'9'=9, 'T'=10, 'J'=11, 'Q'=12, 'K'=13, 'A'=14. Also, for Ace low straight, we need to consider Ace as 1 when checking if {A,2,3,4,5} forms a straight. So we can create a list of numeric values, sort them, then check if they are consecutive (with difference 1). If not, also check for Ace low: if the set contains Ace (value 14) and also 2,3,4,5, then treat Ace as 1 and see if {1,2,3,4,5} is straight. For that, we can create a modified list: if Ace present, create a copy where Ace is 1, sort, and check consecutiveness. But careful: only the Ace low straight is possible; we shouldn't treat Ace as both high and low in other combos.
Alternatively, compute set of ranks and check for straight condition: For sorted values v[0..4], if v[i] = v[0] + i for all i, then straight. If not, and if v contains 14 (Ace), then replace 14 with 1, sort, and check again.
Ok.
Now identification order: We need to output the highest hand that matches. So we should check in order of ranking? Actually we need to output exactly the hand type. So we need to determine which one it is. We can check from highest to lowest, and the first that matches is the hand type. But careful: some hands are subsets of others? For example, a flush could also be a straight flush? Actually a straight flush is both a straight and a flush. So if we check flush first, it would match, but it's actually a straight flush, which is higher. So we need to check in descending order: royal flush > straight flush > four of a kind > full house > flush > straight > three of a kind > two pairs > pair > high card.
So we should test in that order.
Now details:
Royal flush: All same suit? Actually must be flush and straight with specific ranks: 10,J,Q,K,A. So easiest: check if all suits same; and the set of ranks is exactly {10, J, Q, K, A}. Use numeric: sorted values = [10,11,12,13,14]. So if flush and sorted ranks exactly that, it's royal flush. But note that if it's royal flush, it's also a straight flush. So we need to check royal flush first.
Straight flush: All same suit and the ranks form a straight (including Ace low). So: flush true and straight true. But not royal flush (i.e., if it's a straight flush but not the specific royal ranks). So after checking royal flush (if fails), then check straight flush: flush && straight.
Four of a kind: There is a rank that appears 4 times. (Five cards, so one rank appears 4 times, and the fifth card is any other rank.)
Full house: One rank appears 3 times, another rank appears 2 times. So we can count frequencies.
Flush: All same suit, but not a straight flush (but we already check straight flush before, so if it fails straight flush but is flush, then it's flush.)
Straight: Ranks form a straight (with Ace low considered), but not a flush (since flush is higher and we already checked). So we need to ensure it's not a flush? Actually straight is defined as five cards in order, mixed suits. If it were both straight and flush, it would be straight flush, which is higher, and we would have caught it earlier (since straight flush is above straight). So if we check straight after flush and straight flush fails, then it's safe: we just need to check if it's straight. But note that a straight flush also is a straight, but we've already excluded it because we check straight flush earlier? Actually order: royal flush -> straight flush -> four of a kind -> full house -> flush -> straight. So if it's a straight flush, it will be caught as straight flush before reaching flush or straight. So if we get to straight, it means it's not a straight flush nor flush? Not exactly: The order after straight flush, we go to four of a kind, full house, flush, straight. So if a hand is both flush and straight, it would be straight flush, caught earlier, so it won't be considered flush or straight later. So correct: we can check straight independently, assuming it's not a flush. But what if it's a straight but also a flush? That can't happen because it would have been caught earlier. So for straight detection, we can just check the straight condition.
But careful: What if it's a straight flush but not royal flush? It's caught as straight flush and we output that, not going further. So fine.
Three of a kind: Exactly three cards of same rank, and the other two are of different ranks (and not a full house). So we need to ensure it's not full house. Full house is higher, so we check full house before three of a kind. So when we check three of a kind, we need to have exactly three of a kind and not two pair? Actually three of a kind can coexist with a pair? That's full house. So three of a kind is when there is a rank with count 3, and the other two ranks are different from each other and from the triple. So we can test: frequencies: one rank count=3, the other two ranks count=1 each.
Two pairs: Two cards of one rank, two cards of another rank, and one card of a third rank. So frequencies: two ranks with count 2, one rank with count 1.
Pair: Exactly one rank with count 2, and the other three ranks are all different (i.e., frequencies: one count=2, three counts=1). But also not two pairs, etc.
High card: None of the above.
Now we need to implement these checks robustly.
Additional nuance: For straight detection, we must consider that there are no duplicate ranks (except in straight? Actually straight must have all five ranks distinct, otherwise it's not a straight (except if duplicate ranks, it can't be a straight because they would not be consecutive distinct). So we can check if set of ranks size is 5 and they are consecutive. But if there are duplicates (like a pair), it's not a straight anyway. So for straight detection, we can first check if all ranks are distinct. If not, it's not a straight (except maybe if using wild cards, but no). So we can early reject. But to be safe, we can still compute.
Implementation plan for each hand:
Parse input: test cases number n, then for each case, read a line of five card strings (separated by spaces). Might be multiple lines? Input format: each test case consists of one line. So we can read line, split.
For each card, rank_char = card[0], suit_char = card[1].
Convert rank_char to numeric rank using mapping: let's define dict: {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':11, 'Q':12, 'K':13, 'A':14}. Also for Ace low, we can treat as 1 if needed.
Store suits list.
Now checks:
Royal flush:
Check if all suits same.
Extract numeric ranks, sort.
If sorted ranks equals [10,11,12,13,14] -> royal flush. (Since all same suit already, it's flush and straight.)
else not.
But careful: Could there be a royal flush with Ace low? No, royal flush is specific high straight flush. So fine.
Straight flush:
Check if all suits same.
Check if ranks form a straight (including Ace low).
If both true, it's straight flush (and we already know not royal flush because the ranks weren't exactly [10,11,12,13,14] or we checked royal first). So order: check royal flush first. If royal, output. Else continue.
But if we check straight flush after royal flush fails, we still need to check flush and straight. So we can compute flush = all same suit, and straight = is_straight(ranks). Then:
if flush and straight:
if royal? but already not royal, so straight flush.
Four of a kind:
Count frequencies of ranks. If any count == 4 -> true.
Full house:
Frequencies: one count ==3 and one count ==2 -> true.
Flush:
flush true (all same suit) and not straight flush (but we already checked straight flush earlier, so if we reached here, straight flush is false; but we still need flush true. So we can just test flush true.
Straight:
straight true (and not flush? Actually if flush true, it would have been caught as straight flush? But we already checked straight flush earlier, and it would have been true if both flush and straight. Since we are now at step 6, straight flush is false, so either not flush, not straight, or both? But if both flush and straight, straight flush would be true. So if straight is true, it's guaranteed that flush is false? Actually could be both but we didn't catch straight flush? Only if we missed it. But if we check straight flush earlier, we would have caught it. So it's safe: we just check straight true.
Three of a kind:
frequencies: one count ==3, and the other two counts ==1 (i.e., exactly three of a kind). But we must ensure not full house (full house already caught). So we can check: if there is a rank with count 3 and there is no rank with count 2 (i.e., the other two ranks counts are 1 each). So check: counts values contain 3 and not contain 2.
Two pairs:
counts: two ranks with count 2, one rank with count 1.
Pair:
counts: one rank with count 2, and all others count 1 (i.e., number of ranks with count 2 ==1, and total distinct ranks =4 (since one pair and three other singles). Alternatively, counts: exactly one 2 and three 1's.
High card:
else.
We need to ensure we handle tie-breaking: not needed; just classification.
Implementation details:
We'll create a function is_straight(ranks_list) that returns True if the five cards form a straight. Steps:
Convert to list of integers using mapping (A=14).
If all distinct? Not necessary but could speed.
Sort.
Check if each consecutive difference == 1.
If yes, straight.
Else, if 14 present (Ace), replace with 1, sort, and check again.
Edge: 5 distinct ranks needed; but if there are duplicates, the check will fail anyway because after sorting differences might be 0? Actually duplicates might still produce consecutive? No, if there's a pair, the set size is less than 5, but if we use list of values including duplicates, we would have duplicates and differences might be 0, so not all 1. But we need to treat as not straight anyway. So we can simply sort and check differences; if any diff 0, it's not consecutive strictly increasing by 1. Actually if there is a pair, there will be duplicate numbers; after sorting, consecutive values may be equal, so difference can be 0, so not all 1. So it's fine.
But also consider Ace low: For Ace low straight, we need to treat Ace as 1. After replacing Ace (14) with 1, we may have duplicates? Only one Ace, so no duplicates. So it's fine.
Now note: In straight flush, we use same is_straight.
Now for flush: simply check if length of set(suits) == 1.
Now for four of a kind: count frequencies; we can use collections.Counter.
Now for full house: check sorted counts == [2,3] (if we get values sorted, e.g., counts = [2,3] but careful: full house yields two distinct ranks, one with count 3, other with count 2. So sorted list of frequencies = [2,3] after we extract from Counter? Actually Counter values: e.g., for full house, values = [3,2]. Sorted = [2,3].
Similarly for two pairs: three distinct ranks, frequencies: two 2's and one 1 -> sorted = [1,2,2].
Three of a kind: three distinct ranks? Actually three of a kind: three cards same, and two other distinct cards -> total distinct ranks = 3, frequencies = [1,1,3] sorted = [1,1,3].
Four of a kind: distinct ranks = 2, frequencies = [1,4] sorted = [1,4] or [4,1].
High card: distinct ranks = 5, not a straight? Actually high card means none of the above. Could be a non-straight with all distinct and not flush. So frequencies sorted = [1,1,1,1,1]. But also we need to ensure it's not a straight or flush, which we check earlier.
So we can implement classification based on conditions.
Now order: We'll check:
if is_flush and is_straight:
if sorted_ranks == [10,11,12,13,14]: output "royal flush"
else: output "straight flush"
elif four_of_a_kind: output "four of a kind"
elif full_house: output "full house"
elif is_flush: output "flush"
elif is_straight: output "straight"
elif three_of_a_kind: output "three of a kind"
elif two_pairs: output "two pairs"
elif pair: output "pair"
else: output "high card"
But careful: In the above, we put is_flush and is_straight together at top. But what about a hand that is both flush and straight but is also royal flush? The if block catches it. So fine.
But note: If we have a flush and straight but not the specific royal ranks, we output "straight flush". That's correct.
Now, we need to be careful about ordering: After checking straight flush, we check four of a kind, etc. However, what if a hand is both a straight flush and also four of a kind? Not possible because four of a kind requires four same rank, which would conflict with straight flush unless duplicates, but can't have both. So fine.
Now implement:
Pseudo:
def hand_type(cards):
rank_map = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'T':10,'J':11,'Q':12,'K':13,'A':14}
suits = [c[1] for c in cards]
ranks = [rank_map[c[0]] for c in cards]
for straight detection
is_flush = (len(set(suits)) == 1)
Straight detection function
def is_straight(ranks):
vals = sorted(ranks)
if all(vals[i] + 1 == vals[i+1] for i in range(4)):
return True
# Check Ace low
if 14 in vals:
# replace Ace (14) with 1
vals2 = sorted([1 if r == 14 else r for r in ranks])
if all(vals2[i] + 1 == vals2[i+1] for i in range(4)):
return True
return False
straight = is_straight(ranks)
Royal flush check
if is_flush and straight:
if sorted(ranks) == [10,11,12,13,14]:
return "royal flush"
else:
return "straight flush"
Count ranks frequencies
from collections import Counter
count = Counter(ranks)
freq = sorted(count.values())
if freq == [1,4] or freq == [4,1]: # four of a kind
return "four of a kind"
if freq == [2,3] or freq == [3,2]:
return "full house"
if is_flush:
return "flush"
if straight:
return "straight"
if freq == [1,1,3]:
return "three of a kind"
if freq == [1,2,2]:
return "two pairs"
if freq == [1,1,1,2]:
return "pair"
return "high card"
That should work.
But we need to confirm edge cases:
Straight flush with Ace low: is_flush true, straight true, sorted_ranks would be [1?] Actually we convert Ace to 14 initially. So sorted_ranks = [2,3,4,5,14] maybe? Actually for Ace low straight, if we treat Ace as 14, sorted ranks will be [2,3,4,5,14] which is not consecutive. But our is_straight function handles Ace low by converting Ace to 1. So it returns true. In the top check, we have is_flush and straight true, and we check if sorted(ranks) == [10,11,12,13,14] to differentiate royal. For Ace low, sorted(ranks) = [2,3,4,5,14] not equal to royal, so it would be classified as straight flush, which is correct. So fine.
For flush detection, if it's flush but also straight, we already catch at top. So flush output only if not straight flush.
For straight detection, we only output straight if not a straight flush (since we already ruled that out) and if straight true. That's correct.
What about a hand that is both flush and straight but we didn't detect it because our flush and straight check is separate? Actually we catch it at top because we check both together. So fine.
Edge case: Could there be a hand that is both a flush and a straight but with Ace low and also the ranks are 10,J,Q,K,A? That's royal flush, caught by the sorted ranks condition.
For four of a kind: freq = [1,4] or [4,1]; but note that if we have four of a kind, we have two distinct ranks, so freq will be [1,4] after sorting. So ok.
For full house: freq = [2,3] after sorting, because we have two distinct ranks, counts 2 and 3. So check freq == [2,3] (since sorted gives [2,3]). Could also be [3,2] before sorting, but we sort freq, so it becomes [2,3]. So we can just check freq == [2,3].
For three of a kind: freq sorted = [1,1,3]. So check freq == [1,1,3].
For two pairs: freq sorted = [1,2,2]. So check freq == [1,2,2].
For pair: freq sorted = [1,1,1,2].
For high card: freq sorted = [1,1,1,1,1].
But note: high card could also be a straight? Actually if it's a straight, we would have caught earlier. Similarly flush. So high card is only when none of the above.
One subtlety: Our is_straight function might consider a hand with duplicate ranks and still return true? Example: [2,2,3,4,5] sorted: [2,2,3,4,5]; differences: 0,1,1,1; not all 1. So false. If we replace Ace low? No. So it's fine.
But consider Ace low straight with Ace as 1: [1,2,3,4,5] distinct. Works.
Now we need to handle input reading. We'll read integer n, then loop n times. For each line, we split into tokens (cards). There may be extra spaces? Usually fine.
Now edge case: Straight flush with Ace low: e.g., 5H 4H 3H 2H AH -> ranks: 5,4,3,2,14. is_flush true, is_straight? After Ace low conversion, [1,2,3,4,5] straight, so straight true. sorted ranks = [2,3,4,5,14] not equal to [10,...], so output "straight flush". Correct.
Edge case: Flush but not straight: all hearts but ranks not consecutive. e.g., AH 2H 5H 8H JH -> flush true, straight false. So after top check fails (since straight false), four of a kind false, full house false, then is_flush true -> output "flush". Good.
Edge case: Straight but not flush: e.g., 2S 3H 4D 5C 6S -> straight true, flush false. After earlier checks, output "straight". Good.
Edge case: Three of a kind: e.g., 2S 2H 2D 5C 7S -> freq [1,1,3] -> three of a kind. Good.
Edge case: Two pairs: e.g., 2S 2H 3D 3C 5S -> freq [1,2,2] -> two pairs.
Edge case: High card: e.g., AS KS QS JS 9C? But that's flush? Actually all spades except 9C? Not all same suit, but ranks distinct and not straight? A,K,Q,J,9 not consecutive. So high card.
Now note: What about a hand that is a straight but also a flush? Already caught earlier. So fine.
Now confirm that our classification ordering matches exactly the problem's list: royal flush, straight flush, four of a kind, full house, flush, straight, three of a kind, two pairs, pair, high card. Yes.
Now implement in Python.
We should consider reading input: the first line might have trailing spaces; use input().strip().split().
Number of test cases up to 20, so simple.
Now, we need to be careful: The input cards are separated by spaces; each card is two characters; they guarantee no repeats.
Now implement.
Potential pitfalls:
In straight detection, we must treat Ace as low only when Ace is present and the other cards are 2,3,4,5. Our method: check with original values (A=14) first; if that fails and 14 present, convert to 1 and re-check. That's fine.
However, what about hand like A,2,3,4,6? With Ace low, converted to [1,2,3,4,6] not consecutive, so not straight. With Ace high, [2,3,4,6,14] not consecutive, so not straight. So correct.
What about hand with duplicate ranks but still could be considered straight if we ignore duplicates? No, straight requires five distinct ranks in sequence. So duplicates automatically fail.
For the royal flush check, we used sorted(ranks) == [10,11,12,13,14]. That's fine because we haven't converted Ace to 1. But note: If the hand is royal flush, the suits are all same and ranks exactly those. So okay.
One nuance: The problem says: "royal flush - ace, king, queen, jack and ten, all in the same suit". So that's exactly T,J,Q,K,A same suit. So our check is correct.
Now check for straight flush: They allow Ace low straight flush: A,2,3,4,5 of same suit. So our detection works.
Now confirm that full house is correctly identified: "three cards of one rank plus two cards of another rank". So counts [3,2] -> full house. Not that there could be other combinations? No.
Now two pairs: "two cards of one rank, two cards of another rank, and one more card". So counts [2,2,1] -> two pairs. Good.
Now pair: "two cards of the same rank". counts [2,1,1,1] -> pair.
Now high card: none of above.
All good.
Now we output exactly the strings: case sensitive? They are all lowercase as in example: "royal flush", "pair", "full house". So we'll output exactly that.
Now we must implement the code.
Let's write it.
We'll define a function classify_hand(cards) that returns the string.
We'll parse each test case.
Now double-check: Could there be a scenario where we have a straight but also four of a kind? Not possible. Straight requires five distinct ranks; four of a kind requires a rank appears four times, so distinct ranks <=2, so cannot be a straight. So ordering doesn't conflict.
Similarly full house vs straight: full house has only two distinct ranks, not five distinct, so cannot be straight. So checks are exclusive enough.
But there is scenario: A flush could also be a straight flush, but we handle that at top.
Edge: A hand could be both a straight and a flush but not a royal flush (i.e., straight flush). Caught at top.
Edge: A hand could be both a flush and also a three of a kind? That's possible? Flush is all same suit; three of a kind is about ranks. If it's flush and three of a kind, it's still a flush, because flush is higher than three of a kind. So our order will output flush before three of a kind, which is correct according to ranking (flush > three of a kind). So we need to check flush before three of a kind. In our order, after checking four of a kind and full house, we check flush, then straight, then three of a kind. So if it's a flush and three of a kind, we output flush, which is correct. Similarly if it's a straight and three of a kind? Straight is higher than three of a kind, so we check straight before three of a kind, which is correct.
Now one more nuance: The problem lists hands in descending order: royal flush (highest) down to high card (lowest). So our output should be the specific hand type that matches. We need to ensure we don't mistakenly classify a higher hand as lower. Since we test in descending order, we are safe.
Now, let's think about any tricky edge case: "straight flush" vs "royal flush": Our top check uses if is_flush and is_straight: then if sorted(ranks) == [10,11,12,13,14] then royal flush else straight flush. This works, but what about a hand that is both a flush and a straight but not a straight flush? Actually if it's both flush and straight, it's always a straight flush by definition, except if we consider the possibility of a "royal flush" which is a subset. So this classification is correct.
But note: "straight flush" definition includes the Ace low case, as stated. So our is_straight includes Ace low, so Ace low straight flush will be classified as straight flush. Good.
Now, check for flush: is_flush and not (is_flush and is_straight). But we have already checked that combination at top, so if we reach flush check later, is_flush may be true, but we must ensure straight is false (since if straight were true, we would have already output straight flush). So it's safe to output flush if is_flush true.
Now, check for straight: we need to ensure that is_flush is false (since if it were true and straight true we would have output straight flush). But if is_flush true and straight false, it's just flush, and we already output flush before straight. So our order: flush check before straight. So if is_flush true, we output flush and don't reach straight. If is_flush false and straight true, we output straight. That's correct.
Now, check three of a kind: We must ensure it's not full house. Since we check full house before three of a kind, fine.
Now, two pairs: Must not be full house; full house has two distinct ranks, but two pairs has three distinct ranks. So counts differentiate.
Now, pair: Must not be two pairs; two pairs has two ranks with count 2. So counts differentiate.
Now high card: else.
Now we need to be careful about counting frequencies: Use Counter from collections.
Now, implement.
Potential bug: In is_straight, we should use a set to ensure no duplicates? Actually duplicates will cause differences not all 1, so it fails. But there's a subtle case: If there are duplicates, but after converting Ace to 1, it could become straight? For example, hand: A,A,2,3,4? That's not a straight because duplicate A. After conversion: two Aces become two 1's? But we only convert Ace (14) to 1; if there are two Aces, but cards are unique (no repeats), so can't have two Aces. So duplicates only if there are pairs, etc. But is_straight should return false for any hand with duplicates. In our approach, if we have duplicate ranks, e.g., two 5's, sorted list will contain duplicates, and differences may be 0, so not all 1. However, consider duplicate values but still maybe consecutive? Example: [2,3,4,5,5] sorted = [2,3,4,5,5]; differences: 1,1,1,0 -> not all 1. So false. Also Ace low conversion: [14,14,2,3,4] after conversion to 1 would be [1,1,2,3,4] sorted [1,1,2,3,4] differences:0,1,1,1 -> not all 1. So false. So fine.
But we need to ensure that our is_straight function returns true only for 5 distinct consecutive ranks. That's correct.
Now, also we need to be careful about sorting for the royal flush check. We used sorted(ranks) == [10,11,12,13,14]. However, ranks are numbers from mapping: 'T':10, J:11, Q:12, K:13, A:14. So correct.
But what about Ace low straight? sorted ranks would be [2,3,4,5,14] which is not equal.
Now, I think it's correct.
Now we can write the code.
Testing with some additional cases:
Straight flush (non-royal): 5S 6S 7S 8S 9S -> ranks [5,6,7,8,9], suits all S, flush and straight true, sorted not royal, so "straight flush". Good.
Four of a kind: KS KH KD KC 2H -> ranks [13,13,13,13,2], freq [1,4] -> four of a kind.
Full house: 2H 2D 2C 5H 5D -> freq [2,3] -> full house.
Flush: 2H 5H 7H 9H JH -> all hearts, not straight -> flush.
Straight: 2H 3D 4C 5S 6H -> straight, not flush -> straight.
Three of a kind: 2H 2D 2C 5H 7D -> freq [1,1,3] -> three of a kind.
Two pairs: 2H 2D 5C 5D 9S -> freq [1,2,2] -> two pairs.
Pair: 2H 2D 5C 7D 9S -> freq [1,1,1,2] -> pair.
High card: 2H 4D 6C 8S TH -> distinct, not straight, not flush -> high card.
Edge: A-2-3-4-5 straight but not flush: AH 2S 3D 4C 5H -> is_straight? With Ace low, returns true. Suits not all same, so straight. Output "straight". Good.
Edge: A-K-Q-J-10 straight but not flush: all different suits, is_straight true? sorted original: [10,11,12,13,14] yes straight. suits not all same, so straight. Output "straight". Good. (Not royal because not flush.)
Edge: A-K-Q-J-10 all same suit -> royal flush.
Edge: A-2-3-4-5 all same suit -> straight flush.
Now what about a hand that is both a straight and a flush but with Ace low? Already straight flush.
Now all good.
But there is also the possibility of "straight flush" being misclassified as "flush" or "straight" if we don't check both together. We do.
Now test with "straight flush" with Ace low: 5H 4H 3H 2H AH -> ranks [5,4,3,2,14]; flush true; is_straight? After converting Ace to 1, [1,2,3,4,5] straight, so true; sorted ranks = [2,3,4,5,14] not equal royal; output "straight flush". Good.
Now check if we incorrectly treat a hand like "10,J,Q,K,A" as straight flush if it's not flush? No, because we require both flush and straight.
Now note: The order of checking four of a kind and full house before flush and straight is correct because four of a kind and full house are higher than flush and straight.
But we must consider: Could a hand be both a straight and a four of a kind? No.
Now about high card: Should be when none of above. For instance, a hand with all distinct ranks, not flush, not straight. That's high card.
Now implement.
Potential edge: What about a hand like A,2,3,4,5 with suits all same? That's straight flush, as we said.
Now we need to ensure that our is_straight function doesn't mistakenly treat A,10,J,Q,K as Ace low straight? That is Ace high straight, it will be caught by the first check (with Ace as 14) because sorted = [10,11,12,13,14] consecutive. So no need to convert for that case.
Now note: For Ace low straight, the first check fails because sorted includes 14 and low numbers; second check with Ace as 1 passes.
Now one more nuance: The straight flush definition says "ace can be counted both as the highest card or as the lowest card - A,2,3,4,5 of hearts is a straight flush. But 4,3,2,A,K of hearts is not a straight flush - it's just a flush." Our is_straight with Ace low conversion will treat A,2,3,4,5 as straight, but 4,3,2,A,K (ranks 4,3,2,14,13) sorted = [2,3,4,13,14] -> not straight, and with Ace as 1: [1,2,3,4,13] sorted [1,2,3,4,13] not consecutive. So correct.
Now we also need to consider that suits are letters: S,H,D,C.
Now code.
Potential issues: In the input, cards are separated by spaces, maybe multiple spaces; using split() works.
Now reading number of test cases: first line integer. Then for each test case, we read a line. Might be empty lines? Possibly, but we can use while loop to read non-empty lines. But typical UVA style, each test case is on its own line, no blank lines. We'll use input().strip() and if empty, maybe continue. But not needed.
Now writing function:
We'll define rank_map as a dictionary.
Implementation:
import sys
from collections import Counter
def classify_hand(cards):
rank_map = {'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'T':10,'J':11,'Q':12,'K':13,'A':14}
suits = [c[1] for c in cards]
ranks = [rank_map[c[0]] for c in cards]
is_flush = len(set(suits)) == 1
code
def is_straight(r):
vals = sorted(r)
# Check normal
if all(vals[i] + 1 == vals[i+1] for i in range(4)):
return True
# Check Ace low
if 14 in vals:
# Replace Ace with 1
vals2 = [1 if x == 14 else x for x in r]
vals2.sort()
if all(vals2[i] + 1 == vals2[i+1] for i in range(4)):
return True
return False
straight = is_straight(ranks)
# Royal flush check (special case of straight flush)
if is_flush and straight:
if sorted(ranks) == [10,11,12,13,14]:
return "royal flush"
else:
return "straight flush"
# Count frequencies
cnt = Counter(ranks)
freq = sorted(cnt.values())
if freq == [1,4]:
return "four of a kind"
if freq == [2,3]:
return "full house"
if is_flush:
return "flush"
if straight:
return "straight"
if freq == [1,1,3]:
return "three of a kind"
if freq == [1,2,2]:
return "two pairs"
if freq == [1,1,1,2]:
return "pair"
return "high card"
Then read input:
def main():
import sys
data = sys.stdin.read().strip().splitlines()
if not data:
return
t = int(data[0].strip())
idx = 1
out_lines = []
for _ in range(t):
line = data[idx].strip()
idx += 1
# There may be empty lines? skip if empty but ensure we get non-empty
while line == '' and idx < len(data):
line = data[idx].strip()
idx += 1
cards = line.split()
out_lines.append(classify_hand(cards))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
But we need to be careful: The test cases lines might have leading/trailing spaces; strip() and split() will handle.
Alternatively, we can use input() in loop. Since it's small, we can do:
t = int(input().strip())
for _ in range(t):
cards = input().strip().split()
print(classify_hand(cards))
That's simpler and avoid blank lines issues.
But sometimes there are blank lines in input; using input() will read next line, which might be empty. We could handle with while loop reading until we get a non-empty line. But the problem states each test case consists of one line - five space separated cards. So there should be no blank lines. So it's fine.
Thus final code:
def poker_hand():
rank_map = {...}
...
But we need to output each result on its own line.
Now test with sample.
Should work.
Now consider potential pitfalls: The mapping for 'T' is 10, but we must ensure we treat 'T' as rank character. Yes.
Now about "two pairs": The output string is "two pairs" (with a space). Our return string is "two pairs". Yes.
Now about "full house": "full house". Yes.
Now about "high card": "high card". Yes.
Now think about the order of checks: Should we check four of a kind before full house? According to ranking, four of a kind is higher than full house. So yes.
Now think about "three of a kind" vs "two pairs": three of a kind is higher than two pairs. So check three of a kind before two pairs.
Now check "pair" vs "high card": pair higher.
Now all good.
Now consider borderline cases:
A hand with both a flush and a straight but not royal: e.g., 9C TC JC QC KC (all clubs). This is a straight flush. It will be caught in the first block. is_flush true, straight true, sorted ranks = [9,10,11,12,13] (since 9,10,J,Q,K) not equal to [10,11,12,13,14], so output "straight flush". Good.
A hand with a flush and a straight but the straight is Ace low: 5D 4D 3D 2D AD. Sorted ranks: [2,3,4,5,14] not equal to royal, so straight flush.
Now what about a hand that is both a flush and also a straight but also a four of a kind? Not possible.
Now check that we are using sorted(ranks) for royal flush check. Should we use the original ranks (with Ace=14) for the sorted list? Yes, because royal flush has ranks T,J,Q,K,A which map to 10,11,12,13,14. So sorted = [10,11,12,13,14] exactly. But note: For royal flush, the cards might be in any order, so sorted will produce that list. So that works.
Now what about a hand with all same suit but ranks [10,J,Q,K,A]? That's royal flush. Good.
Now what about a hand with all same suit and ranks [A,10,J,Q,K]? Sorted yields same.
Now we must ensure that the is_straight function for royal flush returns true. Since it's a straight, so straight true.
Now code complete.
But there is a nuance: The problem says "straight flush - five cards of the same suit in sequence, such as 10,9,8,7,6 of clubs; ace can be counted both as the highest card or as the lowest card - A,2,3,4,5 of hearts is a straight flush. But 4,3,2,A,K of hearts is not a straight flush - it's just a flush." So they specify that A,2,3,4,5 is a straight flush. Our is_straight handles that.
Now verify 4,3,2,A,K: ranks 4,3,2,14,13 sorted [2,3,4,13,14] not consecutive; with Ace low conversion: [1,2,3,4,13] sorted [1,2,3,4,13] not consecutive, so not straight. So flush but not straight flush; our classification: if flush true, straight false, then we will output flush (since four of a kind and full house false). That matches "it's just a flush."
Now what about a hand like A,2,3,4,5 but not flush? That's a straight.
Now we need to be careful about "full house". Example: QH QD 2S QC 2C => full house. Works.
Now what about "two pairs": example: 2H 2D 3C 3D 5S => two pairs.
Now what about "pair": example: 2H 2D 5C 7D 9S => pair.
Now "three of a kind": example: 2H 2D 2C 5S 7D => three of a kind.
Now "high card": example: 2H 4D 6C 8S TH.
Now check that our frequency checks are correct. For full house, we used freq == [2,3]. But note: If we have a hand with 5 cards, there are only two ranks, one with 3, other with 2. So freq sorted is [2,3]. So good.
But what about a hand with four of a kind? Two ranks: one with 4, one with 1, freq sorted [1,4]. Good.
Now three of a kind: three distinct ranks: frequencies [1,1,3]. Good.
Two pairs: [1,2,2]. Good.
Pair: [1,1,1,2]. Good.
High card: [1,1,1,1,1].
Now all good.
Now we must ensure that after we check for royal flush, we don't accidentally treat a straight flush as royal flush when it's not. So sorted(ranks) == [10,11,12,13,14] is strict.
But note: Could there be a straight flush with ranks 10,J,Q,K,A? That's royal flush, so we want to output "royal flush". Good.
Now confirm ordering: "royal flush" is above "straight flush". We check royal first, then if is_flush and straight true but not royal, we output straight flush.
Now what about a hand that is both flush and straight but also four of a kind? Not possible. So okay.
Now final code.
Now consider if there are any tricky cases like "straight" with Ace low where the hand has an Ace and 2,3,4,5 but also a flush? Already handled.
Now let's test some more:
Test1: Input: "AH 2H 3H 4H 5H" -> suits all H => flush; ranks [14,2,3,4,5] => is_straight? Ace low conversion yields [1,2,3,4,5] straight. So flush and straight true, sorted ranks != [10..14], so straight flush. Output "straight flush". Good.
Test2: "AH 2H 3H 4H 6H" -> flush true, straight? Ranks 14,2,3,4,6: sorted [2,3,4,6,14]; with Ace low [1,2,3,4,6] not consecutive; with Ace high not consecutive => not straight. So not straight flush. Then freq: all distinct? Yes, freq [1,1,1,1,1] but flush true, so output flush (since we check flush before high card). So "flush". Good.
Test3: "AH 2S 3D 4C 5H" -> suits not all same; ranks straight? Yes (Ace low). So straight true, flush false, so output "straight". Good.
Test4: "AH AS AD AC KH" -> four of a kind: freq [1,4] => "four of a kind". flush? All suits? Not all same? Actually three suits? Not all same, but even if all same, four of a kind outranks flush, but we check four of a kind before flush, so correct.
Test5: "AH AS AD KC KS" -> full house: freq [2,3]? Actually A:3, K:2 => freq [2,3] => full house.
Test6: "AH AS AD KS QS" -> three of a kind: freq [1,1,3] => "three of a kind". flush? No.
Test7: "AH AS KS KD QS" -> two pairs: A:2, K:2, Q:1 => freq [1,2,2] => "two pairs".
Test9: "AH KS QD JC 9S" -> high card? Not straight? Actually A,K,Q,J,9 not consecutive. No flush. freq all 1 => high card. But note: A,K,Q,J,10 is straight. With 9, not straight. So high card.
Now test some with duplicates and straight: "2H 3D 4C 5S 5H" -> not straight because duplicate 5. So should be pair? Actually pair of 5s. freq [1,1,1,2] -> pair. Our is_straight will return false because differences not all 1. So pair.
Now test: "2H 3D 4C 5S 6H" -> straight. Output straight.
Now test: "2H 2D 2C 2S 3H" -> four of a kind. Output four of a kind.
Now test: "2H 2D 2C 3H 3D" -> full house. Output full house.
Now all seems fine.
But we must be careful about the order of checking: after royal flush, we check four of a kind and full house. But what about a hand that is both four of a kind and flush? That's impossible because four of a kind uses four cards of same rank, but they could all be same suit? Possibly all four of a kind could be same suit? Actually four of a kind can have same suit? The four cards of same rank could be from different suits; but even if all four are same suit, the fifth card might be a different suit or same suit; it could be a flush if all suits are same. But is that possible? If all five cards are same suit, then it's a flush. But four of a kind outranks flush, so we output four of a kind, which is correct. However, if four of a kind and flush, our check for four of a kind will catch it before flush, so correct.
What about a hand that is both full house and flush? Possibly if all cards same suit, and three of one rank and two of another. That's both full house and flush. Full house outranks flush, so we output full house, correct.
What about a hand that is both straight and three of a kind? Straight requires five distinct ranks, so cannot have three of a kind. So not possible.