From e7a552d17492fd6db3bda596840ddf17c62f5758 Mon Sep 17 00:00:00 2001 From: Archana Prabhu Date: Sun, 20 Oct 2019 09:22:51 +0530 Subject: [PATCH 1/5] Create autocomplete_using_trie.py The program aims to design a trie implementation for autocomplete which is easy to understand and ready to run. --- other/autocomplete_using_trie.py | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 other/autocomplete_using_trie.py diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py new file mode 100644 index 000000000000..f02f543b3b2e --- /dev/null +++ b/other/autocomplete_using_trie.py @@ -0,0 +1,47 @@ +from sys import stdin, stdout +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: + subresult = [' '] + else: + subresult = [c + s for s in self._elements(v)] + result.extend(subresult) + return result + +trie = Trie() +words = ['depart', 'detergent', 'daring', 'dog', 'deer', 'deal', ] +for word in words: + trie.insert_word(word) + +def autocomplete_using_trie(s): + suffixes = trie.find_word(s) + return [s + w for w in suffixes] + +def main(): + print(autocomplete_using_trie('de')) + +if __name__ == "__main__": + main() From 00fb869fde3bce81034db6ee3a4f1823f805e9c2 Mon Sep 17 00:00:00 2001 From: Archana Prabhu Date: Sun, 20 Oct 2019 09:45:46 +0530 Subject: [PATCH 2/5] Removed unused import --- other/autocomplete_using_trie.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py index f02f543b3b2e..222cb0eef422 100644 --- a/other/autocomplete_using_trie.py +++ b/other/autocomplete_using_trie.py @@ -1,4 +1,4 @@ -from sys import stdin, stdout +from sys import stdout END = '#' class Trie: def __init__(self): From 3d78ce783170c72f61d90e99a47f4f70bc210b54 Mon Sep 17 00:00:00 2001 From: Archana Prabhu Date: Sun, 20 Oct 2019 13:20:18 +0530 Subject: [PATCH 3/5] Updated the list value --- other/autocomplete_using_trie.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py index 222cb0eef422..618368a1c285 100644 --- a/other/autocomplete_using_trie.py +++ b/other/autocomplete_using_trie.py @@ -32,7 +32,7 @@ def _elements(self, d): return result trie = Trie() -words = ['depart', 'detergent', 'daring', 'dog', 'deer', 'deal', ] +words = ['depart', 'detergent', 'daring', 'dog', 'deer', 'deal'] for word in words: trie.insert_word(word) From fd42b791bda5ec9873541bcbb43b3bb514693a6a Mon Sep 17 00:00:00 2001 From: Archana Prabhu Date: Sun, 20 Oct 2019 13:26:39 +0530 Subject: [PATCH 4/5] Update autocomplete_using_trie.py --- other/autocomplete_using_trie.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py index 618368a1c285..58ba7cca8fe1 100644 --- a/other/autocomplete_using_trie.py +++ b/other/autocomplete_using_trie.py @@ -1,4 +1,3 @@ -from sys import stdout END = '#' class Trie: def __init__(self): From d5c6c693b01d012fbb59a7032d947355afa4e9be Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 20 Oct 2019 10:36:27 +0200 Subject: [PATCH 5/5] Run the code through Black and add doctest --- other/autocomplete_using_trie.py | 36 ++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/other/autocomplete_using_trie.py b/other/autocomplete_using_trie.py index 58ba7cca8fe1..eb906f8efa9a 100644 --- a/other/autocomplete_using_trie.py +++ b/other/autocomplete_using_trie.py @@ -1,8 +1,10 @@ -END = '#' +END = "#" + + class Trie: def __init__(self): self._trie = {} - + def insert_word(self, text): trie = self._trie for char in text: @@ -10,37 +12,53 @@ def insert_word(self, text): 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 [] return self._elements(trie) def _elements(self, d): result = [] for c, v in d.items(): if c == END: - subresult = [' '] + subresult = [" "] else: subresult = [c + s for s in self._elements(v)] result.extend(subresult) - return result + return tuple(result) + trie = Trie() -words = ['depart', 'detergent', 'daring', 'dog', 'deer', 'deal'] +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 [s + w for w in suffixes] + return tuple(s + w for w in suffixes) + def main(): - print(autocomplete_using_trie('de')) + print(autocomplete_using_trie("de")) + if __name__ == "__main__": main()