liveBook · Manning

Hand

As explained in Regular Expression Puzzles and AI Coding Assistants

A hand of cards is a collection of playing cards held or considered together in a card game.

A poker hand here is five cards represented as space-separated two-symbol strings: rank plus suit. Tens use T, so T♠ stands in for 10♠. Before comparing hands, put the cards in descending order: aces high, then K through 2; equal ranks break ties by suits in the order spades, hearts, diamonds, clubs. section 4.10

That sounds like ordinary sorting until two cards share a rank. A rank-only sort can arrange A♥ 4♠ 4♥ K♦ 3♠ as A♥ K♦ 4♥ 4♠ 3♠, which gets the pair backwards under the stated suit order. The hand is not merely a bag of cards; its requested display order includes an explicit tie-breaker. section 4.10 section 4.12

Useful operations on a hand include:

  • Prettifying suit letters S, H, D, and C into Unicode suit symbols. section 4.10
  • Sorting cards by descending rank, with ace high. section 4.10
  • Breaking equal-rank ties by the specified suit sequence: spades, hearts, diamonds, clubs. section 4.10 section 4.12
  • Using the resulting ordered hand as support for later hand-comparison questions, while postponing comparisons within the same hand category. section 4.10

A pair that exposes the tie-breaker

Take A♥ 4♠ 4♥ K♦ 3♠. Rank ordering yields ace, king, fours, three. The two fours still need suit ordering, so 4♠ must precede 4♥. A candidate that sorts only on rank looks plausible but leaves their relative order to an unrelated detail; a candidate that maps suits to numbers must also ensure those numbers and reverse sorting agree with the requested precedence. section 4.12

Poker terminology supplies familiar categories—straight flush, four of a kind, full house, flush, straight, three of a kind, two pairs, one pair, and high card—but the immediate problem is narrower: normalize a hand into a dependable card order. Do not confuse a correct-looking output with a complete ordering rule. section 4.10 section 4.12

# Given a text that represents cards in a standard card deck.
# For example, a poker hand my be represented as "A♥ 4♠ 4♥ K♦ 3♠".
# The 10 of a suit is represented as "T", such as "T♠".
# This function sorts a hand in descending order by card rank.
# Please rank the suits as spades, hearts, diamonds, clubs.     #1
# Please rank the cards as                                      #2
#     A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2.
# For example, the sorted hand                                  #2
#    "A♥ 4♠ 4♥ K♦ 3♠" would be "A♥ K♦ 4♠ 4♥ 3♠".
def sort_hand(hand):
    # Your code here
    return hand

Inspect the compact specification and test case before writing the sort: it makes both rank order and suit tie-breaking visible. section 4.12

How Regular Expression Puzzles and AI Coding Assistants covers it

How deep it goes
Works through hand encoding and sort requirements but defers tie rules for comparing hands of the same category.
Examples
Uses ranked five-card hands and A♥ 4♠ 4♥ K♦ 3♠ to test rank and suit ordering.
What it assumes
Assumes comfort with Python for the sorting function while using regular expressions where practical.
What you can do after
Build it from scratch as functions that prettify and sort a five-card hand under stated rank and suit rules.
How it connects
Connects poker hand rankings with regular expressions, Python sorting, Unicode suit symbols, and tie-breaking.

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.