Issues waiting for a reply

Maintainers lose contributors by answering too slowly. Find the issues where the last word came from a contributor.

What to build

A program that finds the issues that are waiting for a maintainer's reply.

Requirements

  • Each line is ISSUE ROLE, where ISSUE is a number and ROLE is maintainer or contributor. The lines are in time order, and one issue may appear many times.
  • For each issue, in the order of its first appearance, look at the role of its last comment. Print #ISSUE: needs reply if it is contributor and #ISSUE: answered if it is maintainer.
  • Finally print waiting: K, where K is the number of issues that need a reply.

Input

The first line has n (1 to 100). Each of the next n lines is ISSUE ROLE.

Output

One line per issue, then the waiting count.

Constraints

1 ≤ n ≤ 100.

Example

Input:

5
12 contributor
15 contributor
12 maintainer
20 maintainer
15 contributor

Output:

#12: answered
#15: needs reply
#20: answered
waiting: 1

Why it works

Only the latest comment of each issue matters, so a dictionary that is overwritten on every comment ends up holding exactly that, and keeping a separate list of first appearances fixes the output order.

Examples

Example 1 · Input
5
12 contributor
15 contributor
12 maintainer
20 maintainer
15 contributor
Expected output
#12: answered
#15: needs reply
#20: answered
waiting: 1
Example 2 · Input
2
7 maintainer
7 contributor
Expected output
#7: needs reply
waiting: 1

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

Hints

Hint 1

Keep a dictionary from issue to the role of the latest comment, and a list of issues in order of first appearance.

Hint 2

Update the role every time you see the issue again.

Hint 3

An issue is waiting if its last comment is by a contributor.

Solution

Show one correct solution (try it yourself first)
n = int(input())
last = {}
order = []
for _ in range(n):
    issue, role = input().split()
    if issue not in last:
        order.append(issue)
    last[issue] = role
waiting = 0
for issue in order:
    if last[issue] == "contributor":
        waiting += 1
        print(f"#{issue}: needs reply")
    else:
        print(f"#{issue}: answered")
print(f"waiting: {waiting}")

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

Lesson: Communication and the code of conduct →