easy +8 pts

Stack Trace Sanitizer

Clean absolute file paths from Python traceback lines, keeping quotes intact.

You are given a multi-line string that looks like a Python traceback. Each line may contain file paths that are absolute or relative, like `/home/user/project/module.py`. Your task is to write a function `sanitize_traceback(text: str) -> str` that returns the same text but with every file path replaced by its basename (the part after the last `/`). A file path is defined as a contiguous sequence of non-whitespace characters that contains at least one `/` and at least one letter. Replace the entire path with its basename. All other characters (spaces, punctuation, line breaks) remain unchanged. If a path ends with a colon, comma, or other non-letter character, you must include that character in the basename (i.e., strip only the directory, not trailing delimiters). Paths may use forward slashes only. Assume paths are not quoted and are separated from surrounding text by whitespace or line boundaries. Note that the basename may include quoted filename parts like `"module.py` or `module.py"` — in that case the quotes stay. Also, keep any surrounding quotes (like `"..."`) intact; only the path component inside them is shortened.

Constraints

The input will be a non-empty string of length at most 10,000. The function should process the string in a single pass, so O(n) time is expected.

Example

>>> sanitize_traceback('File "/home/user/project/module.py", line 3\n    x = 1')
'File "module.py", line 3\n    x = 1'
>>> sanitize_traceback('  File "/usr/lib/python3.9/os.py:123", in foo')
'  File "os.py:123", in foo'
>>> sanitize_traceback('Traceback (most recent call last):\n  File "/home/user/app/main.py", line 5, in <module>')
'Traceback (most recent call last):\n  File "main.py", line 5, in <module>'
>>> sanitize_traceback('No paths here')
'No paths here'
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a regex to find sequences of non-whitespace characters that contain a slash and a letter.
For each match that contains a slash, find the last slash and take the substring after it.
Preserve any leading or trailing quotes that are part of the matched token.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.