To keep one maintainer from being overloaded, pull requests are handed to whoever has the least to review. Write the assignment rule.
What to build
A program that assigns pull requests to maintainers by their load.
Requirements
- The first line lists the maintainers separated by commas. A name ending with
:awayis away and gets no reviews; its name is the part before:away. - Each pull request line is
ID SIZEwith SIZEsmall,mediumorlarge, of weight 1, 2 and 3. Everybody starts with load 0. - Go through the pull requests in order and give each one to the available maintainer with the lowest load so far; ties go to the name that comes first alphabetically. Add the weight to that maintainer's load. Print
#ID -> NAME. - If nobody is available, print
#ID -> nobody. - Finally print
load:followed byNAME=LOADfor each available maintainer in alphabetical order, separated by a space (printload:with nothing after it if nobody is available).
Input
The first line lists the maintainers. The second line has n (1 to 50). Each of the next n lines is ID SIZE.
Output
One line per pull request, then the load line.
Constraints
1 ≤ n ≤ 50. Names have no spaces or commas.
Example
Input:
ana,bo,cy
4
11 large
12 small
13 medium
14 small
Output:
#11 -> ana
#12 -> bo
#13 -> cy
#14 -> bo
load: ana=3 bo=2 cy=2
Why it works
Each assignment depends on the loads left by the earlier ones, so the program keeps a running load per person and always takes the minimum with a fixed tie-break, which makes the result repeatable.
Examples
ana,bo,cy 4 11 large 12 small 13 medium 14 small
#11 -> ana #12 -> bo #13 -> cy #14 -> bo load: ana=3 bo=2 cy=2
ana,bo:away 3 1 small 2 small 3 small
#1 -> ana #2 -> ana #3 -> ana load: ana=3
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Make a dictionary load = {name: 0} for the maintainers who are not away.
Hint 2
For each pull request pick min(load, key=lambda name: (load[name], name)) and add its weight.
Hint 3
Print the loads of all available maintainers in alphabetical order at the end.
Solution
Show one correct solution (try it yourself first)
WEIGHT = {"small": 1, "medium": 2, "large": 3}
people = [p.strip() for p in input().split(",")]
load = {}
for person in people:
if not person.endswith(":away"):
load[person] = 0
n = int(input())
for _ in range(n):
pr, size = input().split()
if not load:
print(f"#{pr} -> nobody")
continue
chosen = min(load, key=lambda name: (load[name], name))
load[chosen] += WEIGHT[size]
print(f"#{pr} -> {chosen}")
print("load:" + "".join(f" {name}={load[name]}" for name in sorted(load)))
▶ Open this solution in the compiler, then press Visualize to watch it run step by step