Extract Answer String from \\boxed expression.
(string)
| 344 | |
| 345 | |
| 346 | def extract_answer(string): |
| 347 | """Extract Answer String from \\boxed expression.""" |
| 348 | idx = string.rfind("\\boxed") |
| 349 | if idx < 0: |
| 350 | idx = string.rfind("\\fbox") |
| 351 | if idx < 0: |
| 352 | return None |
| 353 | |
| 354 | i = idx |
| 355 | right_brace_idx = None |
| 356 | num_left_braces_open = 0 |
| 357 | while i < len(string): |
| 358 | if string[i] == "{": |
| 359 | num_left_braces_open += 1 |
| 360 | if string[i] == "}": |
| 361 | num_left_braces_open -= 1 |
| 362 | if num_left_braces_open == 0: |
| 363 | right_brace_idx = i |
| 364 | break |
| 365 | i += 1 |
| 366 | |
| 367 | if right_brace_idx is None: |
| 368 | retval = None |
| 369 | else: |
| 370 | retval = string[idx : right_brace_idx + 1] |
| 371 | |
| 372 | if retval: |
| 373 | left = "\\boxed{" |
| 374 | try: |
| 375 | assert retval[: len(left)] == left |
| 376 | assert retval[-1] == "}" |
| 377 | return retval[len(left) : -1] |
| 378 | except AssertionError: |
| 379 | return None |
| 380 | |
| 381 | return None |
| 382 | |
| 383 | |
| 384 | class TimeoutException(Exception): |
nothing calls this directly
no outgoing calls
no test coverage detected