Is the pull request ready to merge?

A maintainer merges a pull request only when the reviews and the checks agree. Decide whether one is ready.

What to build

A program that decides if a pull request can be merged.

Requirements

  • Each reviewer's latest approve or changes event counts; a comment event is ignored. A reviewer with only comments has no decision.
  • Print approvals: A of R, where A is the number of reviewers whose latest decision is approve and R is the number required.
  • Print changes requested: NAMES with the names of reviewers whose latest decision is changes, sorted alphabetically and separated by a comma and a space, or none.
  • Print failing checks: NAMES with the names of checks that are fail in input order, or none, and pending checks: NAMES the same way for pending checks.
  • Print verdict: ready to merge if A is at least R and there are no requested changes and no failing and no pending checks, otherwise verdict: not ready.

Input

Line 1: R. Line 2: m, the number of review events. Then m lines REVIEWER STATE (state is approve, changes or comment). Then a line with c, the number of checks, and c lines NAME STATE (state is pass, fail or pending).

Output

Four lines of facts and the verdict.

Constraints

0 ≤ R ≤ 5, 0 ≤ m ≤ 50, 0 ≤ c ≤ 20. Names have no spaces.

Example

Input:

2
3
ana approve
bo approve
cy comment
2
tests pass
lint pass

Output:

approvals: 2 of 2
changes requested: none
failing checks: none
pending checks: none
verdict: ready to merge

Why it works

Storing only the latest decision per reviewer handles a reviewer who first asks for changes and later approves; the verdict then combines the three conditions, so any one of them can block the merge.

Examples

Example 1 · Input
2
3
ana approve
bo approve
cy comment
2
tests pass
lint pass
Expected output
approvals: 2 of 2
changes requested: none
failing checks: none
pending checks: none
verdict: ready to merge
Example 2 · Input
1
3
ana changes
bo comment
ana approve
1
tests fail
Expected output
approvals: 1 of 1
changes requested: none
failing checks: tests
pending checks: none
verdict: not ready

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

Hints

Hint 1

Go through the review events in order and keep the latest approve or changes decision of each reviewer in a dictionary.

Hint 2

A comment event does not change anything.

Hint 3

Print names of reviewers sorted, but checks in input order.

Solution

Show one correct solution (try it yourself first)
r = int(input())
m = int(input())
latest = {}
for _ in range(m):
    who, state = input().split()
    if state in ("approve", "changes"):
        latest[who] = state
c = int(input())
failing = []
pending = []
for _ in range(c):
    name, state = input().split()
    if state == "fail":
        failing.append(name)
    elif state == "pending":
        pending.append(name)
approvals = sum(1 for s in latest.values() if s == "approve")
changes = sorted(who for who, s in latest.items() if s == "changes")


def names(items):
    return ", ".join(items) if items else "none"


print(f"approvals: {approvals} of {r}")
print("changes requested:", names(changes))
print("failing checks:", names(failing))
print("pending checks:", names(pending))
ready = approvals >= r and not changes and not failing and not pending
print("verdict: ready to merge" if ready else "verdict: not ready")

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

Lesson: Pull requests →