-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIn-Memory File System
78 lines (59 loc) · 1.78 KB
/
In-Memory File System
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
66
67
68
69
70
71
72
73
74
75
76
77
78
class TrieNode:
def __init__(self):
self.next = {}
self.word = ''
class FileSystem(object):
def __init__(self):
self.root = TrieNode()
def insert(self, path):
trie_crawl = self.root
for i in path.split('/')[1:]:
if not i in trie_crawl.next:
trie_crawl.next[i] = TrieNode()
trie_crawl = trie_crawl.next[i]
return trie_crawl
def search(self, path):
trie_crawl = self.root
for i in path.split('/')[1:]:
if not i in trie_crawl.next:
return trie_crawl
trie_crawl = trie_crawl.next[i]
return trie_crawl
def ls(self, path):
"""
:type path: str
:rtype: List[str]
"""
temp_list = [path.split('/')[-1]]
trie_c = self.search(path)
if trie_c.word:
return temp_list
temp_list = sorted(trie_c.next.keys())
return temp_list
def mkdir(self, path):
"""
:type path: str
:rtype: None
"""
self.insert(path)
def addContentToFile(self, filePath, content):
"""
:type filePath: str
:type content: str
:rtype: None
"""
trie_c = self.insert(filePath)
trie_c.word += content
def readContentFromFile(self, filePath):
"""
:type filePath: str
:rtype: str
"""
trie_c = self.search(filePath)
return trie_c.word
# Your FileSystem object will be instantiated and called as such:
# obj = FileSystem()
# param_1 = obj.ls(path)
# obj.mkdir(path)
# obj.addContentToFile(filePath,content)
# param_4 = obj.readContentFromFile(filePath)