mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-24 12:10:16 +00:00
7f04e5cd34
* spelling corrections * review * improved documentation, removed redundant variables, added testing * added type hint * camel case to snake case * spelling fix * review * python --> Python # it is a brand name, not a snake * explicit cast to int * spaces in int list * "!= None" to "is not None" * Update comb_sort.py * various spelling corrections in documentation & several variables naming conventions fix * + char in file name * import dependency - bug fix Co-authored-by: John Law <johnlaw.po@gmail.com>
65 lines
1.3 KiB
Python
65 lines
1.3 KiB
Python
END = "#"
|
|
|
|
|
|
class Trie:
|
|
def __init__(self):
|
|
self._trie = {}
|
|
|
|
def insert_word(self, text):
|
|
trie = self._trie
|
|
for char in text:
|
|
if char not in trie:
|
|
trie[char] = {}
|
|
trie = trie[char]
|
|
trie[END] = True
|
|
|
|
def find_word(self, prefix):
|
|
trie = self._trie
|
|
for char in prefix:
|
|
if char in trie:
|
|
trie = trie[char]
|
|
else:
|
|
return []
|
|
return self._elements(trie)
|
|
|
|
def _elements(self, d):
|
|
result = []
|
|
for c, v in d.items():
|
|
if c == END:
|
|
sub_result = [" "]
|
|
else:
|
|
sub_result = [c + s for s in self._elements(v)]
|
|
result.extend(sub_result)
|
|
return tuple(result)
|
|
|
|
|
|
trie = Trie()
|
|
words = ("depart", "detergent", "daring", "dog", "deer", "deal")
|
|
for word in words:
|
|
trie.insert_word(word)
|
|
|
|
|
|
def autocomplete_using_trie(s):
|
|
"""
|
|
>>> trie = Trie()
|
|
>>> for word in words:
|
|
... trie.insert_word(word)
|
|
...
|
|
>>> matches = autocomplete_using_trie("de")
|
|
|
|
"detergent " in matches
|
|
True
|
|
"dog " in matches
|
|
False
|
|
"""
|
|
suffixes = trie.find_word(s)
|
|
return tuple(s + w for w in suffixes)
|
|
|
|
|
|
def main():
|
|
print(autocomplete_using_trie("de"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|