FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-algorithms/algorithms/stack/longest_abs_path.py at master · ilyvsc/python-algorithms · GitHub
ilyvsc
/
python-algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-algorithms
/
algorithms
/
stack
/
longest_abs_path.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (60 loc) · 2.25 KB
Breadcrumbs
python-algorithms
/
algorithms
/
stack
/
longest_abs_path.py
Copy path
File metadata and controls
65 lines (60 loc) · 2.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# def lengthLongestPath(input):
# maxlen = 0
# pathlen = {0: 0}
# for line in input.splitlines():
# print("---------------")
# print("line:", line)
# name = line.strip('\t')
# print("name:", name)
# depth = len(line) - len(name)
# print("depth:", depth)
# if '.' in name:
# maxlen = max(maxlen, pathlen[depth] + len(name))
# else:
# pathlen[depth + 1] = pathlen[depth] + len(name) + 1
# print("maxlen:", maxlen)
# return maxlen
# def lengthLongestPath(input):
# paths = input.split("\n")
# level = [0] * 10
# maxLength = 0
# for path in paths:
# print("-------------")
# levelIdx = path.rfind("\t")
# print("Path: ", path)
# print("path.rfind(\\t)", path.rfind("\t"))
# print("levelIdx: ", levelIdx)
# print("level: ", level)
# level[levelIdx + 1] = level[levelIdx] + len(path) - levelIdx + 1
# print("level: ", level)
# if "." in path:
# maxLength = max(maxLength, level[levelIdx+1] - 1)
# print("maxlen: ", maxLength)
# return maxLength
def
length_longest_path
(
input
):
"""
:type input: str
:rtype: int
"""
curr_len
,
max_len
=
0
,
0
# running length and max length
stack
=
[]
# keep track of the name length
for
s
in
input
.
split
(
'
\n
'
):
print
(
"---------"
)
print
(
"<path>:"
,
s
)
depth
=
s
.
count
(
'
\t
'
)
# the depth of current dir or file
print
(
"depth: "
,
depth
)
print
(
"stack: "
,
stack
)
print
(
"curlen: "
,
curr_len
)
while
len
(
stack
)
>
depth
:
# go back to the correct depth
curr_len
-=
stack
.
pop
()
stack
.
append
(
len
(
s
.
strip
(
'
\t
'
))
+
1
)
# 1 is the length of '/'
curr_len
+=
stack
[
-
1
]
# increase current length
print
(
"stack: "
,
stack
)
print
(
"curlen: "
,
curr_len
)
if
'.'
in
s
:
# update maxlen only when it is a file
max_len
=
max
(
max_len
,
curr_len
-
1
)
# -1 is to minus one '/'
return
max_len
st
=
"dir
\n
\t
subdir1
\n
\t
\t
file1.ext
\n
\t
\t
subsubdirectory1
\n
\t
subdir2
\n
\t
\t
subsubdir2
\n
\t
\t
\t
file2.ext"
st2
=
"a
\n
\t
b1
\n
\t
\t
f1.txt
\n
\t
aaaaa
\n
\t
\t
f2.txt"
print
(
"path:"
,
st2
)
print
(
"answer:"
,
length_longest_path
(
st2
))
Back
|
FazBrowse Home
|
New Git URL