Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

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".

Difficulty: interview

Starter code:

class Solution:
    def simplifyPath(self, path: str) -> str:
        

Solutions:
class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         stack=[]
         path=[p for p in path.split('/') if p]
         for f in path:
             if f == '.': continue
             elif f == '..': 
                 if stack: stack.pop()
             else: stack.append(f)
         return '/'+'/'.join(stack)

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         res=[]
         str_list=path.split('/')
         loc=len(str_list)-1
         for i in range (len(str_list)):
             ss=str_list[i]
             if ss=='':
                 continue
             if ss=='.':
                 continue
             if ss=='..':
                 if len(res)>0:
                     res.pop()
             else:
                 res.append(ss)
                 
         return '/'+'/'.join(res)
                 


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         stack = []
         for p in path.split("/"):
             if p == "..":
                 if stack:
                     stack.pop()
             elif p and p != '.':
                 stack.append(p)
         return "/" + "/".join(stack)

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         
         lst = list([_f for _f in path.split("/") if _f])
         print(lst)
         stack = []
         
         for item in lst:
             if item == "..":
                 if not stack:
                     continue
                 stack.pop()
             elif item == ".":
                 print("here")
                 continue
             else:
                 stack.append(item)
         
         return "/"+"/".join(stack)


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         place = [p for p in path.split('/') if p != '.' and p != '' ]
         stack = []
         for p in place:
             if p == '..':
                 if len(stack) > 0:
                     stack.pop()
             else:
                 stack.append(p)
         return '/'+'/'.join(stack)


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         l = []
         ans = '/'
         names = path.split('/')
         print(('names', names))
         for s in names:
             if s == '..':
                 # if len(l) == 0:
                 #     l.append(s)
                 # else:
                 #     l.pop()
                 if len(l)!=0:
                     l.pop()
             elif s == '.':
                 continue
             elif s == '':
                 continue
             else:
                 l.append(s)
         print(('l: ', l))
         for s in l:
             ans += s + '/'
         n = len(ans)
         if n > 1:
             return ans[:len(ans)-1]
         else:
             return ans


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         place=[p for p in path.split('/') if p!='.' and p!='' ]
         result=[]
         for p in place:
             if p=='..':
                 if len(result)>0:
                     result.pop()
             else:
                 result.append(p)
         return '/'+'/'.join(result)
                 


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         stack = []
         tmpstr = ''
         path += '/'
         for c in path:
             if c == '/':
                 if tmpstr:
                     if tmpstr == '..':
                         if stack:
                             stack.pop()
                     elif tmpstr != '.':
                         stack.append(tmpstr)
                 tmpstr = ''
             else:
                 tmpstr += c
         return '/'+"/".join(stack)

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         if not path or len(path)==1:
             return path
         stack = []
         list_path = path.split('/')
         for ch in list_path:
             if ch == '..':
                 if stack: stack.pop()
             elif ch != '.' and ch:
                 stack.append(ch)
         return '/'+'/'.join(stack)

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         n=len(path)
         if(n==0): return path
 
         v=[]
         i=0
         while(i<n):
             while(i<n and path[i]=='/' ):i=i+1
             if (i == n): break
             
             left=i
             while(i<n and path[i]!='/'):i=i+1
             right=i-1
             
             s=path[left:right+1]
             
             if s=="..":
                 if (len(v)!=0):
                     v.pop() 
             elif s!= ".":
                 v.append(s)
 
         if(len(v)==0):
             return "/"
         
         ret=''
         for i in range(len(v)):
             ret=ret+'/'+v[i]
 
         return ret

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         
         tokens = path.split('/')
         stack = list()
         for token in tokens:
             if token != '':
                 stack.append(token)
         
         res = ''
         back = 0
         while stack:
             top = stack.pop()
             if top == '.':
                 continue
             elif top == '..':
                 back = back + 1
             else:
                 if back == 0:
                     res = '/'+top+res
                 else:
                     back = back - 1
                     
         if res == '':
             return '/'
         return res 
                         


class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
     
         
         places = [tmp for tmp in path.split("/") if tmp!="." and tmp!=""]
         stack = []
         for name in places:
             
             if name == "." or name == "":
                 continue
             elif name == "..":
                 if len(stack) > 0:
                     stack.pop()
             else:
                 stack.append(name)
                     
         return "/" + "/".join(stack)

class Solution:
     def simplifyPath(self, path):
         """
         :type path: str
         :rtype: str
         """
         path = path.split("/")
         stack = []
         for n in path:
             if n == "" or n == ".":
                 continue
             elif n == "..":
                 if stack != []:
                     stack.pop()
             else:
                 stack.append(n)
                 
         return "/" + '/'.join(stack)