Three strikes moderation

Many projects enforce their code of conduct in steps: a warning, then a mute, then a ban. Simulate it.

What to build

A program that applies a three-strike policy to a stream of messages.

Requirements

  • The first line lists the banned words, separated by commas. A message breaks the rules if its lower-case text contains any of them.
  • Each message line is USER|TEXT. If the user is already banned, print N: USER ignored (a banned user's messages are not checked).
  • Otherwise, if the message breaks no rule print N: USER ok. If it breaks the rules, it is a strike: the 1st strike of that user prints N: USER warning, the 2nd prints N: USER muted and the 3rd prints N: USER banned; the user is banned from then on.
  • Number the messages from 1. Finally print banned: NAMES with the banned users in alphabetical order separated by a comma and a space, or banned: none.

Input

The first line has the banned words. The second line has n (1 to 100). Each of the next n lines is USER|TEXT.

Output

One line per message, then the banned line.

Constraints

1 ≤ n ≤ 100. User names have no spaces and no |.

Example

Input:

idiot,garbage
5
ana|Thanks for the report
bo|This code is garbage
bo|You are an Idiot
ana|Could you add a test?
bo|GARBAGE again

Output:

1: ana ok
2: bo warning
3: bo muted
4: ana ok
5: bo banned
banned: bo

Why it works

Strikes are state that grows over the stream, so a dictionary of counts and a set of banned users carry it from message to message, and testing the ban first makes later messages of a banned user have no effect.

Examples

Example 1 · Input
idiot,garbage
5
ana|Thanks for the report
bo|This code is garbage
bo|You are an Idiot
ana|Could you add a test?
bo|GARBAGE again
Expected output
1: ana ok
2: bo warning
3: bo muted
4: ana ok
5: bo banned
banned: bo
Example 2 · Input
stupid
2
cy|Nice work
dee|Good catch
Expected output
1: cy ok
2: dee ok
banned: none

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

Hints

Hint 1

Keep a dictionary of strikes per user and a set of banned users.

Hint 2

Check if the user is already banned before you look at the message.

Hint 3

A message breaks the rules if any banned word is inside its lower-case text.

Solution

Show one correct solution (try it yourself first)
words = [w.strip().lower() for w in input().split(",")]
n = int(input())
strikes = {}
banned = set()
for number in range(1, n + 1):
    user, text = input().split("|", 1)
    if user in banned:
        print(f"{number}: {user} ignored")
        continue
    lowered = text.lower()
    if any(word in lowered for word in words):
        strikes[user] = strikes.get(user, 0) + 1
        result = ["warning", "muted", "banned"][strikes[user] - 1]
        if strikes[user] == 3:
            banned.add(user)
        print(f"{number}: {user} {result}")
    else:
        print(f"{number}: {user} ok")
print("banned: " + (", ".join(sorted(banned)) if banned else "none"))

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

Lesson: Communication and the code of conduct →