Skip to content

Commit 1b419ae

Browse files
authored
Create problem17
1 parent 2507ff0 commit 1b419ae

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

daily_coding_problems/problem17

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
Daily Coding Problem #17
2+
Problem
3+
This problem was asked by Google.
4+
5+
Suppose we represent our file system by a string in the following manner:
6+
7+
The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
8+
9+
dir
10+
subdir1
11+
subdir2
12+
file.ext
13+
The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
14+
15+
The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
16+
17+
dir
18+
subdir1
19+
file1.ext
20+
subsubdir1
21+
subdir2
22+
subsubdir2
23+
file2.ext
24+
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.
25+
26+
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).
27+
28+
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.
29+
30+
Note:
31+
32+
The name of a file contains at least a period and an extension.
33+
34+
The name of a directory or sub-directory will not contain a period.
35+
36+
Solution
37+
There are two steps in solving this question: we must first parse the string representing the file system and then get the longest absolute path to a file.
38+
39+
Step 1: Parsing the file system
40+
Ideally, we would initially parse the string given into a dictionary of some sort. That would mean a string like:
41+
42+
dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext
43+
would become:
44+
45+
{
46+
"dir": {
47+
"subdir1": {
48+
"file1.ext": True,
49+
"subsubdir1": {}
50+
},
51+
"subdir2": {
52+
"subsubdir2": {
53+
"file2.ext": True
54+
}
55+
}
56+
}
57+
}
58+
where each key with a dictionary as its value represents a directory, and a key with True as its value represents an actual file.
59+
60+
To achieve this, we can first split the string by the newline character, meaning each item in our array represents a file or directory. Then, we create an empty dictionary to represent our parsed file system and traverse the file system on each entry. We keep track of the last path we've seen so far in current_path because we may need to return to some level in that path, depending on the number of tabs. Once we are at the correct place to put down the new directory or file, we check the name for a . and set the correct value to either True (if file) or {} (if directory).
61+
62+
def build_fs(input):
63+
fs = {}
64+
files = input.split('\n')
65+
66+
current_path = []
67+
for f in files:
68+
indentation = 0
69+
while '\t' in f[:2]:
70+
indentation += 1
71+
f = f[1:]
72+
73+
current_node = fs
74+
for subdir in current_path[:indentation]:
75+
current_node = current_node[subdir]
76+
77+
if '.' in f:
78+
current_node[f] = True
79+
else:
80+
current_node[f] = {}
81+
82+
current_path = current_path[:indentation]
83+
current_path.append(f)
84+
85+
return fs
86+
Step 2: Computing the longest path
87+
After we've constructed a native representation of the file system, we can write a fairly straightforward recursive function that takes the current root, recursively calculates the longest_path of all the subdirectories and files under the root, and returns the longest one. Remember that since we specifically want the longest path to a file to discard any paths that do not have a . in them. And if there are no paths starting at this root, then we can simply return the empty string.
88+
89+
def longest_path(root):
90+
paths = []
91+
for key, node in root.items():
92+
if node == True:
93+
paths.append(key)
94+
else:
95+
paths.append(key + '/' + longest_path(node))
96+
# filter out unfinished paths
97+
paths = [path for path in paths if '.' in path]
98+
if paths:
99+
return max(paths, key=lambda path:len(path))
100+
else:
101+
return ''
102+
Step 3: Putting it together
103+
Now that the hard part is done, we just need to put the two together:
104+
105+
def longest_absolute_path(s):
106+
return len(longest_path(build_fs(s)))
107+
This runs in O(n), since we iterate over the input string twice to build the file system, and then in the worst case we go through the string again to compute the longest path.

0 commit comments

Comments
 (0)