python - Matching incompletely qualified paths -
let's have series of incompletely qualified paths, missing parts, guaranteed have 2 properties:
- the final part of both incompletely , qualified paths equal, and
- the order of each part of incompletely qualified path match actual order of parts of qualified path.
for example,
p1 = '/foo/baz/myfile.txt' p2 = '/bar/foo/myfile.txt' actual = '/foo/bar/baz/myfile.txt'
in case, p1
match, p2
not, because in actual path, bar
occurs after foo
. easy enough: [actual.split('/').index(part) part in p1.split('/')]
ordered list, same comprehension p2
not.
but happens if there repetitions in path?
p1 = '/foo/bar/bar/myfile.txt' p2 = '/bar/bar/baz/myfile.txt' actual = '/foo/bar/baz/bar/myfile.txt'
how can identify p1
match, p2
not (because, although baz
occurs after first bar
, not occur after second?
def match(path, actual): path = path.strip('/').split('/') actual = iter(actual.strip('/').split('/')) pathitem in path: item in actual: if pathitem == item: break else: # for-loop never breaked, pathitem never found return false return true q1 = '/foo/baz/myfile.txt' q2 = '/bar/foo/myfile.txt' p1 = '/foo/bar/bar/myfile.txt' p2 = '/bar/bar/baz/myfile.txt' actual = '/foo/bar/baz/bar/myfile.txt' print(match(q1, actual)) # true print(match(q2, actual)) # false print(match(p1, actual)) # true print(match(p2, actual)) # false
Comments
Post a Comment