Algobase
Problems
Get Premium
Pricing
Problems
/
71. Simplify Path
Prev
Next
Visualizer
Problem
Solution
Code
/home//foo
/home/user/../docs/./files/
/../
⌥
Visualizer will appear here
Problem
Solution
Code
1
def simplify_path(path):
2
stack = []
3
for part in path.split('/'):
4
if part == '..':
5
if stack: stack.pop()
6
elif part and part != '.':
7
stack.append(part)
8
return '/' + '/'.join(stack)
Visualizer
/home//foo
/home/user/../docs/./files/
/../
⌥
Visualizer will appear here