-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
54 lines (43 loc) · 1.54 KB
/
solution.py
File metadata and controls
54 lines (43 loc) · 1.54 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
from typing import List
class Node:
def __init__(self):
self.content = ""
self.children = dict()
class FileSystem:
def __init__(self):
self.root = Node()
def ls(self, filePath: str) -> List[str]:
node = self.root
paths = [path for path in filePath.split("/") if path]
print(paths)
for path in paths:
if path not in node.children:
node.children[path] = Node()
node = node.children[path]
if node.content:
return [paths[-1]]
else:
return sorted(node.children.keys())
def mkdir(self, filePath: str) -> None:
node = self.root
paths = [path for path in filePath.split("/") if path]
for path in paths:
if path not in node.children:
node.children[path] = Node()
node = node.children[path]
def addContentToFile(self, filePath: str, content: str) -> None:
node = self.root
paths = [path for path in filePath.split("/") if path]
for path in paths:
if path not in node.children:
node.children[path] = Node()
node = node.children[path]
node.content += content
def readContentFromFile(self, filePath: str) -> str:
node = self.root
paths = [path for path in filePath.split("/") if path]
for path in paths:
if path not in node.children:
node.children[path] = Node()
node = node.children[path]
return node.content