Skip to content

S1Q2 · Card to Value Tuple

⚡ Quick Reference

Function: card_to_value_tuple(card: str) -> tuple

Core idea: split the card string into rank and suit, look both up in dicts, return (suit_value, rank_value).

def card_to_value_tuple(card: str) -> tuple:
    suit_values = {'S': 1, 'H': 2, 'D': 3, 'C': 4}
    rank_values = {'A': 1, 'J': 11, 'Q': 12, 'K': 13}

    suit = card[-1]                        # always last character
    rank = card[:-1]                       # everything before the suit
    rank_val = rank_values.get(rank, int(rank))
    return (suit_values[suit], rank_val)

Key rules: - Suit is always the last character: card[-1] - Rank is everything before the suit: card[:-1] - Numeric ranks (210) convert directly with int() - Face cards and Ace use a lookup dict; dict.get(rank, int(rank)) handles both cases


Problem Statement

Problem

Write a function card_to_value_tuple(card) that takes a card string like "AH" or "10D" and returns a tuple (suit_value, rank_value).

Suit values: S=1, H=2, D=3, C=4

Rank values: A=1, 2–10=face value, J=11, Q=12, K=13

Examples:

Input
'AH'
Output
(2, 1)
Input
'10D'
Output
(3, 10)
Input
'QS'
Output
(1, 12)

Understanding the parsing

The card format is {rank}{suit} where suit is always exactly one character at the end. This means:

'AH'  → suit = 'H',  rank = 'A'
'7D'  → suit = 'D',  rank = '7'
'10D' → suit = 'D',  rank = '10'   ← rank can be 2 chars
'QS'  → suit = 'S',  rank = 'Q'

card[-1] always gives the suit. card[:-1] gives everything before -which is the rank (1 or 2 characters).

dict.get(rank, int(rank)) -handles both cases

Face cards and Ace are in the dict. Numeric ranks are not. rank_values.get(rank, int(rank)) returns the dict value if the key exists, otherwise falls back to int(rank) -converting "7" or "10" to 7 or 10. One expression handles all ranks.


Tracing all examples

Card card[-1] (suit) card[:-1] (rank) suit_val rank_val Result
'AH' 'H' 'A' 2 1 (2, 1)
'7D' 'D' '7' 3 7 (3, 7)
'QS' 'S' 'Q' 1 12 (1, 12)
'9C' 'C' '9' 4 9 (4, 9)
'10D' 'D' '10' 3 10 (3, 10)

Solution approaches

def card_to_value_tuple(card: str) -> tuple:
    suit_values = {'S': 1, 'H': 2, 'D': 3, 'C': 4}
    rank_values = {'A': 1, 'J': 11, 'Q': 12, 'K': 13}
    suit = card[-1]
    rank = card[:-1]
    return (suit_values[suit], rank_values.get(rank, int(rank)))
def card_to_value_tuple(card: str) -> tuple:
    suit_values = {'S': 1, 'H': 2, 'D': 3, 'C': 4}
    rank_values = {'A': 1, 'J': 11, 'Q': 12, 'K': 13}

    suit = card[-1]
    rank = card[:-1]

    if rank in rank_values:
        rank_val = rank_values[rank]
    else:
        rank_val = int(rank)

    return (suit_values[suit], rank_val)
def card_to_value_tuple(card: str) -> tuple:
    suit_values = {'S': 1, 'H': 2, 'D': 3, 'C': 4}
    rank_values = {
        'A': 1, '2': 2, '3': 3, '4': 4, '5': 5,
        '6': 6, '7': 7, '8': 8, '9': 9, '10': 10,
        'J': 11, 'Q': 12, 'K': 13
    }
    suit = card[-1]
    rank = card[:-1]
    return (suit_values[suit], rank_values[rank])

Include all ranks in the dict -no int() conversion needed. More explicit, slightly more typing.

def card_to_value_tuple(card: str) -> tuple:
    suit_values = {'S': 1, 'H': 2, 'D': 3, 'C': 4}
    rank_values = {'A': 1, 'J': 11, 'Q': 12, 'K': 13}
    get_rank = lambda r: rank_values.get(r, int(r))
    return (suit_values[card[-1]], get_rank(card[:-1]))

The lambda encapsulates the rank-lookup logic cleanly, making the return statement read like a direct mapping.


Key takeaways

01

card[-1] and card[:-1] for fixed-suffix parsing

When the last character is always the delimiter (here, the suit), card[-1] and card[:-1] split it cleanly -no need to know the length upfront. Works for both "AH" and "10D".

02

dict.get(key, default) for mixed lookups

rank_values.get(rank, int(rank)) handles both named ranks (via dict) and numeric ranks (via fallback conversion) in one expression -no branching needed.

03

Return order matters -(suit, rank) not (rank, suit)

The problem specifies (suit_value, rank_value). Always double-check the tuple order against the examples -swapping them is an easy mistake that passes visual inspection.