-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy path068 Simplify Path.py
52 lines (40 loc) · 1.56 KB
/
068 Simplify Path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
"""
__author__ = 'Danyang'
class Solution(object):
def simplifyPath(self, path):
"""
use "." as intermediate
:param path: a string
:return: a string
"""
path = path.split("/")
path = filter(lambda x: x not in ("", " ", "."), path)
# modify the content of the list, not the structure.
for idx in xrange(len(path)):
val = path[idx]
if val == "..":
path[idx] = "."
# rm a previous meaningful part
i = idx-1
while i >= 0 and path[i] == ".": i -= 1
if i >= 0: path[i] = "." # avoid path[-1]
path = filter(lambda x: x not in (".",), path)
if not path:
return "/"
path = map(lambda x: "/"+x, path)
return "".join(path)
if __name__ == "__main__":
assert Solution().simplifyPath("/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///") == "/e/f/g"
assert Solution().simplifyPath("/a/./b/../../c/") == "/c"
assert Solution().simplifyPath("/../") == "/"