broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, hg, ag)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Dover', 1, 'Corby', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Dover', 0, 'Ashby', 1],
     ['Ashby', 1, 'Epsom', 1],
     ['Corby', 0, 'Dover', 0],
     ['Epsom', 0, 'Ashby', 0],
     ['Corby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 4, 5, 0, 2],
    ['Dover', 4, 5, 0, 2],
    ['Brent', 1, 3, 1, 1],
    ['Epsom', 2, 2, 0, 1],
    ['Corby', 3, 2, -1, 1]]),
  ('variant scenario 1',
   ([['Corby', 0, 'Brent', 1],
     ['Corby', 0, 'Dover', 1],
     ['Corby', 0, 'Dover', 2],
     ['Brent', 1, 'Dover', 2],
     ['Ashby', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 3, 9, 4, 5], ['Brent', 3, 4, 0, 3], ['Ashby', 1, 1, 0, 1], ['Corby', 3, 0, -4, 0]]),
  ('variant scenario 2',
   ([['Brent', 1, 'Ashby', 1], ['Brent', 0, 'Ashby', 1], ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Brent', 3, 4, 0, 2], ['Corby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Corby', 0, 'Ashby', 1], ['Corby', 1, 'Ashby', 1], ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Corby', 3, 4, 0, 2], ['Brent', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Ashby', 3, 'Brent', 1],
     ['Ashby', 1, 'Corby', 3],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 2, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 4, 7, 2, 8], ['Corby', 4, 7, 1, 7], ['Brent', 4, 2, -3, 4]]),
  ('variant scenario 2',
   ([['Ashby', 0, 'Epsom', 0],
     ['Epsom', 0, 'Ashby', 0],
     ['Epsom', 0, 'Dover', 1],
     ['Ashby', 1, 'Dover', 0],
     ['Corby', 0, 'Brent', 1],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 4, 6, 0, 2],
    ['Ashby', 3, 5, 1, 1],
    ['Epsom', 5, 5, -1, 1],
    ['Brent', 1, 3, 1, 1],
    ['Corby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Ashby', 1, 'Brent', 3],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Ashby', 1],
     ['Brent', 0, 'Corby', 3],
     ['Corby', 0, 'Ashby', 1],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 6, 1, 5], ['Brent', 4, 6, 0, 6], ['Ashby', 4, 6, -1, 3]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Corby', 0],
     ['Ashby', 1, 'Corby', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Ashby', 1, 'Corby', 0],
     ['Corby', 0, 'Ashby', 0],
     ['Ashby', 1, 'Brent', 1],
     ['Brent', 0, 'Corby', 0],
     ['Corby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 6, 12, 3, 5], ['Brent', 3, 5, 1, 2], ['Corby', 7, 3, -4, 1]]),
  ('variant scenario 2',
   ([['Brent', 1, 'Corby', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Corby', 1],
     ['Epsom', 0, 'Ashby', 0],
     ['Corby', 1, 'Dover', 1],
     ['Brent', 1, 'Corby', 0],
     ['Dover', 1, 'Brent', 1],
     ['Epsom', 0, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 7, 2, 3],
    ['Dover', 3, 5, 1, 3],
    ['Epsom', 4, 3, -1, 1],
    ['Corby', 5, 3, -2, 2],
    ['Ashby', 1, 1, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Dover', 1, 'Ashby', 1],
     ['Brent', 0, 'Corby', 1],
     ['Dover', 1, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Ashby', 0, 'Dover', 1],
     ['Brent', 0, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 3, 5, 1, 3], ['Ashby', 4, 5, 0, 3], ['Corby', 2, 4, 1, 2], ['Brent', 3, 1, -2, 1]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Corby', 1],
     ['Brent', 0, 'Ashby', 1],
     ['Corby', 2, 'Ashby', 1],
     ['Ashby', 2, 'Corby', 1],
     ['Corby', 1, 'Brent', 0],
     ['Corby', 1, 'Brent', 2],
     ['Brent', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 5, 10, 2, 7], ['Corby', 7, 10, 0, 8], ['Brent', 4, 3, -2, 2]]),
  ('variant scenario 2',
   ([['Corby', 1, 'Dover', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 3, 'Dover', 3],
     ['Corby', 2, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 4, 5, 1, 6], ['Brent', 1, 3, 1, 1], ['Dover', 2, 2, 0, 4], ['Ashby', 1, 0, -2, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Ashby', 0, 'Corby', 1],
     ['Epsom', 1, 'Dover', 0],
     ['Corby', 1, 'Dover', 1],
     ['Epsom', 1, 'Brent', 1],
     ['Corby', 1, 'Brent', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Dover', 1],
     ['Ashby', 1, 'Corby', 1],
     ['Dover', 1, 'Epsom', 0],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 0, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 5, 8, 1, 4],
    ['Dover', 5, 8, 1, 4],
    ['Epsom', 5, 5, -1, 3],
    ['Ashby', 3, 4, 0, 2],
    ['Brent', 4, 4, -1, 2]]),
  ('variant scenario 1',
   ([['Ashby', 0, 'Brent', 2],
     ['Ashby', 3, 'Corby', 2],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 2, 'Corby', 2],
     ['Corby', 3, 'Ashby', 1],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 7, 2, 8], ['Brent', 4, 5, 1, 4], ['Ashby', 4, 4, -3, 4]]),
  ('variant scenario 2',
   ([['Epsom', 1, 'Dover', 0],
     ['Brent', 1, 'Epsom', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Dover', 0, 'Ashby', 0],
     ['Epsom', 0, 'Corby', 1],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Epsom', 1],
     ['Dover', 0, 'Brent', 0],
     ['Epsom', 0, 'Dover', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Dover', 0, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 7, 2, 2],
    ['Corby', 4, 7, 1, 3],
    ['Epsom', 6, 7, -1, 2],
    ['Dover', 5, 6, 0, 1],
    ['Ashby', 4, 2, -2, 1]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, ag)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Dover', 1, 'Corby', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Dover', 0, 'Ashby', 1],
     ['Ashby', 1, 'Epsom', 1],
     ['Corby', 0, 'Dover', 0],
     ['Epsom', 0, 'Ashby', 0],
     ['Corby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 4, 5, 0, 2],
    ['Dover', 4, 5, 0, 2],
    ['Brent', 1, 3, 1, 1],
    ['Epsom', 2, 2, 0, 1],
    ['Corby', 3, 2, -1, 1]]),
  ('variant scenario 1',
   ([['Corby', 0, 'Brent', 1],
     ['Corby', 0, 'Dover', 1],
     ['Corby', 0, 'Dover', 2],
     ['Brent', 1, 'Dover', 2],
     ['Ashby', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 3, 9, 4, 5], ['Brent', 3, 4, 0, 3], ['Ashby', 1, 1, 0, 1], ['Corby', 3, 0, -4, 0]]),
  ('variant scenario 2',
   ([['Brent', 1, 'Ashby', 1], ['Brent', 0, 'Ashby', 1], ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Brent', 3, 4, 0, 2], ['Corby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Corby', 0, 'Ashby', 1], ['Corby', 1, 'Ashby', 1], ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Corby', 3, 4, 0, 2], ['Brent', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Ashby', 3, 'Brent', 1],
     ['Ashby', 1, 'Corby', 3],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 2, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 4, 7, 2, 8], ['Corby', 4, 7, 1, 7], ['Brent', 4, 2, -3, 4]]),
  ('variant scenario 2',
   ([['Ashby', 0, 'Epsom', 0],
     ['Epsom', 0, 'Ashby', 0],
     ['Epsom', 0, 'Dover', 1],
     ['Ashby', 1, 'Dover', 0],
     ['Corby', 0, 'Brent', 1],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 4, 6, 0, 2],
    ['Ashby', 3, 5, 1, 1],
    ['Epsom', 5, 5, -1, 1],
    ['Brent', 1, 3, 1, 1],
    ['Corby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Ashby', 1, 'Brent', 3],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Ashby', 1],
     ['Brent', 0, 'Corby', 3],
     ['Corby', 0, 'Ashby', 1],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 6, 1, 5], ['Brent', 4, 6, 0, 6], ['Ashby', 4, 6, -1, 3]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Corby', 0],
     ['Ashby', 1, 'Corby', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Ashby', 1, 'Corby', 0],
     ['Corby', 0, 'Ashby', 0],
     ['Ashby', 1, 'Brent', 1],
     ['Brent', 0, 'Corby', 0],
     ['Corby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 6, 12, 3, 5], ['Brent', 3, 5, 1, 2], ['Corby', 7, 3, -4, 1]]),
  ('variant scenario 2',
   ([['Brent', 1, 'Corby', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Corby', 1],
     ['Epsom', 0, 'Ashby', 0],
     ['Corby', 1, 'Dover', 1],
     ['Brent', 1, 'Corby', 0],
     ['Dover', 1, 'Brent', 1],
     ['Epsom', 0, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 7, 2, 3],
    ['Dover', 3, 5, 1, 3],
    ['Epsom', 4, 3, -1, 1],
    ['Corby', 5, 3, -2, 2],
    ['Ashby', 1, 1, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Dover', 1, 'Ashby', 1],
     ['Brent', 0, 'Corby', 1],
     ['Dover', 1, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Ashby', 0, 'Dover', 1],
     ['Brent', 0, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 3, 5, 1, 3], ['Ashby', 4, 5, 0, 3], ['Corby', 2, 4, 1, 2], ['Brent', 3, 1, -2, 1]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Corby', 1],
     ['Brent', 0, 'Ashby', 1],
     ['Corby', 2, 'Ashby', 1],
     ['Ashby', 2, 'Corby', 1],
     ['Corby', 1, 'Brent', 0],
     ['Corby', 1, 'Brent', 2],
     ['Brent', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 5, 10, 2, 7], ['Corby', 7, 10, 0, 8], ['Brent', 4, 3, -2, 2]]),
  ('variant scenario 2',
   ([['Corby', 1, 'Dover', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 3, 'Dover', 3],
     ['Corby', 2, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 4, 5, 1, 6], ['Brent', 1, 3, 1, 1], ['Dover', 2, 2, 0, 4], ['Ashby', 1, 0, -2, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: away goal orientation',
   ([['Ashby', 0, 'Corby', 1],
     ['Epsom', 1, 'Dover', 0],
     ['Corby', 1, 'Dover', 1],
     ['Epsom', 1, 'Brent', 1],
     ['Corby', 1, 'Brent', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Epsom', 1, 'Dover', 1],
     ['Ashby', 1, 'Corby', 1],
     ['Dover', 1, 'Epsom', 0],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 0, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 5, 8, 1, 4],
    ['Dover', 5, 8, 1, 4],
    ['Epsom', 5, 5, -1, 3],
    ['Ashby', 3, 4, 0, 2],
    ['Brent', 4, 4, -1, 2]]),
  ('variant scenario 1',
   ([['Ashby', 0, 'Brent', 2],
     ['Ashby', 3, 'Corby', 2],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 2, 'Corby', 2],
     ['Corby', 3, 'Ashby', 1],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 7, 2, 8], ['Brent', 4, 5, 1, 4], ['Ashby', 4, 4, -3, 4]]),
  ('variant scenario 2',
   ([['Epsom', 1, 'Dover', 0],
     ['Brent', 1, 'Epsom', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Dover', 0, 'Ashby', 0],
     ['Epsom', 0, 'Corby', 1],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Epsom', 1],
     ['Dover', 0, 'Brent', 0],
     ['Epsom', 0, 'Dover', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Dover', 0, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 7, 2, 2],
    ['Corby', 4, 7, 1, 3],
    ['Epsom', 6, 7, -1, 2],
    ['Dover', 5, 6, 0, 1],
    ['Ashby', 4, 2, -2, 1]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Away teams receive the home side goals and the wrong results.

Round-robin football table. results rows are [home, home_goals, away, away_goals]. A win is 3 points, a draw 1. Rank by points, then goal difference, then goals scored, then head-to-head points among the teams still level on all three (only matches between those teams), then team name. Return rows [team, played, points, goal_difference, goals_for].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[2], -key3(t)[1], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Brent', 2, 'Ashby', 1],
     ['Dover', 3, 'Corby', 1],
     ['Dover', 3, 'Epsom', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 2, 6, 5, 6],
    ['Brent', 1, 3, 1, 2],
    ['Ashby', 3, 3, -1, 3],
    ['Corby', 3, 3, -2, 3],
    ['Epsom', 1, 0, -3, 0]]),
  ('regression: criteria order',
   ([['Corby', 0, 'Ashby', 1], ['Ashby', 1, 'Brent', 2], ['Brent', 1, 'Corby', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 2], ['Corby', 2, 3, 0, 2]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Brent', 3],
     ['Ashby', 0, 'Corby', 2],
     ['Brent', 0, 'Ashby', 1],
     ['Brent', 1, 'Corby', 1],
     ['Corby', 2, 'Ashby', 2],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 6, 2, 7], ['Brent', 4, 5, 1, 6], ['Ashby', 4, 4, -3, 4]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Ashby', 2],
     ['Brent', 2, 'Corby', 3],
     ['Corby', 3, 'Ashby', 0],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 9, 3, 8], ['Ashby', 4, 6, 0, 4], ['Brent', 4, 3, -3, 5]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Ashby', 1, 'Epsom', 1],
     ['Dover', 3, 'Epsom', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Ashby', 0, 'Dover', 0],
     ['Dover', 3, 'Corby', 0],
     ['Corby', 0, 'Dover', 3],
     ['Dover', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 6, 14, 9, 11],
    ['Ashby', 3, 2, -1, 1],
    ['Brent', 1, 1, 0, 1],
    ['Epsom', 2, 1, -2, 2],
    ['Corby', 2, 0, -6, 0]]),
  ('regression: criteria order',
   ([['Epsom', 1, 'Brent', 0],
     ['Ashby', 0, 'Corby', 0],
     ['Epsom', 0, 'Brent', 0],
     ['Epsom', 1, 'Dover', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 5, 1, 2],
    ['Dover', 1, 1, 0, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Brent', 2, 1, -1, 0]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Brent', 0],
     ['Ashby', 2, 'Corby', 3],
     ['Corby', 0, 'Ashby', 2],
     ['Ashby', 1, 'Corby', 0],
     ['Dover', 2, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 3, 6, 2, 5],
    ['Corby', 4, 6, -1, 4],
    ['Dover', 1, 3, 2, 2],
    ['Epsom', 0, 0, 0, 0],
    ['Brent', 2, 0, -3, 0]]),
  ('variant scenario 2',
   ([['Brent', 0, 'Corby', 0],
     ['Ashby', 0, 'Dover', 0],
     ['Brent', 0, 'Dover', 0],
     ['Brent', 3, 'Ashby', 0],
     ['Brent', 0, 'Dover', 3],
     ['Ashby', 2, 'Dover', 1],
     ['Brent', 2, 'Ashby', 0],
     ['Ashby', 2, 'Corby', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Brent', 5, 8, 2, 5], ['Dover', 4, 5, 2, 4], ['Ashby', 5, 5, -4, 4], ['Corby', 2, 2, 0, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Dover', 0, 'Ashby', 0],
     ['Ashby', 0, 'Brent', 1],
     ['Epsom', 1, 'Corby', 1],
     ['Corby', 1, 'Brent', 1],
     ['Dover', 0, 'Brent', 0],
     ['Epsom', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 5, 1, 2],
    ['Corby', 2, 2, 0, 2],
    ['Epsom', 2, 2, 0, 2],
    ['Dover', 2, 2, 0, 0],
    ['Ashby', 3, 2, -1, 1]]),
  ('regression: criteria order',
   ([['Ashby', 1, 'Brent', 2],
     ['Ashby', 2, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 1, 'Corby', 2],
     ['Corby', 2, 'Ashby', 2],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 5, 0, 7], ['Brent', 4, 5, 0, 6], ['Ashby', 4, 5, 0, 6]]),
  ('variant scenario 1',
   ([['Brent', 1, 'Corby', 0], ['Ashby', 1, 'Corby', 0], ['Dover', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 2, 4, 1, 1], ['Brent', 1, 3, 1, 1], ['Dover', 1, 1, 0, 0], ['Corby', 2, 0, -2, 0]]),
  ('variant scenario 2',
   ([['Corby', 1, 'Brent', 1], ['Brent', 1, 'Corby', 1], ['Brent', 0, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 5, 1, 3], ['Brent', 3, 2, -1, 2], ['Ashby', 0, 0, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Ashby', 0, 'Brent', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Brent', 0, 'Ashby', 1],
     ['Corby', 0, 'Brent', 1],
     ['Ashby', 0, 'Brent', 1],
     ['Ashby', 1, 'Dover', 0],
     ['Ashby', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Brent', 5, 8, 1, 2], ['Ashby', 6, 8, 0, 3], ['Corby', 4, 5, 0, 2], ['Dover', 1, 0, -1, 0]]),
  ('regression: criteria order',
   ([['Corby', 1, 'Brent', 1], ['Ashby', 0, 'Corby', 0], ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 2, 0, 2],
    ['Ashby', 2, 2, 0, 1],
    ['Corby', 2, 2, 0, 1],
    ['Dover', 0, 0, 0, 0],
    ['Epsom', 0, 0, 0, 0]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Brent', 3],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 2, 'Ashby', 2],
     ['Brent', 0, 'Corby', 2],
     ['Corby', 3, 'Ashby', 2],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 7, 1, 8], ['Corby', 4, 7, 1, 6], ['Ashby', 4, 2, -2, 6]]),
  ('variant scenario 2',
   ([['Dover', 3, 'Ashby', 1],
     ['Brent', 2, 'Dover', 3],
     ['Dover', 3, 'Corby', 0],
     ['Dover', 0, 'Corby', 2],
     ['Ashby', 0, 'Corby', 2],
     ['Ashby', 2, 'Dover', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 5, 10, 4, 11],
    ['Corby', 3, 6, 1, 4],
    ['Ashby', 3, 1, -4, 3],
    ['Brent', 1, 0, -1, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Epsom', 2, 'Dover', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Dover', 0, 'Brent', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 2, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 9, 3, 5],
    ['Epsom', 2, 3, 1, 2],
    ['Dover', 3, 3, -2, 1],
    ['Ashby', 0, 0, 0, 0],
    ['Corby', 2, 0, -2, 2]]),
  ('regression: criteria order',
   ([['Corby', 3, 'Brent', 0],
     ['Brent', 3, 'Corby', 1],
     ['Ashby', 1, 'Dover', 1],
     ['Epsom', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 4, -1, 5],
    ['Corby', 2, 3, 1, 4],
    ['Epsom', 1, 1, 0, 2],
    ['Ashby', 1, 1, 0, 1],
    ['Dover', 1, 1, 0, 1]]),
  ('variant scenario 1',
   ([['Ashby', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 0],
     ['Ashby', 3, 'Dover', 1],
     ['Dover', 0, 'Ashby', 1],
     ['Ashby', 2, 'Brent', 3],
     ['Brent', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 6, 7, 0, 6], ['Corby', 2, 6, 2, 2], ['Brent', 2, 4, 1, 3], ['Dover', 2, 0, -3, 1]]),
  ('variant scenario 2',
   ([['Ashby', 1, 'Brent', 1],
     ['Corby', 0, 'Dover', 2],
     ['Brent', 1, 'Dover', 1],
     ['Brent', 2, 'Corby', 0],
     ['Ashby', 2, 'Corby', 2],
     ['Dover', 0, 'Ashby', 3],
     ['Dover', 0, 'Ashby', 3],
     ['Corby', 0, 'Ashby', 0],
     ['Brent', 0, 'Corby', 3],
     ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 5, 9, 6, 9], ['Corby', 6, 8, 0, 7], ['Brent', 5, 5, -2, 5], ['Dover', 4, 4, -4, 3]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -h2h(t), -key3(t)[2], t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Brent', 2, 'Ashby', 1],
     ['Dover', 3, 'Corby', 1],
     ['Dover', 3, 'Epsom', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 2, 6, 5, 6],
    ['Brent', 1, 3, 1, 2],
    ['Ashby', 3, 3, -1, 3],
    ['Corby', 3, 3, -2, 3],
    ['Epsom', 1, 0, -3, 0]]),
  ('regression: criteria order',
   ([['Corby', 0, 'Ashby', 1], ['Ashby', 1, 'Brent', 2], ['Brent', 1, 'Corby', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 2], ['Corby', 2, 3, 0, 2]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Brent', 3],
     ['Ashby', 0, 'Corby', 2],
     ['Brent', 0, 'Ashby', 1],
     ['Brent', 1, 'Corby', 1],
     ['Corby', 2, 'Ashby', 2],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 6, 2, 7], ['Brent', 4, 5, 1, 6], ['Ashby', 4, 4, -3, 4]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Ashby', 2],
     ['Brent', 2, 'Corby', 3],
     ['Corby', 3, 'Ashby', 0],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 9, 3, 8], ['Ashby', 4, 6, 0, 4], ['Brent', 4, 3, -3, 5]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Ashby', 1, 'Epsom', 1],
     ['Dover', 3, 'Epsom', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Ashby', 0, 'Dover', 0],
     ['Dover', 3, 'Corby', 0],
     ['Corby', 0, 'Dover', 3],
     ['Dover', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 6, 14, 9, 11],
    ['Ashby', 3, 2, -1, 1],
    ['Brent', 1, 1, 0, 1],
    ['Epsom', 2, 1, -2, 2],
    ['Corby', 2, 0, -6, 0]]),
  ('regression: criteria order',
   ([['Epsom', 1, 'Brent', 0],
     ['Ashby', 0, 'Corby', 0],
     ['Epsom', 0, 'Brent', 0],
     ['Epsom', 1, 'Dover', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 5, 1, 2],
    ['Dover', 1, 1, 0, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Brent', 2, 1, -1, 0]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Brent', 0],
     ['Ashby', 2, 'Corby', 3],
     ['Corby', 0, 'Ashby', 2],
     ['Ashby', 1, 'Corby', 0],
     ['Dover', 2, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 3, 6, 2, 5],
    ['Corby', 4, 6, -1, 4],
    ['Dover', 1, 3, 2, 2],
    ['Epsom', 0, 0, 0, 0],
    ['Brent', 2, 0, -3, 0]]),
  ('variant scenario 2',
   ([['Brent', 0, 'Corby', 0],
     ['Ashby', 0, 'Dover', 0],
     ['Brent', 0, 'Dover', 0],
     ['Brent', 3, 'Ashby', 0],
     ['Brent', 0, 'Dover', 3],
     ['Ashby', 2, 'Dover', 1],
     ['Brent', 2, 'Ashby', 0],
     ['Ashby', 2, 'Corby', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Brent', 5, 8, 2, 5], ['Dover', 4, 5, 2, 4], ['Ashby', 5, 5, -4, 4], ['Corby', 2, 2, 0, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Dover', 0, 'Ashby', 0],
     ['Ashby', 0, 'Brent', 1],
     ['Epsom', 1, 'Corby', 1],
     ['Corby', 1, 'Brent', 1],
     ['Dover', 0, 'Brent', 0],
     ['Epsom', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 5, 1, 2],
    ['Corby', 2, 2, 0, 2],
    ['Epsom', 2, 2, 0, 2],
    ['Dover', 2, 2, 0, 0],
    ['Ashby', 3, 2, -1, 1]]),
  ('regression: criteria order',
   ([['Ashby', 1, 'Brent', 2],
     ['Ashby', 2, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 1, 'Corby', 2],
     ['Corby', 2, 'Ashby', 2],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 5, 0, 7], ['Brent', 4, 5, 0, 6], ['Ashby', 4, 5, 0, 6]]),
  ('variant scenario 1',
   ([['Brent', 1, 'Corby', 0], ['Ashby', 1, 'Corby', 0], ['Dover', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 2, 4, 1, 1], ['Brent', 1, 3, 1, 1], ['Dover', 1, 1, 0, 0], ['Corby', 2, 0, -2, 0]]),
  ('variant scenario 2',
   ([['Corby', 1, 'Brent', 1], ['Brent', 1, 'Corby', 1], ['Brent', 0, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 5, 1, 3], ['Brent', 3, 2, -1, 2], ['Ashby', 0, 0, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Ashby', 0, 'Brent', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Brent', 0, 'Ashby', 1],
     ['Corby', 0, 'Brent', 1],
     ['Ashby', 0, 'Brent', 1],
     ['Ashby', 1, 'Dover', 0],
     ['Ashby', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Brent', 5, 8, 1, 2], ['Ashby', 6, 8, 0, 3], ['Corby', 4, 5, 0, 2], ['Dover', 1, 0, -1, 0]]),
  ('regression: criteria order',
   ([['Corby', 1, 'Brent', 1], ['Ashby', 0, 'Corby', 0], ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 2, 0, 2],
    ['Ashby', 2, 2, 0, 1],
    ['Corby', 2, 2, 0, 1],
    ['Dover', 0, 0, 0, 0],
    ['Epsom', 0, 0, 0, 0]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Brent', 3],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 2, 'Ashby', 2],
     ['Brent', 0, 'Corby', 2],
     ['Corby', 3, 'Ashby', 2],
     ['Corby', 1, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 7, 1, 8], ['Corby', 4, 7, 1, 6], ['Ashby', 4, 2, -2, 6]]),
  ('variant scenario 2',
   ([['Dover', 3, 'Ashby', 1],
     ['Brent', 2, 'Dover', 3],
     ['Dover', 3, 'Corby', 0],
     ['Dover', 0, 'Corby', 2],
     ['Ashby', 0, 'Corby', 2],
     ['Ashby', 2, 'Dover', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 5, 10, 4, 11],
    ['Corby', 3, 6, 1, 4],
    ['Ashby', 3, 1, -4, 3],
    ['Brent', 1, 0, -1, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: criteria order',
   ([['Epsom', 2, 'Dover', 0],
     ['Dover', 1, 'Epsom', 0],
     ['Dover', 0, 'Brent', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 2, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 9, 3, 5],
    ['Epsom', 2, 3, 1, 2],
    ['Dover', 3, 3, -2, 1],
    ['Ashby', 0, 0, 0, 0],
    ['Corby', 2, 0, -2, 2]]),
  ('regression: criteria order',
   ([['Corby', 3, 'Brent', 0],
     ['Brent', 3, 'Corby', 1],
     ['Ashby', 1, 'Dover', 1],
     ['Epsom', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 4, -1, 5],
    ['Corby', 2, 3, 1, 4],
    ['Epsom', 1, 1, 0, 2],
    ['Ashby', 1, 1, 0, 1],
    ['Dover', 1, 1, 0, 1]]),
  ('variant scenario 1',
   ([['Ashby', 0, 'Corby', 1],
     ['Corby', 1, 'Ashby', 0],
     ['Ashby', 3, 'Dover', 1],
     ['Dover', 0, 'Ashby', 1],
     ['Ashby', 2, 'Brent', 3],
     ['Brent', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 6, 7, 0, 6], ['Corby', 2, 6, 2, 2], ['Brent', 2, 4, 1, 3], ['Dover', 2, 0, -3, 1]]),
  ('variant scenario 2',
   ([['Ashby', 1, 'Brent', 1],
     ['Corby', 0, 'Dover', 2],
     ['Brent', 1, 'Dover', 1],
     ['Brent', 2, 'Corby', 0],
     ['Ashby', 2, 'Corby', 2],
     ['Dover', 0, 'Ashby', 3],
     ['Dover', 0, 'Ashby', 3],
     ['Corby', 0, 'Ashby', 0],
     ['Brent', 0, 'Corby', 3],
     ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 5, 9, 6, 9], ['Corby', 6, 8, 0, 7], ['Brent', 5, 5, -2, 5], ['Dover', 4, 4, -4, 3]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Two teams level on points are ordered by goals scored when goal difference should decide.

Round-robin football table. results rows are [home, home_goals, away, away_goals]. A win is 3 points, a draw 1. Rank by points, then goal difference, then goals scored, then head-to-head points among the teams still level on all three (only matches between those teams), then team name. Return rows [team, played, points, goal_difference, goals_for].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 1, 'Epsom', 1],
     ['Ashby', 1, 'Dover', 1],
     ['Corby', 0, 'Brent', 0],
     ['Corby', 1, 'Epsom', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 2, 2, 0, 2],
    ['Epsom', 2, 2, 0, 2],
    ['Corby', 2, 2, 0, 1],
    ['Dover', 1, 1, 0, 1],
    ['Brent', 1, 1, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 0, 'Brent', 3],
     ['Ashby', 2, 'Corby', 1],
     ['Brent', 3, 'Ashby', 0],
     ['Brent', 1, 'Corby', 3],
     ['Corby', 3, 'Ashby', 0],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 7, 4, 7], ['Brent', 4, 7, 4, 7], ['Ashby', 4, 3, -8, 2]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 1],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 2, 4, 1, 1], ['Ashby', 3, 3, 0, 1], ['Corby', 3, 2, -1, 1]]),
  ('variant scenario 2',
   ([['Ashby', 0, 'Brent', 3],
     ['Ashby', 3, 'Corby', 3],
     ['Brent', 1, 'Ashby', 2],
     ['Brent', 3, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 3, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 7, 3, 10], ['Ashby', 4, 7, 0, 8], ['Corby', 4, 2, -3, 9]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Brent', 1, 'Corby', 1], ['Corby', 0, 'Dover', 1], ['Epsom', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 1, 3, 1, 1],
    ['Ashby', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Brent', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 0], ['Brent', 1, 'Corby', 0], ['Brent', 0, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 6, 1, 2], ['Brent', 2, 3, 0, 1], ['Ashby', 1, 0, -1, 0]]),
  ('variant scenario 2',
   ([['Ashby', 3, 'Corby', 1], ['Corby', 0, 'Ashby', 0], ['Brent', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 3, 5, 2, 3], ['Brent', 1, 1, 0, 0], ['Corby', 2, 1, -2, 1]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 0, 'Brent', 1],
     ['Ashby', 0, 'Corby', 0],
     ['Ashby', 1, 'Epsom', 0],
     ['Dover', 0, 'Corby', 1],
     ['Brent', 0, 'Corby', 0],
     ['Brent', 0, 'Epsom', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 5, 1, 1],
    ['Brent', 3, 4, 0, 1],
    ['Ashby', 3, 4, 0, 1],
    ['Epsom', 2, 3, 0, 1],
    ['Dover', 1, 0, -1, 0]]),
  ('regression: head to head scope',
   ([['Epsom', 0, 'Brent', 0],
     ['Ashby', 1, 'Corby', 1],
     ['Epsom', 1, 'Dover', 1],
     ['Epsom', 0, 'Brent', 1],
     ['Dover', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 4, 1, 1],
    ['Ashby', 2, 2, 0, 2],
    ['Dover', 2, 2, 0, 2],
    ['Epsom', 3, 2, -1, 1],
    ['Corby', 1, 1, 0, 1]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Brent', 2],
     ['Ashby', 3, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 0, 'Corby', 2],
     ['Corby', 2, 'Ashby', 1],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 9, 1, 5], ['Ashby', 4, 5, 2, 6], ['Brent', 4, 2, -3, 2]]),
  ('variant scenario 2',
   ([['Dover', 3, 'Corby', 1],
     ['Ashby', 3, 'Brent', 1],
     ['Ashby', 3, 'Dover', 0],
     ['Corby', 1, 'Epsom', 0],
     ['Dover', 1, 'Brent', 1],
     ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 3, 7, 5, 7],
    ['Dover', 3, 4, -1, 4],
    ['Corby', 2, 3, -1, 2],
    ['Brent', 3, 2, -2, 3],
    ['Epsom', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Epsom', 1, 'Corby', 1],
     ['Epsom', 1, 'Corby', 0],
     ['Ashby', 0, 'Brent', 1],
     ['Corby', 0, 'Ashby', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Dover', 0, 'Corby', 1],
     ['Dover', 1, 'Brent', 0],
     ['Brent', 1, 'Ashby', 0],
     ['Corby', 1, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 5, 7, 0, 3],
    ['Dover', 3, 6, 1, 2],
    ['Brent', 3, 6, 1, 2],
    ['Epsom', 2, 4, 1, 2],
    ['Ashby', 5, 3, -3, 1]]),
  ('regression: head to head scope',
   ([['Corby', 1, 'Epsom', 1],
     ['Brent', 1, 'Dover', 1],
     ['Dover', 1, 'Corby', 0],
     ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 2, 4, 1, 2],
    ['Brent', 2, 2, 0, 2],
    ['Ashby', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Brent', 1],
     ['Corby', 0, 'Ashby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Brent', 1, 'Dover', 1],
     ['Ashby', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 5, 7, 1, 3], ['Brent', 3, 3, 0, 2], ['Corby', 3, 2, -1, 1], ['Dover', 1, 1, 0, 1]]),
  ('variant scenario 2',
   ([['Epsom', 1, 'Dover', 0],
     ['Epsom', 1, 'Dover', 0],
     ['Corby', 1, 'Dover', 0],
     ['Dover', 0, 'Epsom', 0],
     ['Ashby', 1, 'Dover', 0],
     ['Dover', 1, 'Ashby', 0],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Dover', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 7, 2, 2],
    ['Brent', 2, 6, 2, 2],
    ['Dover', 7, 5, -3, 2],
    ['Ashby', 4, 4, -1, 2],
    ['Corby', 2, 3, 0, 1]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 2, 'Brent', 1],
     ['Ashby', 1, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 1, 'Corby', 0],
     ['Corby', 1, 'Ashby', 0],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 5, 0, 5], ['Corby', 4, 5, 0, 4], ['Ashby', 4, 5, 0, 4]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Brent', 1],
     ['Corby', 0, 'Epsom', 0],
     ['Ashby', 1, 'Brent', 0],
     ['Corby', 1, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 5, 1, 2],
    ['Ashby', 1, 3, 1, 1],
    ['Brent', 2, 1, -1, 1],
    ['Epsom', 2, 1, -1, 0],
    ['Dover', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 3],
     ['Ashby', 3, 'Corby', 1],
     ['Brent', 2, 'Ashby', 1],
     ['Brent', 2, 'Corby', 0],
     ['Corby', 2, 'Ashby', 0],
     ['Corby', 3, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 9, 2, 8], ['Corby', 4, 6, 0, 6], ['Ashby', 4, 3, -2, 6]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u)[0] == key3(t)[0]]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += 3 if f > g else (1 if f == g else 0)
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 1, 'Epsom', 1],
     ['Ashby', 1, 'Dover', 1],
     ['Corby', 0, 'Brent', 0],
     ['Corby', 1, 'Epsom', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 2, 2, 0, 2],
    ['Epsom', 2, 2, 0, 2],
    ['Corby', 2, 2, 0, 1],
    ['Dover', 1, 1, 0, 1],
    ['Brent', 1, 1, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 0, 'Brent', 3],
     ['Ashby', 2, 'Corby', 1],
     ['Brent', 3, 'Ashby', 0],
     ['Brent', 1, 'Corby', 3],
     ['Corby', 3, 'Ashby', 0],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 7, 4, 7], ['Brent', 4, 7, 4, 7], ['Ashby', 4, 3, -8, 2]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 1],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 2, 4, 1, 1], ['Ashby', 3, 3, 0, 1], ['Corby', 3, 2, -1, 1]]),
  ('variant scenario 2',
   ([['Ashby', 0, 'Brent', 3],
     ['Ashby', 3, 'Corby', 3],
     ['Brent', 1, 'Ashby', 2],
     ['Brent', 3, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 3, 'Brent', 3]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 7, 3, 10], ['Ashby', 4, 7, 0, 8], ['Corby', 4, 2, -3, 9]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Brent', 1, 'Corby', 1], ['Corby', 0, 'Dover', 1], ['Epsom', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 1, 3, 1, 1],
    ['Ashby', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Brent', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 0], ['Brent', 1, 'Corby', 0], ['Brent', 0, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 6, 1, 2], ['Brent', 2, 3, 0, 1], ['Ashby', 1, 0, -1, 0]]),
  ('variant scenario 2',
   ([['Ashby', 3, 'Corby', 1], ['Corby', 0, 'Ashby', 0], ['Brent', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 3, 5, 2, 3], ['Brent', 1, 1, 0, 0], ['Corby', 2, 1, -2, 1]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 0, 'Brent', 1],
     ['Ashby', 0, 'Corby', 0],
     ['Ashby', 1, 'Epsom', 0],
     ['Dover', 0, 'Corby', 1],
     ['Brent', 0, 'Corby', 0],
     ['Brent', 0, 'Epsom', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 5, 1, 1],
    ['Brent', 3, 4, 0, 1],
    ['Ashby', 3, 4, 0, 1],
    ['Epsom', 2, 3, 0, 1],
    ['Dover', 1, 0, -1, 0]]),
  ('regression: head to head scope',
   ([['Epsom', 0, 'Brent', 0],
     ['Ashby', 1, 'Corby', 1],
     ['Epsom', 1, 'Dover', 1],
     ['Epsom', 0, 'Brent', 1],
     ['Dover', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 4, 1, 1],
    ['Ashby', 2, 2, 0, 2],
    ['Dover', 2, 2, 0, 2],
    ['Epsom', 3, 2, -1, 1],
    ['Corby', 1, 1, 0, 1]]),
  ('variant scenario 1',
   ([['Ashby', 2, 'Brent', 2],
     ['Ashby', 3, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Brent', 0, 'Corby', 2],
     ['Corby', 2, 'Ashby', 1],
     ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 4, 9, 1, 5], ['Ashby', 4, 5, 2, 6], ['Brent', 4, 2, -3, 2]]),
  ('variant scenario 2',
   ([['Dover', 3, 'Corby', 1],
     ['Ashby', 3, 'Brent', 1],
     ['Ashby', 3, 'Dover', 0],
     ['Corby', 1, 'Epsom', 0],
     ['Dover', 1, 'Brent', 1],
     ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 3, 7, 5, 7],
    ['Dover', 3, 4, -1, 4],
    ['Corby', 2, 3, -1, 2],
    ['Brent', 3, 2, -2, 3],
    ['Epsom', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Epsom', 1, 'Corby', 1],
     ['Epsom', 1, 'Corby', 0],
     ['Ashby', 0, 'Brent', 1],
     ['Corby', 0, 'Ashby', 1],
     ['Dover', 1, 'Ashby', 0],
     ['Dover', 0, 'Corby', 1],
     ['Dover', 1, 'Brent', 0],
     ['Brent', 1, 'Ashby', 0],
     ['Corby', 1, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 5, 7, 0, 3],
    ['Dover', 3, 6, 1, 2],
    ['Brent', 3, 6, 1, 2],
    ['Epsom', 2, 4, 1, 2],
    ['Ashby', 5, 3, -3, 1]]),
  ('regression: head to head scope',
   ([['Corby', 1, 'Epsom', 1],
     ['Brent', 1, 'Dover', 1],
     ['Dover', 1, 'Corby', 0],
     ['Brent', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Dover', 2, 4, 1, 2],
    ['Brent', 2, 2, 0, 2],
    ['Ashby', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('variant scenario 1',
   ([['Ashby', 1, 'Brent', 1],
     ['Corby', 0, 'Ashby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Ashby', 1],
     ['Brent', 1, 'Dover', 1],
     ['Ashby', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Ashby', 5, 7, 1, 3], ['Brent', 3, 3, 0, 2], ['Corby', 3, 2, -1, 1], ['Dover', 1, 1, 0, 1]]),
  ('variant scenario 2',
   ([['Epsom', 1, 'Dover', 0],
     ['Epsom', 1, 'Dover', 0],
     ['Corby', 1, 'Dover', 0],
     ['Dover', 0, 'Epsom', 0],
     ['Ashby', 1, 'Dover', 0],
     ['Dover', 1, 'Ashby', 0],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Dover', 1, 'Ashby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 7, 2, 2],
    ['Brent', 2, 6, 2, 2],
    ['Dover', 7, 5, -3, 2],
    ['Ashby', 4, 4, -1, 2],
    ['Corby', 2, 3, 0, 1]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head scope',
   ([['Ashby', 2, 'Brent', 1],
     ['Ashby', 1, 'Corby', 1],
     ['Brent', 1, 'Ashby', 1],
     ['Brent', 1, 'Corby', 0],
     ['Corby', 1, 'Ashby', 0],
     ['Corby', 2, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 5, 0, 5], ['Corby', 4, 5, 0, 4], ['Ashby', 4, 5, 0, 4]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Brent', 1],
     ['Corby', 0, 'Epsom', 0],
     ['Ashby', 1, 'Brent', 0],
     ['Corby', 1, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 5, 1, 2],
    ['Ashby', 1, 3, 1, 1],
    ['Brent', 2, 1, -1, 1],
    ['Epsom', 2, 1, -1, 0],
    ['Dover', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 3],
     ['Ashby', 3, 'Corby', 1],
     ['Brent', 2, 'Ashby', 1],
     ['Brent', 2, 'Corby', 0],
     ['Corby', 2, 'Ashby', 0],
     ['Corby', 3, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 9, 2, 8], ['Corby', 4, 6, 0, 6], ['Ashby', 4, 3, -2, 6]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Level teams are split by overall form instead of their meetings with each other.

Round-robin football table. results rows are [home, home_goals, away, away_goals]. A win is 3 points, a draw 1. Rank by points, then goal difference, then goals scored, then head-to-head points among the teams still level on all three (only matches between those teams), then team name. Return rows [team, played, points, goal_difference, goals_for].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += f - g
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Epsom', 1, 'Brent', 1],
     ['Corby', 0, 'Epsom', 0],
     ['Corby', 1, 'Brent', 1],
     ['Brent', 1, 'Dover', 1],
     ['Ashby', 0, 'Brent', 1],
     ['Ashby', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 4, 6, 1, 4],
    ['Corby', 2, 2, 0, 1],
    ['Epsom', 2, 2, 0, 1],
    ['Dover', 2, 2, 0, 1],
    ['Ashby', 2, 1, -1, 0]]),
  ('variant scenario 1',
   ([['Brent', 0, 'Epsom', 0], ['Brent', 0, 'Epsom', 0], ['Ashby', 1, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 1, 3, 1, 1],
    ['Brent', 2, 2, 0, 0],
    ['Epsom', 3, 2, -1, 0],
    ['Corby', 0, 0, 0, 0],
    ['Dover', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Epsom', 3, 'Ashby', 1],
     ['Brent', 0, 'Epsom', 1],
     ['Dover', 1, 'Epsom', 2],
     ['Dover', 2, 'Ashby', 1],
     ['Brent', 3, 'Ashby', 0],
     ['Ashby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 9, 4, 6],
    ['Brent', 3, 6, 3, 4],
    ['Dover', 2, 3, 0, 3],
    ['Corby', 0, 0, 0, 0],
    ['Ashby', 4, 0, -7, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Brent', 1, 'Ashby', 1],
     ['Dover', 1, 'Epsom', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 5, 1, 3],
    ['Dover', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Ashby', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('regression: head to head metric',
   ([['Dover', 0, 'Epsom', 0], ['Brent', 0, 'Corby', 0], ['Epsom', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 2, 0, 0],
    ['Brent', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Ashby', 1, 1, 0, 0],
    ['Dover', 1, 1, 0, 0]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 0],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Brent', 1],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 8, 2, 3], ['Corby', 5, 5, -1, 2], ['Ashby', 3, 2, -1, 0]]),
  ('variant scenario 2',
   ([['Brent', 0, 'Corby', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Brent', 1, 'Corby', 1],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 5, 1, 2], ['Brent', 4, 5, 0, 2], ['Ashby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Corby', 0, 'Ashby', 0], ['Epsom', 1, 'Dover', 0], ['Epsom', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 4, 1, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Brent', 1, 1, 0, 0],
    ['Dover', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Brent', 2, 'Ashby', 1],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Dover', 1],
     ['Dover', 0, 'Brent', 2],
     ['Dover', 0, 'Ashby', 0],
     ['Brent', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 4, 8, 3, 5],
    ['Corby', 2, 2, 0, 2],
    ['Ashby', 3, 2, -1, 1],
    ['Dover', 3, 2, -2, 1],
    ['Epsom', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Dover', 1, 'Ashby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Ashby', 1, 'Dover', 1],
     ['Dover', 1, 'Brent', 1],
     ['Ashby', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 4, 4, 0, 3], ['Ashby', 3, 3, 0, 2], ['Brent', 2, 2, 0, 1], ['Corby', 1, 1, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Brent', 1, 'Dover', 0], ['Ashby', 0, 'Epsom', 0], ['Brent', 0, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 4, 1, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Epsom', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Dover', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Epsom', 0, 'Dover', 1],
     ['Epsom', 0, 'Corby', 1],
     ['Epsom', 1, 'Dover', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 7, 2, 2],
    ['Dover', 2, 3, 0, 1],
    ['Epsom', 3, 3, -1, 1],
    ['Brent', 1, 1, 0, 0],
    ['Ashby', 1, 0, -1, 0]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 1],
     ['Ashby', 1, 'Corby', 3],
     ['Brent', 3, 'Ashby', 2],
     ['Brent', 3, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 3, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 6, 0, 9], ['Corby', 4, 6, 0, 9], ['Ashby', 4, 6, 0, 8]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Ashby', 0, 'Corby', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Epsom', 0],
     ['Dover', 0, 'Corby', 0],
     ['Ashby', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 4, 6, 1, 2],
    ['Ashby', 3, 2, -1, 1],
    ['Brent', 1, 1, 0, 0],
    ['Epsom', 1, 1, 0, 0],
    ['Dover', 1, 1, 0, 0]]),
  ('variant scenario 1',
   ([['Brent', 1, 'Ashby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Epsom', 0, 'Brent', 0],
     ['Corby', 3, 'Ashby', 1],
     ['Brent', 0, 'Epsom', 3],
     ['Brent', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 4, 3, 3],
    ['Corby', 2, 4, 2, 3],
    ['Brent', 5, 4, -3, 1],
    ['Dover', 1, 1, 0, 0],
    ['Ashby', 2, 1, -2, 2]]),
  ('variant scenario 2',
   ([['Ashby', 1, 'Brent', 1], ['Corby', 0, 'Ashby', 1], ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Corby', 2, 3, 0, 1], ['Brent', 2, 1, -1, 1]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(results, teams):
    table = {t: {'p': 0, 'pts': 0, 'gd': 0, 'gf': 0} for t in teams}
    for h, hg, a, ag in results:
        for team, f, g in ((h, hg, ag), (a, ag, hg)):
            row = table[team]
            row['p'] += 1
            row['gf'] += f
            row['gd'] += f - g
            row['pts'] += 3 if f > g else (1 if f == g else 0)
    def key3(t):
        r = table[t]
        return (r['pts'], r['gd'], r['gf'])
    def h2h(t):
        group = [u for u in teams if key3(u) == key3(t)]
        pts = 0
        for h, hg, a, ag in results:
            if h in group and a in group and t in (h, a):
                f, g = (hg, ag) if t == h else (ag, hg)
                pts += f
        return pts
    order = sorted(teams, key=lambda t: (-key3(t)[0], -key3(t)[1], -key3(t)[2], -h2h(t), t))
    return [[t, table[t]['p'], table[t]['pts'], table[t]['gd'], table[t]['gf']] for t in order]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
def run(args):
    try:
        return solve(*args)
    except Exception as exc:
        return 'raised ' + type(exc).__name__
cases = [[('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Epsom', 1, 'Brent', 1],
     ['Corby', 0, 'Epsom', 0],
     ['Corby', 1, 'Brent', 1],
     ['Brent', 1, 'Dover', 1],
     ['Ashby', 0, 'Brent', 1],
     ['Ashby', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 4, 6, 1, 4],
    ['Corby', 2, 2, 0, 1],
    ['Epsom', 2, 2, 0, 1],
    ['Dover', 2, 2, 0, 1],
    ['Ashby', 2, 1, -1, 0]]),
  ('variant scenario 1',
   ([['Brent', 0, 'Epsom', 0], ['Brent', 0, 'Epsom', 0], ['Ashby', 1, 'Epsom', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Ashby', 1, 3, 1, 1],
    ['Brent', 2, 2, 0, 0],
    ['Epsom', 3, 2, -1, 0],
    ['Corby', 0, 0, 0, 0],
    ['Dover', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Epsom', 3, 'Ashby', 1],
     ['Brent', 0, 'Epsom', 1],
     ['Dover', 1, 'Epsom', 2],
     ['Dover', 2, 'Ashby', 1],
     ['Brent', 3, 'Ashby', 0],
     ['Ashby', 0, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 3, 9, 4, 6],
    ['Brent', 3, 6, 3, 4],
    ['Dover', 2, 3, 0, 3],
    ['Corby', 0, 0, 0, 0],
    ['Ashby', 4, 0, -7, 2]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Brent', 1, 'Ashby', 1],
     ['Dover', 1, 'Epsom', 1],
     ['Corby', 0, 'Brent', 1],
     ['Corby', 1, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 3, 5, 1, 3],
    ['Dover', 1, 1, 0, 1],
    ['Epsom', 1, 1, 0, 1],
    ['Ashby', 1, 1, 0, 1],
    ['Corby', 2, 1, -1, 1]]),
  ('regression: head to head metric',
   ([['Dover', 0, 'Epsom', 0], ['Brent', 0, 'Corby', 0], ['Epsom', 0, 'Ashby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 2, 0, 0],
    ['Brent', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Ashby', 1, 1, 0, 0],
    ['Dover', 1, 1, 0, 0]]),
  ('variant scenario 1',
   ([['Corby', 1, 'Ashby', 0],
     ['Ashby', 0, 'Corby', 0],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Brent', 1],
     ['Corby', 0, 'Brent', 1],
     ['Brent', 1, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 8, 2, 3], ['Corby', 5, 5, -1, 2], ['Ashby', 3, 2, -1, 0]]),
  ('variant scenario 2',
   ([['Brent', 0, 'Corby', 1],
     ['Brent', 1, 'Ashby', 0],
     ['Brent', 1, 'Corby', 1],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Corby', 3, 5, 1, 2], ['Brent', 4, 5, 0, 2], ['Ashby', 1, 0, -1, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Corby', 0, 'Ashby', 0], ['Epsom', 1, 'Dover', 0], ['Epsom', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 4, 1, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Brent', 1, 1, 0, 0],
    ['Dover', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Brent', 2, 'Ashby', 1],
     ['Brent', 0, 'Ashby', 0],
     ['Corby', 1, 'Dover', 1],
     ['Dover', 0, 'Brent', 2],
     ['Dover', 0, 'Ashby', 0],
     ['Brent', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 4, 8, 3, 5],
    ['Corby', 2, 2, 0, 2],
    ['Ashby', 3, 2, -1, 1],
    ['Dover', 3, 2, -2, 1],
    ['Epsom', 0, 0, 0, 0]]),
  ('variant scenario 2',
   ([['Dover', 1, 'Ashby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Ashby', 1, 'Dover', 1],
     ['Dover', 1, 'Brent', 1],
     ['Ashby', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Dover', 4, 4, 0, 3], ['Ashby', 3, 3, 0, 2], ['Brent', 2, 2, 0, 1], ['Corby', 1, 1, 0, 0]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Brent', 1, 'Dover', 0], ['Ashby', 0, 'Epsom', 0], ['Brent', 0, 'Corby', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Brent', 2, 4, 1, 1],
    ['Ashby', 1, 1, 0, 0],
    ['Epsom', 1, 1, 0, 0],
    ['Corby', 1, 1, 0, 0],
    ['Dover', 1, 0, -1, 0]]),
  ('variant scenario 1',
   ([['Epsom', 0, 'Dover', 1],
     ['Epsom', 0, 'Corby', 1],
     ['Epsom', 1, 'Dover', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Corby', 0, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 3, 7, 2, 2],
    ['Dover', 2, 3, 0, 1],
    ['Epsom', 3, 3, -1, 1],
    ['Brent', 1, 1, 0, 0],
    ['Ashby', 1, 0, -1, 0]]),
  ('variant scenario 2',
   ([['Ashby', 2, 'Brent', 1],
     ['Ashby', 1, 'Corby', 3],
     ['Brent', 3, 'Ashby', 2],
     ['Brent', 3, 'Corby', 2],
     ['Corby', 1, 'Ashby', 3],
     ['Corby', 3, 'Brent', 2]],
    ['Ashby', 'Brent', 'Corby']),
   [['Brent', 4, 6, 0, 9], ['Corby', 4, 6, 0, 9], ['Ashby', 4, 6, 0, 8]])],
 [('control single win',
   ([['Ashby', 2, 'Brent', 0]], ['Ashby', 'Brent']),
   [['Ashby', 1, 3, 2, 2], ['Brent', 1, 0, -2, 0]]),
  ('boundary draw',
   ([['Ashby', 1, 'Brent', 1]], ['Brent', 'Ashby']),
   [['Ashby', 1, 1, 0, 1], ['Brent', 1, 1, 0, 1]]),
  ('boundary goals scored before name',
   ([['Ashby', 1, 'Corby', 1], ['Brent', 2, 'Corby', 2]], ['Ashby', 'Brent', 'Corby']),
   [['Corby', 2, 2, 0, 3], ['Brent', 1, 1, 0, 2], ['Ashby', 1, 1, 0, 1]]),
  ('boundary head to head decides',
   ([['Brent', 2, 'Ashby', 1], ['Ashby', 2, 'Dover', 1], ['Corby', 2, 'Brent', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover']),
   [['Corby', 1, 3, 1, 2], ['Brent', 2, 3, 0, 3], ['Ashby', 2, 3, 0, 3], ['Dover', 1, 0, -1, 1]]),
  ('control no matches', ([], ['Corby', 'Ashby']), [['Ashby', 0, 0, 0, 0], ['Corby', 0, 0, 0, 0]]),
  ('regression: head to head metric',
   ([['Ashby', 0, 'Corby', 0],
     ['Ashby', 0, 'Corby', 1],
     ['Brent', 0, 'Epsom', 0],
     ['Dover', 0, 'Corby', 0],
     ['Ashby', 1, 'Corby', 1]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Corby', 4, 6, 1, 2],
    ['Ashby', 3, 2, -1, 1],
    ['Brent', 1, 1, 0, 0],
    ['Epsom', 1, 1, 0, 0],
    ['Dover', 1, 1, 0, 0]]),
  ('variant scenario 1',
   ([['Brent', 1, 'Ashby', 1],
     ['Corby', 0, 'Brent', 0],
     ['Epsom', 0, 'Brent', 0],
     ['Corby', 3, 'Ashby', 1],
     ['Brent', 0, 'Epsom', 3],
     ['Brent', 0, 'Dover', 0]],
    ['Ashby', 'Brent', 'Corby', 'Dover', 'Epsom']),
   [['Epsom', 2, 4, 3, 3],
    ['Corby', 2, 4, 2, 3],
    ['Brent', 5, 4, -3, 1],
    ['Dover', 1, 1, 0, 0],
    ['Ashby', 2, 1, -2, 2]]),
  ('variant scenario 2',
   ([['Ashby', 1, 'Brent', 1], ['Corby', 0, 'Ashby', 1], ['Corby', 1, 'Brent', 0]],
    ['Ashby', 'Brent', 'Corby']),
   [['Ashby', 2, 4, 1, 2], ['Corby', 2, 3, 0, 1], ['Brent', 2, 1, -1, 1]])]]
for label, args, expected in cases[N - 1]:
    check(label, run(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A team that lost a head-to-head match on points is ranked above its rival by goals.

Round-robin football table. results rows are [home, home_goals, away, away_goals]. A win is 3 points, a draw 1. Rank by points, then goal difference, then goals scored, then head-to-head points among the teams still level on all three (only matches between those teams), then team name. Return rows [team, played, points, goal_difference, goals_for].

split: open-access