← back

Boxed Answer Extraction for Math Benchmarks

#318 · LLM · Medium

⊣ Solve on deep-ml.com

Problem

Extract a boxed answer from a math benchmark response. Math competition and benchmark answers are often formatted as \boxed{answer}. Parse and extract the content inside the last \boxed{} command from a LaTeX-formatted string.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def extract_boxed_answer(text: str) -> str:
    idx = text.rfind("\\boxed{")
    if idx == -1:
        return ""

    start = idx + len("\\boxed{")
    depth = 1
    i = start
    while i < len(text) and depth > 0:
        if text[i] == '{':
            depth += 1
        elif text[i] == '}':
            depth -= 1
        i += 1

    if depth != 0:
        return ""

    return text[start:i - 1].strip()

Explanation

  1. Search for the last occurrence of \boxed{ in the text using rfind, since the final boxed expression typically holds the answer.
  2. Track brace depth starting after the opening brace. Increment on { and decrement on }.
  3. When depth returns to 0, the matching closing brace has been found.
  4. Return the content between the opening and closing braces, stripped of whitespace.

Complexity

  • Time: O(n) where n is the length of the text
  • Space: O(1) aside from the returned substring