easy +10 pts

Reverse Words in a Sentence

Given a sentence, return a new string with the order of words reversed, preserving single spaces between words.

Write a function `reverse_words(sentence: str) -> str` that takes a string `sentence` consisting of words separated by exactly one space. The function should return a new string containing the same words in reverse order, still separated by exactly one space. The input will not have leading, trailing, or multiple consecutive spaces.

Constraints

- `sentence` is a non-empty string. - Words consist of printable ASCII characters (no spaces inside). - There is exactly one space between words. - Length of `sentence` is at most 10^5. - Time complexity: O(n), where n is the length of the string.

Example

>>> reverse_words("hello world")
"world hello"
>>> reverse_words("Python is fun")
"fun is Python"
>>> reverse_words("single")
"single"
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

You can split the sentence into a list of words using the `split()` method.
Reverse the list and join it back into a string with a space separator.
Remember that Python strings are immutable, so you'll return a new string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.