71 Simplify Path
class Solution:
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
tokens = [token for token in path.split('/') if token != '' and token != '.']
s = []
for token in tokens:
if token == '..':
if s: # separated ifs for case where '...'
s.pop()
else:
s.append(token)
return '/' + '/'.join(s)
obj = Solution()
print(obj.simplifyPath('/...')) # /...
print(obj.simplifyPath('/../')) # /
print(obj.simplifyPath('/')) # /
print(obj.simplifyPath('/a/./b/../../c/')) # /c
print(obj.simplifyPath('/home//foo/')) # /home/fooLast updated