|
| 1 | +class FileSystem: |
| 2 | + |
| 3 | + def __init__(self): |
| 4 | + self.root = (None, {}) |
| 5 | + |
| 6 | + def createPath(self, path: str, value: int) -> bool: |
| 7 | + path = [parent for parent in path.split("/") if parent != ""] |
| 8 | + val, children = self.root |
| 9 | + for parent in path[:-1]: |
| 10 | + if parent not in children: |
| 11 | + return False |
| 12 | + val, children = children[parent] |
| 13 | + if path[-1] in children: |
| 14 | + return False |
| 15 | + else: |
| 16 | + children[path[-1]] = (value, {}) |
| 17 | + return True |
| 18 | + |
| 19 | + def get(self, path: str) -> int: |
| 20 | + val, children = self.root |
| 21 | + path = [parent for parent in path.split("/") if parent != ""] |
| 22 | + for parent in path: |
| 23 | + if parent not in children: |
| 24 | + return -1 |
| 25 | + val, children = children[parent] |
| 26 | + return val |
| 27 | + |
| 28 | + |
| 29 | +# Your FileSystem object will be instantiated and called as such: |
| 30 | +fs = FileSystem() |
| 31 | +creates = [ |
| 32 | + ["/leet", 1, True], |
| 33 | + ["/leet/code", 2, True], |
| 34 | + ["/derp/herp", 4, False], |
| 35 | + ["/leet/code", 4, False], |
| 36 | +] |
| 37 | +for path, val, expected in creates: |
| 38 | + fs.createPath(path, val) |
| 39 | +gets = [ |
| 40 | + ["/leet", 1], |
| 41 | + ["/leet/code", 2], |
| 42 | + ["/derp/herp", -1], |
| 43 | +] |
| 44 | +for path, expected in gets: |
| 45 | + actual = fs.get(path) |
| 46 | + assert actual == expected, f"get {path}: {expected} != {actual}" |
| 47 | +# param_1 = obj.createPath(path,value) |
| 48 | +# param_2 = obj.get(path) |
0 commit comments