Skip to content

Commit

Permalink
solve "71. Simplify Path"
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Oct 19, 2024
1 parent 8302201 commit a341761
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .plan
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ solve all "stack" questions in top-150
complete top interview 150
solve each available question at least once

Oct 19, 2024

* solve "71. Simplify Path"

Oct 16, 2024

* re-solve "20. Valid Parentheses"
Expand Down
12 changes: 12 additions & 0 deletions src/simplify_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def simplifyPath(self, path: str) -> str:
stack: list[str] = []

for part in path.strip("/").split("/"):
if part == "..":
if stack:
stack.pop()
elif part and part != ".":
stack.append(part)

return "/" + "/".join(stack)
17 changes: 17 additions & 0 deletions tests/test_simplify_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from src.simplify_path import Solution


@pytest.mark.parametrize(
"path,expected",
(
("/home/", "/home"),
("/home//foo/", "/home/foo"),
("/home/user/Documents/../Pictures", "/home/user/Pictures"),
("/../", "/"),
("/.../a/../b/c/../d/./", "/.../b/d"),
),
)
def test_solution(path, expected):
assert Solution().simplifyPath(path) == expected

0 comments on commit a341761

Please sign in to comment.