Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-094

Simplify Path

Problem

Simplify a Unix-style absolute path to its canonical form.

Split on "/"; skip empty parts and ".", pop the stack for "..", and push ordinary names. The canonical path always starts with a single slash and never ends with one except at the root.

Return the canonical path.

Hint

A ".." at the root has no effect because the stack is already empty.

Test cases

case 1
in: ["/home/"]
expected: "/home"
case 2
in: ["/../"]
expected: "/"
case 3
in: ["/home//foo/"]
expected: "/home/foo"
case 4
in: ["/a/./b/../../c/"]
expected: "/c"
case 5
in: ["/a/../../b/../c//.//"]
expected: "/c"
Starter codePython
def simplify_path(path):
    # Your code here
    pass

5 test cases · run your code against them in the editor