lamiastella

simpl

Jun 2nd, 2016
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1.  
  2. # @param A : string
  3. # @return a strings
  4. #path = "/home/", => "/home"
  5. #path = "/a/./b/../../c/", => "/c"
  6. import os
  7. class Solution:
  8. def abs_path(self, string):
  9. return os.path.abspath(string)
  10.  
  11. def simplifyPath(self, A):
  12. stack = []
  13. result = ''
  14. path_list = A.split("/")
  15. for i in range(len(path_list)-1, 0, -1):
  16. if path_list[i] != '':
  17. stack.append(path_list[i])
  18. # stack :
  19. while len(stack) != 0:
  20. path = stack.pop()
  21. if path == "..":
  22. if len(stack) != 0:
  23. stack.pop()
  24. else:
  25. result += '/
  26. if path == ".":
  27. continue
  28. else:
  29. result += '/'+path
  30. return result
  31.  
  32.  
  33.  
  34.  
  35. s = Solution()
  36. print(s.simplifyPath("/../"))
  37. print(s.abs_path("/../"))
Advertisement
Add Comment
Please, Sign In to add comment