#!/usr/bin/env python3 """Check every coefficient and parse the fully expanded polynomial independently. Standard library only. This checker imports neither the producer nor the optimizer nor the upstream measuring implementation. """ from collections import Counter,defaultdict from fractions import Fraction from hashlib import sha256 from itertools import combinations_with_replacement from math import gcd from pathlib import Path import argparse import json import re import subprocess import sys ROOT=Path(__file__).resolve().parent SOURCE_HASH='f0e6fc3f62525d7b251a10fc5f4cf0e6663c3f6f08beeef3cb28092b2e1555a8' TEXT_HASH='45bdea1ea7876bf6ec10b5427f9403cf1f94c144fab35817aa545bcb2cea1975' MEASURER_HASH='7ab1ec5cf5838589abf5d31cc44e00a01912d6c490b737a6ad45a1ec3f195a2d' def parse(raw): text=raw.decode('ascii') assert text and not any(c.isspace() for c in text) pieces=re.findall(r'[+-]?[^+-]+',text) assert ''.join(pieces)==text result={} for j,piece in enumerate(pieces): assert j==0 or piece[0]in'+-' assert j>0 or piece[0]!='+' sign=-1 if piece.startswith('-')else 1 body=piece[1:]if piece[:1]in'+-'else piece factors=body.split('*') coefficient=1 if re.fullmatch('[1-9][0-9]*',factors[0]): coefficient=int(factors.pop(0)) assert coefficient!=1 or not factors,'Unit coefficient must be omitted' mon=[];last=0 for factor in factors: match=re.fullmatch(r'x([1-9][0-9]*)(?:\^([234]))?',factor) assert match,'Invalid ordinary variable/power' i=int(match[1]);power=int(match[2]or'1') assert last