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
