lamiastella

simplify

Jun 2nd, 2016
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. class Solution:
  2.     # @param A : string
  3.     # @return a strings
  4.     #path = "/home/", => "/home"
  5.     #path = "/a/./b/../../c/", => "/c"
  6.     def simplifyPath(self, A):
  7.         stack = []
  8.         result = ''
  9.         for char in A:
  10.             stack.append(char)
  11.         if A[len(A)-1] == '/':
  12.             last_char_slash = True
  13.         else:
  14.             last_char_slash = False
  15.         while True:
  16.             tmp = stack.pop()
  17.             if tmp == '/' and last_char_slash == True:
  18.                 continue
  19.             if tmp != '/':
  20.                 result += tmp
  21.             elif tmp == '/':
  22.                 result += tmp
  23.                 return result[::-1]
  24.  
  25. s = Solution()
  26. print(s.simplifyPath("/home/"))
  27.  
  28.  
  29.  
  30.  
  31. /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4 /Users/mona/PycharmProjects/PythonCodes/simplify_path.py
  32. Traceback (most recent call last):
  33.   File "/Users/mona/PycharmProjects/PythonCodes/simplify_path.py", line 26, in <module>
  34.     print(s.simplifyPath("/home/"))
  35.   File "/Users/mona/PycharmProjects/PythonCodes/simplify_path.py", line 16, in simplifyPath
  36.     tmp = stack.pop()
  37. IndexError: pop from empty list
  38.  
  39. Process finished with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment