The simplify_path
function takes a Unix-style file path as input, splits it into tokens, and uses a stack to remove unnecessary directory references, returning the simplified path. The function handles various edge cases, including parent directories (..
), multiple parent directories (...
), and trailing slashes.
def simplify_path(path):
"""Simplify Unix-style file path."""
stack = []
tokens = [t for t in path.split('/') if t != '.' and t != '']
for token in tokens:
if token != '..':
stack.append(token)
elif stack:
stack.pop()
return '/'+'/'.join(stack)
assert simplify_path('/') == '/'
assert simplify_path('/../') == '/'
assert simplify_path('/...') == '/...'
assert simplify_path('/.../') == '/...'
assert simplify_path('/foo/..') == '/'
assert simplify_path('/foo///.//bar//') == '/foo/bar'
print('All passed!')
python
def simplify_path(path: str) -> str:
"""
Simplify Unix-style file path.
Args:
path (str): Unix-style file path.
Returns:
str: Simplified file path.
"""
stack = []
tokens = [t for t in path.split('/') if t!= '.' and t!= '']
# Iterate over each token in the path
for token in tokens:
# If the token is '..', pop the last element from the stack if it's not empty
if token == '..':
if stack:
stack.pop()
# If the token is not '.' or '', add it to the stack
elif token:
stack.append(token)
# Join the elements in the stack with '/' and add a '/' at the beginning
return '/' + '/'.join(stack)
assert simplify_path('/') == '/'
assert simplify_path('/../') == '/'
assert simplify_path('/...') == '/...'
assert simplify_path('/.../') == '/...'
assert simplify_path('/foo/..') == '/'
assert simplify_path('/foo///.//bar//') == '/foo/bar'
print('All passed!')
simplify_path(path)
Simplifies a Unix-style file path by removing unnecessary directory references.
path
: The Unix-style file path to be simplified.The simplified file path.
Split the path into tokens: The path.split('/')
function breaks the path into a list of tokens, where each token represents a directory or an empty string.
[t for t in path.split('/') if t!= '.' and t!= '']
filters out empty strings and the current directory (.
) from the list of tokens.Initialize a stack: An empty stack is created to store the directory tokens.
Process each token:
..
(the parent directory), it is pushed onto the stack...
and the stack is not empty, the top directory is popped from the stack, effectively moving up to the parent directory.Join the stack into a path: The simplified path is constructed by joining the tokens in the stack with /
as the delimiter.
Return the simplified path: The simplified path is returned as the result.
simplify_path('/')
returns /
, as the root directory is the only directory in the path.simplify_path('/../')
returns /
, as the ..
token effectively moves up to the root directory.simplify_path('/...')
returns /...
, as the ...
token represents multiple parent directories and is not simplified.simplify_path('/.../')
returns /.../
, as the extra /
is not removed.simplify_path('/foo/..')
returns /
, as the ..
token effectively moves up to the root directory.simplify_path('/foo///.//bar//')
returns /foo/bar
, as the unnecessary directories are removed.