medium +20 pts

Change directory manager

Parse `cd` commands and compute the absolute path after a series of directory changes.

You are given a starting absolute path (e.g., `"/a/b"`) and a list of `cd` commands (e.g., `["..", "c", "d/", "."]`). Your task is to simulate the commands and return the resulting absolute path. Define the function `cd_manager(start: str, commands: list[str]) -> str` that returns the absolute path after applying all commands. Rules: - The starting path is always absolute (begins with `/`) and may contain multiple slashes (e.g., `"/a//b/"`). You should normalize it to a single slash between components. - Each command is a string. It can be: - `".."` — move up one directory. If already at root (`"/"`), stay at root. - `"."` — stay in the current directory (no effect). - a directory name (possibly with trailing slashes, e.g., `"c/"`) — move into that directory. - Commands are applied sequentially. - The output must be a normalized absolute path: no trailing slashes except for root `"/"`, and no consecutive slashes. - You may also have commands like `"folder/"` (trailing slash), which should be treated as `"folder"`. Implement the function without using any path manipulation libraries (like `os.path`). Use string processing and a stack. Note: The input will always be valid; you do not need to handle invalid commands.

Constraints

- `1 <= len(start) <= 200` - `0 <= len(commands) <= 200` - Each command is either `".."`, `"."`, or a non-empty string of lowercase letters and digits, possibly with one or more trailing slashes. - Total length of all commands combined <= 2000. - No commands contain `"/"` inside except trailing slashes. - Time complexity: O(total length of start + commands). - Space: O(number of components).

Example

```python
>>> cd_manager("/a/b", ["..", "c", "d/", "."])
"/a/c/d"
>>> cd_manager("/a//b/", ["..", "..", ".."])
"/"
>>> cd_manager("/", ["usr", "local", ".."])
"/usr"
```
20 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Split the start path by `/` and ignore empty strings to get components.
Use a stack to keep track of the current directory components; pop on `..` and push on directory names.
Remember to normalize the final path: join with `/` and prepend `/`; if stack is empty, return `"/"`.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.