-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsimplify.py
24 lines (20 loc) · 943 Bytes
/
simplify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def simplifyPath(self, path: str) -> str:
# split on '/'
# now path contains only valid folder names and
# unwanted stuff - '' '.' '..'
path = path.split('/')
res = []
for p in path:
if p != "" and p != ".": # ignore if it dot - '.' or empty - ''
if p == "..": # pop if its double dots - '..'
if res: # check if there is something to pop
res.pop()
else:
res.append(p) # if it makes it till here, it is part of solution
return '/' + '/'.join(res) # rejoin the res by '/' and add '/' in front
# Checking in console
if __name__ == '__main__':
Instant = Solution()
Solve = Instant.simplifyPath(path = "/home//foo/") # path = "/home//foo/" -> "/home/foo"
print(Solve)