liveBook · Manning

Flush

As explained in Regular Expression Puzzles and AI Coding Assistants

A flush is a poker hand whose cards all share one suit without necessarily having consecutive ranks.

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

How Regular Expression Puzzles and AI Coding Assistants covers it

How deep it goes
Implements a regex-based suit check and truthy suit return, while deferring five-card validation to other functions.
Examples
Tests diamond Unicode and ASCII hands, mixed-suit failures, and AI-generated tests that wrongly call a mixed hand a flush.
What it assumes
Assumes sorted five-card representations and relies on separate validation rather than rechecking hand length.
What you can do after
Build it from scratch as an is_flush predicate that returns a suit or False for represented poker hands.
How it connects
Connects flush detection to regular expressions, straight detection, straight flushes, card sorting, and poker-hand comparison.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Regular Expression Puzzles and AI Coding Assistants. The section links point to the parts of the book it draws on.