easy +10 pts

Longest Word Finder

Find the longest word in a given string, ignoring punctuation and ties by earliest occurrence.

Write a function `longest_word(text: str) -> str` that takes a string `text` and returns the longest word in it. Words are defined as contiguous sequences of alphabetic characters (letters A-Z and a-z). All non-letter characters (spaces, digits, punctuation) act as separators. If multiple words have the same maximum length, return the one that appears earliest in the string. If the string contains no words, return an empty string. The function should be case-insensitive for the purpose of identification, but should return the word exactly as it appears in the input.

Constraints

Input will contain at most 10,000 characters. The function must run in O(n) time where n is the length of the string, or O(n log n) worst-case, but a simple scan is expected.

Example

[">>> longest_word('Hello world!')\n'Hello'", ">>> longest_word('The quick brown fox')\n'quick'", ">>> longest_word('a bb ccc')\n'ccc'", ">>> longest_word('123 456')\n''"]
10 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Iterate through the characters while building the current word. When you hit a non-letter, finalize the current word.
Keep track of the current word's starting index to break ties by earliest occurrence.
Use string methods like `isalpha()` to check if a character is a letter.
Handle the case where the string ends with a word: finalize it after the loop.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.