Can a hand be a flush without looking anything like a straight? Absolutely: J♦ 9♦ 6♦ 5♦ 2♦ qualifies because its five cards share diamonds, even though the ranks skip around. A flush asks one narrow question—do all cards have the same suit?—while a straight asks about rank sequence instead. section 4.13 section 4.10
With two-character cards and spaces between them, a regular expression can capture the suit on the first card and require that same captured suit four more times. The resulting is_flush(hand) returns the suit when it succeeds and False otherwise. Returning '♦' rather than merely True preserves useful information for later hand-ranking logic while remaining truthy. section 4.14
>>> def is_flush(hand):
... match = re.search(r'^.(.)(.*\1){4}$', hand)
... return match.group(1) if match else False
>>> is_flush('J♣ T♣ 9♣ 8♣ 7♣')
'♣'
>>> is_flush('J♦ 9♦ 6♦ 5♦ 2♦')
'♦'
>>> is_flush('J♦ 9♥ 6♦ 5♦ 2♦')
False
>>> is_flush('JD 9H 6D 5D 2D')
False
>>> is_flush('JD 9D 6D 5D 2D')
'D'
Inspect the capture-and-backreference pattern against matching Unicode suits, mixed suits, ASCII suit letters, and the returned suit value. section 4.14
A reliable flush check depends on the representation and its boundaries:
- It accepts both Unicode suit symbols and ASCII suit letters, so J♦ 9♦ 6♦ 5♦ 2♦ and JD 9D 6D 5D 2D can both identify diamonds. section 4.14
- It relies on other validation to establish that the hand contains five cards; the flush pattern itself is used within that assumption. section 4.14
- It composes cleanly with is_straight(hand): a hand is a straight flush only when both checks succeed, and their returned information can be combined. section 4.13 section 4.14
A test that looks plausible but is wrong
A♠ K♠ 4♠ 4♥ 3♠ is not a flush: the 4♥ breaks the shared-suit condition. An AI-generated function correctly compares suits but labels this mixed-suit hand as expected True. A second regex-oriented version repeats the same faulty expectation. Code that runs and an explanation that sounds reasonable do not rescue contradictory tests; inspect the cards yourself. section 4.15
Flush detection is a small brick, not a complete poker judge. Poker-hand comparison also needs sorted cards, other hand categories, and tie-breaking rules within a category. Keeping the predicate focused makes it easier to combine, test, and distrust appropriately when a generated solution claims success. section 4.10 section 4.14
