progress

~/problems / Stacks / Stack: path parsing

Current folder after each move

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy basics ~10 min

You start in the top folder, written "/". Each move is either a folder name, which steps into that folder inside the one you're in, or "..", which steps back out to the parent. At "/" there is no parent, so ".." does nothing.

Write current_folders(moves: list[str]) -> list[str] that returns, after each move, the name of the folder you're in ("/" at the top).

current_folders(["docs", "notes", "..", "pics", "..", "..", ".."])
# ["docs", "notes", "docs", "pics", "docs", "/", "/"]
current_folders(["a", "a", ".."])   # ["a", "a", "a"]   (a folder "a" inside a folder "a")
current_folders([])                 # []

Constraints: up to 10^4 moves; folder names use letters and digits only.

Show hint

keep the folders you're inside in a list used as a stack: append a name, pop on "..", and the top is where you are.

Topic: Stack: path parsing. Resolve ., .. and symlinks with a stack.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc