Find the flaky tests

A test that sometimes fails and sometimes passes on the same code is flaky. Sort the tests of a CI history into groups.

What to build

A program that classifies tests by their results over several runs.

Requirements

  • Each line is NAME RESULTS, where RESULTS is a string of P and F, one letter per run, oldest first. All tests have the same number of runs R.
  • Check in this order. All P: print NAME: ok. All F: NAME: broken. Only the last letter is F and the rest P: NAME: regression. Anything else: NAME: flaky (F of R failed) where F is the number of F letters.
  • Finally print ok: A, flaky: B, regression: C, broken: D with the number of tests in each group.

Input

The first line has n (1 to 50). Each of the next n lines is NAME RESULTS.

Output

One line per test, then the summary line.

Constraints

1 ≤ n ≤ 50, 1 ≤ R ≤ 20.

Example

Input:

4
test_add PPPPP
test_net PFPPF
test_parse PPPPF
test_old FFFFF

Output:

test_add: ok
test_net: flaky (2 of 5 failed)
test_parse: regression
test_old: broken
ok: 1, flaky: 1, regression: 1, broken: 1

Why it works

The order of the checks matters: a test with only the last run failed looks flaky at first sight, but it is far more likely a real new break, so it is named before the general mixed case.

Examples

Example 1 · Input
4
test_add PPPPP
test_net PFPPF
test_parse PPPPF
test_old FFFFF
Expected output
test_add: ok
test_net: flaky (2 of 5 failed)
test_parse: regression
test_old: broken
ok: 1, flaky: 1, regression: 1, broken: 1
Example 2 · Input
2
a PPP
b FPP
Expected output
a: ok
b: flaky (1 of 3 failed)
ok: 1, flaky: 1, regression: 0, broken: 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Each line has a name and a string like PPFP with one letter per run.

Hint 2

Check in this order: all P, all F, only the last run F, otherwise flaky.

Hint 3

Count the failures with results.count("F").

Solution

Show one correct solution (try it yourself first)
n = int(input())
groups = {"ok": 0, "flaky": 0, "regression": 0, "broken": 0}
for _ in range(n):
    name, results = input().split()
    runs = len(results)
    failures = results.count("F")
    if failures == 0:
        kind, text = "ok", "ok"
    elif failures == runs:
        kind, text = "broken", "broken"
    elif failures == 1 and results[-1] == "F":
        kind, text = "regression", "regression"
    else:
        kind, text = "flaky", f"flaky ({failures} of {runs} failed)"
    groups[kind] += 1
    print(f"{name}: {text}")
print(f"ok: {groups['ok']}, flaky: {groups['flaky']}, regression: {groups['regression']}, broken: {groups['broken']}")

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Tests and continuous integration →