Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- # @param A : string
- # @return a strings
- #path = "/home/", => "/home"
- #path = "/a/./b/../../c/", => "/c"
- def simplifyPath(self, A):
- stack = []
- result = ''
- for char in A:
- stack.append(char)
- if A[len(A)-1] == '/':
- last_char_slash = True
- else:
- last_char_slash = False
- while True:
- tmp = stack.pop()
- if tmp == '/' and last_char_slash == True:
- continue
- if tmp != '/':
- result += tmp
- elif tmp == '/':
- result += tmp
- return result[::-1]
- s = Solution()
- print(s.simplifyPath("/home/"))
- /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/mona/PycharmProjects/PythonCodes/simplify_path.py
- Traceback (most recent call last):
- File "/Users/mona/PycharmProjects/PythonCodes/simplify_path.py", line 26, in <module>
- print(s.simplifyPath("/home/"))
- File "/Users/mona/PycharmProjects/PythonCodes/simplify_path.py", line 16, in simplifyPath
- tmp = stack.pop()
- IndexError: pop from empty list
- Process finished with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment