Add missing type annotations for strings directory (#5817)

* Type annotations for `strings/autocomplete_using_trie.py`

* Update autocomplete_using_trie.py

* Update detecting_english_programmatically.py

* Update detecting_english_programmatically.py

* Update frequency_finder.py

* Update frequency_finder.py

* Update frequency_finder.py

* Update word_occurrence.py

* Update frequency_finder.py

* Update z_function.py

* Update z_function.py

* Update frequency_finder.py
This commit is contained in:
Rohan R Bharadwaj
2022-05-13 11:25:53 +05:30
committed by GitHub
parent bbb88bb5c2
commit e95ecfaf27
5 changed files with 82 additions and 102 deletions

View File

@@ -1,11 +1,13 @@
from __future__ import annotations
END = "#"
class Trie:
def __init__(self):
self._trie = {}
def __init__(self) -> None:
self._trie: dict = {}
def insert_word(self, text):
def insert_word(self, text: str) -> None:
trie = self._trie
for char in text:
if char not in trie:
@@ -13,7 +15,7 @@ class Trie:
trie = trie[char]
trie[END] = True
def find_word(self, prefix):
def find_word(self, prefix: str) -> tuple | list:
trie = self._trie
for char in prefix:
if char in trie:
@@ -22,7 +24,7 @@ class Trie:
return []
return self._elements(trie)
def _elements(self, d):
def _elements(self, d: dict) -> tuple:
result = []
for c, v in d.items():
if c == END:
@@ -39,26 +41,28 @@ for word in words:
trie.insert_word(word)
def autocomplete_using_trie(s):
def autocomplete_using_trie(string: str) -> tuple:
"""
>>> trie = Trie()
>>> for word in words:
... trie.insert_word(word)
...
>>> matches = autocomplete_using_trie("de")
"detergent " in matches
>>> "detergent " in matches
True
"dog " in matches
>>> "dog " in matches
False
"""
suffixes = trie.find_word(s)
return tuple(s + w for w in suffixes)
suffixes = trie.find_word(string)
return tuple(string + word for word in suffixes)
def main():
def main() -> None:
print(autocomplete_using_trie("de"))
if __name__ == "__main__":
import doctest
doctest.testmod()
main()