easy +10 pts

Extract mentions

Pull out unique @username mentions from text, preserving first appearance order.

Write a function `extract_mentions(text: str) -> list[str]` that takes a string `text` and returns a list of unique usernames mentioned in the text. A mention is defined as an '@' character immediately followed by one or more characters that are lowercase or uppercase letters or digits (`a-z`, `A-Z`, `0-9`). The mention ends when a non-alphanumeric character is encountered (or end of string). However, if the character immediately after the alphanumeric run is a dot followed by more alphanumeric characters, the dot and subsequent alphanumeric characters are considered part of the same mention (to handle email-like patterns). Usernames should be extracted exactly as they appear (case-sensitive) and returned in order of their first occurrence in the text. Each username should appear only once, even if mentioned multiple times. If there are no mentions, return an empty list. The input may contain multiple spaces, punctuation, newlines, and other symbols.

Constraints

The input `text` is a non-empty string of length at most 10^5. Output list length is at most the number of unique mentions, which is bounded by the input length.

Example

>>> extract_mentions('Hey @alice, @bob! How are you @alice?')
['alice', 'bob']
>>> extract_mentions('Contact us at @support [at] example.com')
['support']
>>> extract_mentions('No mentions here')
[]
>>> extract_mentions('@hello_world @php@example.com')
['hello', 'php', 'example.com']
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Scan the string for '@' and then accumulate letters and digits.
After the alphanumeric sequence, check if a '.' followed by alphanumeric characters appears; if so, include it.
Use a set to track seen usernames and a list to preserve order.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.