easy +8 pts

Longest word in a sentence

Find the longest word in a sentence, breaking ties by first occurrence.

Write a function `longest_word(sentence: str) -> str` that takes a sentence (a string) and returns the longest word in the sentence. A word is defined as a maximal sequence of non-space characters. You may assume the sentence consists of letters, digits, punctuation, and spaces, and contains at least one word. Words are separated by one or more spaces. If two or more words have the same maximum length, return the one that appears first in the sentence. Punctuation is considered part of a word (e.g., "hello," is a word of length 6).

Constraints

1 <= len(sentence) <= 1000. The sentence contains at least one non-space character. Avoid using regex; simple splitting is enough.

Example

>>> longest_word("The quick brown fox")
'quick'
>>> longest_word("Hello world")
'Hello'
>>> longest_word("a bb ccc")
'ccc'
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use the `split()` method to divide the sentence into words based on whitespace.
Track the best word and its length while iterating; update only when the current word is longer.
The split() method handles multiple spaces automatically.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.