A small service builds text badges for notifications from incoming events. After a recent change, preview results started depending on previous calls, and some scenarios began returning unexpected labels for different channels.
build_badges(events, limit=3) function must:
{"channel": str, "count": int};limit strings in the format <channel>: <count>;count <= 0;make_channel_formatters(channels) function must return a dictionary where:
<channel>: <count> specifically for its own channel.notifications.py file.from notifications import build_badges, make_channel_formatters
first = build_badges([
{"channel": "email", "count": 2},
])
second = build_badges([
{"channel": "sms", "count": 1},
])
formatters = make_channel_formatters(["email", "sms", "push"])
labels = [
formatters["email"](3),
formatters["sms"](4),
formatters["push"](5),
]
print(first)
print(second)
print(labels)
build_badges() must be independent of each other.second must contain only badges from its own input list.make_channel_formatters() must use its own channel name corresponding to its key in the dictionary.CPython 3.12build_badges(events, limit=3) -> list[str]make_channel_formatters(channels) -> dict[str, callable]channel and count keys with correct typesUse line comments to explain the problem, its impact, the fix and how to test it.