easy +10 pts

Strip and Collapse Whitespace

Clean up messy strings by trimming ends and replacing runs of whitespace with a single space.

Write a function `clean_whitespace(text: str) -> str` that returns a new string with: - leading and trailing whitespace removed, - every inner run of whitespace (spaces, tabs, newlines, etc.) replaced by exactly one space character. Whitespace includes space, tab (`\t`), newline (`\n`), carriage return (`\r`), and other Unicode whitespace characters. No other characters are changed. The function should work for any string. **Signature:** ```python def clean_whitespace(text: str) -> str: ... ```

Constraints

Input is a string of length 0 to 100,000. Time complexity O(n), memory O(n). The output must not contain any runs of whitespace longer than one space. Empty and all-whitespace inputs produce an empty string.

Example

```python
>>> clean_whitespace("  Hello   world!  ")
'Hello world!'
>>> clean_whitespace("a\t\nb\n\n c")
'a b c'
>>> clean_whitespace("   ")
''
>>> clean_whitespace("")
''
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `text.split()` to split on any whitespace and then join with a single space – this naturally removes empty pieces.
`str.split()` with no arguments splits on runs of whitespace, so you don't need a regex.
After splitting, joining with `' '.join(...)` and returning the result handles both trimming and collapsing.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.