Normalize a Path

mediumpy_strings

Implement normalize_path(path) — like os.path.normpath, but you write it.

Collapse repeated slashes, drop . components, and resolve .. against the preceding component. A leading .. survives in a relative path; .. at the root of an absolute path is dropped. The empty path and "." normalize to "."; the root stays "/". For example normalize_path("/a/b/../c") == "/a/c" and normalize_path("a/..///../b") == "../b".

pathlib and os.path.normpath are not allowed. Do it in a single O(len(path)) pass — some tests use very long paths.

Constraints
  • no pathlib
  • no normpath

Your solution

Edit normalize_path and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

Split on '/'. Skip '' and '.' components; on '..' pop the stack (unless it's empty / its top is '..').

Hint 2

Track whether the path is absolute (leading '/'); rejoin with '/'. Empty relative result is '.'.