|
1 | 1 | from test_framework import generic_test
|
2 | 2 |
|
3 | 3 |
|
| 4 | +""" |
| 5 | +Allowed chars: |
| 6 | +- . (current path) |
| 7 | +- .. (parent dir) |
| 8 | +- / (subdir) |
| 9 | +- alphanum (dir name) |
| 10 | +- empty (same as .) |
| 11 | +
|
| 12 | +cases: |
| 13 | + - empty |
| 14 | + - /////// -> / |
| 15 | + - / -> / |
| 16 | + - /../.. -> / |
| 17 | + - ../.. -> ../.. |
| 18 | + - derp -> derp |
| 19 | + - /derp -> /derp |
| 20 | + - /derp/.. -> / |
| 21 | + - derp/.. -> empty? |
| 22 | + - derp/../.. -> .. |
| 23 | +
|
| 24 | +- Paths can be absolute (begin at '/') or relative (begin at current dir) |
| 25 | +- for abspaths, / is highest parent; any '..' from '/' yields '/' |
| 26 | +- for relpaths, each '..' either pops the last known dir, or appends '..' |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +def shortest_equivalent_path_first(path: str) -> str: |
| 31 | + """ |
| 32 | + - is absolute? (first char) |
| 33 | + - split path on '/' |
| 34 | + - keep stack |
| 35 | + - for each item in path list: |
| 36 | + - if char is ., skip it |
| 37 | + - if item is alphanum, push it |
| 38 | + - if item is ..: |
| 39 | + - if dir is absolute, pop if stack is nonempty, else skip |
| 40 | + - if dir is rel, pop if stack is nonempty and stack top not .., else push |
| 41 | + - return joined stack; prepend '/' is absolute path |
| 42 | + """ |
| 43 | + if not path: |
| 44 | + return path |
| 45 | + absolute = path[0] == '/' |
| 46 | + |
| 47 | + stack = [] |
| 48 | + dirs = [item for item in path.split('/') if item] |
| 49 | + for item in dirs: |
| 50 | + if item == "..": |
| 51 | + if absolute and stack: |
| 52 | + stack.pop() |
| 53 | + elif not absolute and (not stack or stack[-1] == ".."): |
| 54 | + stack.append(item) |
| 55 | + elif not absolute and stack and stack[-1] != "..": |
| 56 | + stack.pop() |
| 57 | + elif item == ".": |
| 58 | + continue |
| 59 | + else: |
| 60 | + stack.append(item) |
| 61 | + |
| 62 | + return ("/" if absolute else "") + "/".join(stack) |
| 63 | + |
| 64 | + |
4 | 65 | def shortest_equivalent_path(path: str) -> str:
|
5 |
| - # TODO - you fill in here. |
6 |
| - return '' |
| 66 | + if not path: |
| 67 | + return path |
| 68 | + |
| 69 | + stack = [] |
| 70 | + for item in (item for item in path.split('/') if (item and item != ".")): |
| 71 | + if item == "..": |
| 72 | + if stack and (path[0] == '/' or stack[-1] != ".."): |
| 73 | + stack.pop() |
| 74 | + else: |
| 75 | + stack.append(item) |
| 76 | + else: |
| 77 | + stack.append(item) |
| 78 | + |
| 79 | + return ("/" if path.startswith('/') else "") + "/".join(stack) |
7 | 80 |
|
8 | 81 |
|
9 | 82 | if __name__ == '__main__':
|
| 83 | + cases = [ |
| 84 | + ("", ""), |
| 85 | + ("///////", "/"), |
| 86 | + ("/", "/"), |
| 87 | + ("/../..", "/"), |
| 88 | + ("../..", "../.."), |
| 89 | + ("derp", "derp"), |
| 90 | + ("/derp", "/derp"), |
| 91 | + ("/derp/..", "/"), |
| 92 | + ("derp/..", ""), |
| 93 | + ("derp/../..", "..") |
| 94 | + ] |
| 95 | + for path, expected in cases: |
| 96 | + actual = shortest_equivalent_path(path) |
| 97 | + assert actual == expected, f"{path}: {actual} != {expected}" |
| 98 | + |
10 | 99 | exit(
|
11 | 100 | generic_test.generic_test_main('directory_path_normalization.py',
|
12 | 101 | 'directory_path_normalization.tsv',
|
|
0 commit comments