"""Repeats the demonstrations at https://www.pops4.com/tansaku and checks each answer itself. Standard library only. Sends only the public example data. python check.py""" import json, itertools, urllib.request U = 'https://www.pops4.com/tansaku/examples.json?format=json' H = {'user-agent': 'tansaku-check-bot/1', 'content-type': 'application/json', 'accept': 'application/json'} def get(url, body=None): q = urllib.request.Request(url, None if body is None else json.dumps(body).encode(), H) with urllib.request.urlopen(q, timeout=60) as r: return json.load(r) def restore(d, x): got = {w['row']: w.get('value') for w in d['worked_out']} return got == {a['row']: a['value'] for a in x['answer']} def verify(d, x): a = x['answer'] return any(z.get('located') and z['row'] == a['row'] and z['the_laws_ask_for'] == a['the_laws_ask_for'] for z in d['look_at']) def discover(d, x): # one of D = B x C, B = D / C, C = D / B, exponents within 0.02 of 1 in size if not d['measured_relations']: return False y, rest = d['measured_relations'][0]['says'].split(' = ') e = {t.split('^')[0]: float(t.split('^')[1]) for t in rest.split(' x ')[1:]} want = {'D': {'B': 1, 'C': 1}, 'B': {'D': 1, 'C': -1}, 'C': {'D': 1, 'B': -1}}.get(y, {}) return set(e) == set(want) and all(abs(e[k] - want[k]) < .02 for k in e) def protect(d, x): L, I = x['body']['links'], x['body']['items'] def down(n, dead, seen=()): # a part is down if it fails, or if something it needs is down if n in dead: return True return n not in seen and needs(L.get(n, []), dead, seen + (n,)) def needs(rs, dead, seen=()): return any(all(down(a, dead, seen) for a in r) if isinstance(r, list) else down(r, dead, seen) for r in rs) stops = lambda dead: all(needs(i['rests_on'], dead) for i in I) parts, todo = set(), [a for i in I for r in i['rests_on'] for a in (r if isinstance(r, list) else [r])] while todo: p = todo.pop() if p not in parts: parts.add(p); todo += [a for r in L.get(p, []) for a in (r if isinstance(r, list) else [r])] return len(d['which']) == d['really'] and stops(set(d['which'])) and not any(stops(set(c)) for k in range(d['really']) for c in itertools.combinations(sorted(parts), k)) CHECK = { 'restore': restore, 'verify': verify, 'discover': discover, 'protect': protect, 'discover-control': lambda d, x: d['measured_relations'] == [] and d['exact_relations'] == [], 'rule': lambda d, x: [str(v) for v in d.get('next') or []] == x['answer'], 'rule-control': lambda d, x: 'status' in d and not d.get('next'), } if __name__ == '__main__': X = get(U)['examples'] ok = 0 for x in X: try: r = bool(CHECK[x['id']](get(x['post'], x['body']) if 'post' in x else get(x['get']), x)) ok += r print('agrees ' if r else 'DIFFERS ', x['task']) except Exception as e: print('no answer', x['task'], '|', e, '(429 means busy: wait a minute)') print(ok, 'of', len(X), 'agree')