Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- test_1 = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
- def lengthLongestPath(string):
- # a stack, each element is a tuple ("dir_name", length of name)
- pwd = []
- prefix_length = 0
- currMax = -1
- # if we see \n means we go into the next directory
- # the number of \t s folllowing \n is the number of elements in pwd we need to keep
- items = string.split("\n")
- for i in items:
- tab_split = i.split("\t")
- # length of tab_split - 1 is the no of elem we want to keep in pwd
- while (len(pwd) > len(tab_split) -1): # remove excess items
- prefix_length -= pwd.pop()[1] # index 1 is the length
- curr_item = tab_split[-1]
- # cannot find dot, is a directory
- if not curr_item.find('.'):
- pwd += (curr_item, len(curr_item))
- else: # current item is a file
- # length of current path = filename_len + prefix_length + no of slashes
- currMax = max(currMax, len(curr_item) + prefix_length + len(pwd))
- return currMax
- lengthLongestPath(test_1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement