Extract the Final Answer from Chain-of-Thought

easy
Unit 5 · Evaluation & Embeddings0/3 to milestone

Math benchmarks like GSM8K grade only the final answer buried at the end of a long reasoning trace. Extract it robustly.

Implement extract_answer(text) returning the answer as a string, or None:

  1. If there's a \boxed{...}, use the contents of the last one.
  2. Otherwise, if there's a line containing "answer is" or "Answer:" (case-insensitive), use what follows on that line, taking the last such line.
  3. Otherwise, use the last number in the text.

For the chosen text, if it contains a number, return just that number normalised: remove commas, $ and a trailing ., and turn "42.0" into "42". Otherwise return it trimmed. Then implement is_correct(text, gold): compare normalised answers, and numbers numerically within 1e-6.

#evaluation#reasoning#parsing2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Extract the Final Answer from Chain-of-Thought · TechGradify