Created
September 15, 2019 20:23
-
-
Save santhalakshminarayana/9b3c9e07838d3c90004ccaa5b2925666 to your computer and use it in GitHub Desktop.
Given a string representing the file system, return the length of the longest absolute path to a file in the abstracted file system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Suppose we represent our file system by a string in the following manner: | |
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: | |
dir | |
subdir1 | |
subdir2 | |
file.ext | |
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext. | |
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents: | |
dir | |
subdir1 | |
file1.ext | |
subsubdir1 | |
subdir2 | |
subsubdir2 | |
file2.ext | |
The directory dir contains two sub-directories subdir1 and subdir2. | |
subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1. | |
subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext. | |
We are interested in finding the longest (number of characters) absolute path to a file within our file system. | |
For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", | |
and its length is 32 (not including the double quotes). | |
Given a string representing the file system in the above format, | |
return the length of the longest absolute path to a file in the abstracted file system. | |
If there is no file in the system, return 0. | |
Note: | |
The name of a file contains at least a period and an extension. | |
The name of a directory or sub-directory will not contain a period. | |
''' | |
class Node: | |
def __init__(self,name): | |
self.name=name | |
self.is_file=False | |
self.next=[] | |
class File_System: | |
def __init__(self): | |
self.root=None | |
self.stack=[] | |
self.has_file=False | |
def add_dir(self,path): | |
curr_par=self.stack[-1] | |
if curr_par.name.count('\t') >= path.count('\t'): | |
while(curr_par.name.count('\t') != path.count('\t')): | |
self.stack.pop() | |
curr_par=self.stack[-1] | |
self.stack.pop() | |
curr_par=self.stack[-1] | |
dir_node=Node(path) | |
if '.ext' in path: | |
dir_node.is_file=True | |
self.has_file=True | |
curr_par.next.append(dir_node) | |
if dir_node.is_file is False: | |
self.stack.append(dir_node) | |
def create(self,fs_string): | |
if len(fs_string) is 0: | |
return | |
paths=fs_string.split('\n') | |
for path in paths: | |
if self.root==None: | |
self.root=Node(path) | |
self.is_file=False | |
self.stack.append(self.root) | |
else: | |
self.add_dir(path) | |
def get_path(self): | |
if self.has_file is False: | |
return 0 | |
queue=[] | |
queue.append((self.root,self.root.name)) | |
long_path=len(self.root.name) | |
while(len(queue)!=0): | |
node_path=queue.pop(0) | |
for i in range(len(node_path[0].next)): | |
if node_path[0].next[i].is_file == True: | |
long_path=max(long_path,len(node_path[1]+'/'+node_path[0].next[i].name.replace('\t',''))) | |
else: | |
queue.append((node_path[0].next[i], \ | |
(node_path[1]+"/"+node_path[0].next[i].name).replace('\t',''))) | |
return long_path | |
if __name__=="__main__": | |
fs_path="dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" | |
fs=File_System() | |
fs.create(fs_path) | |
print(fs.get_path()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment