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
approveorchangesevent counts; acommentevent 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 isapproveand R is the number required. - Print
changes requested: NAMESwith the names of reviewers whose latest decision ischanges, sorted alphabetically and separated by a comma and a space, ornone. - Print
failing checks: NAMESwith the names of checks that arefailin input order, ornone, andpending checks: NAMESthe same way forpendingchecks. - Print
verdict: ready to mergeif A is at least R and there are no requested changes and no failing and no pending checks, otherwiseverdict: 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
2 3 ana approve bo approve cy comment 2 tests pass lint pass
approvals: 2 of 2 changes requested: none failing checks: none pending checks: none verdict: ready to merge
1 3 ana changes bo comment ana approve 1 tests fail
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