forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestPath.py
More file actions
executable file
·88 lines (67 loc) · 2.62 KB
/
longestPath.py
File metadata and controls
executable file
·88 lines (67 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
""" longestPath -- 45 min
Suppose we represent our file system as a string. For example, the string
"user\n\tpictures\n\tdocuments\n\t\tnotes.txt" represents:
user
pictures
documents
notes.txt
The directory user contains an empty sub-directory pictures and a
sub-directory documents containing a file notes.txt.
The string
"user\n\tpictures\n\t\tphoto.png\n\t\tcamera\n\tdocuments\n\t\tlectures
\n\t\t\tnotes.txt" represents:
user
pictures
photo.png
camera
documents
lectures
notes.txt
The directory user contains two sub-directories pictures and documents.
pictures contains a file photo.png and an empty second-level sub-directory
camera. documents contains a second-level sub-directory lectures containing
a file notes.txt.
We want to find the longest (as determined by the number of characters)
absolute path to a file within our system. For example, in the second
example above, the longest absolute path is "user/documents/lectures/notes.txt"
, and its length is 33 (not including the double quotes).
Given a string representing the file system in this format, return the
length of the longest absolute path to a file in the abstracted file
system. If there is not a file in the file system, return 0.
Note: Due to system limitations, test cases use form feeds ('\f', ASCII
code 12) instead of newline characters.
Example
For fileSystem = "user\f\tpictures\f\tdocuments\f\t\tnotes.txt", the
output should be
longestPath(fileSystem) = 24.
The longest path is "user/documents/notes.txt", and it consists of 24
characters.
Input/Output
[execution time limit] 4 seconds (py3)
[input] string fileSystem
File system in the format described above. It is guaranteed that:
the name of a file contains at least a . and an extension;
the name of a directory or sub-directory does not contain a ..
Note: Due to system limitations, newline characters are given as form
feeds ('\f', ASCII code 12) in our test cases.
Guaranteed constraints:
1 ≤ fileSystem.length ≤ 6310.
[output] integer
"""
def longestPath(filesys):
lenLst, lastLevel, lenMax = [-1], -1, 0
lstStr = filesys.splitlines()
for item in lstStr:
nameClean = item.lstrip('\t')
curLevel = len(item) - len(nameClean)
while curLevel <= lastLevel:
lenLst.pop()
lastLevel -= 1
lenLst.append(len(nameClean) + lenLst[-1] + 1)
lastLevel = curLevel
if '.' in item:
lenMax = max(lenMax, lenLst[-1])
return lenMax
print(longestPath(
('user\f\tpictures\f\t\tphoto.png\f\t\tcamera\f\tdocuments\f\t\tlectures\f\t\t\tnotes.txt')
)) # 33