import re from decimal import Decimal,InvalidOperation def extract_code(text,preserve_indentation=False): text=text.strip('\r\n') if preserve_indentation else text.strip() # Closing fences must occupy their own line; backticks inside a string are code. blocks=re.findall(r'^[ \t]*```[^\r\n`]*\r?\n(.*?)^[ \t]*```[ \t]*(?=\r?$)',text,re.S|re.M) if len(blocks)==1: body=blocks[0].rstrip('\r\n') if preserve_indentation else blocks[0].strip() return body+'\n' if len(blocks)>1:raise ValueError('Multiple code blocks are ambiguous') if re.search(r'^[ \t]*```',text,re.M):raise ValueError('Unclosed code fence') return text+'\n' def compare_output(actual,expected): # Remove one terminal newline only: spaces in string results are observable. value=actual.removesuffix('\n').removesuffix('\r') if isinstance(expected,int): try:return Decimal(value.strip())==expected and value.strip()!='' except InvalidOperation:return False return value==expected class FunctionRunner: def evaluate(self,row,code,hidden,sandbox): inputs=[t['input'] for t in hidden['tests']] response=sandbox.run({'language':row['language'],'kind':row['metadata']['family'], 'code':code,'inputs':inputs,'timeout_seconds':row['timeout_seconds']}, timeout=50+len(inputs)*(row['timeout_seconds']+2)) results=[] observations=response['observations'] for i,test in enumerate(hidden['tests']): actual=observations[i] if i