mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-19 04:07:36 +00:00
[mypy] Fix type annotations for trie.py (#5022)
* Fix type annotations for trie.py * Add strict type annotations to trie.py Annotate return type for all functions and type for "nodes" * updating DIRECTORY.md * Format trie.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
07141e4bcc
commit
11ec2fd3fb
@ -7,11 +7,11 @@ longest word)) lookup time making it an optimal approach when space is not an is
|
|||||||
|
|
||||||
|
|
||||||
class TrieNode:
|
class TrieNode:
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.nodes = dict() # Mapping from char to TrieNode
|
self.nodes: dict[str, TrieNode] = dict() # Mapping from char to TrieNode
|
||||||
self.is_leaf = False
|
self.is_leaf = False
|
||||||
|
|
||||||
def insert_many(self, words: list[str]):
|
def insert_many(self, words: list[str]) -> None:
|
||||||
"""
|
"""
|
||||||
Inserts a list of words into the Trie
|
Inserts a list of words into the Trie
|
||||||
:param words: list of string words
|
:param words: list of string words
|
||||||
@ -20,7 +20,7 @@ class TrieNode:
|
|||||||
for word in words:
|
for word in words:
|
||||||
self.insert(word)
|
self.insert(word)
|
||||||
|
|
||||||
def insert(self, word: str):
|
def insert(self, word: str) -> None:
|
||||||
"""
|
"""
|
||||||
Inserts a word into the Trie
|
Inserts a word into the Trie
|
||||||
:param word: word to be inserted
|
:param word: word to be inserted
|
||||||
@ -46,14 +46,14 @@ class TrieNode:
|
|||||||
curr = curr.nodes[char]
|
curr = curr.nodes[char]
|
||||||
return curr.is_leaf
|
return curr.is_leaf
|
||||||
|
|
||||||
def delete(self, word: str):
|
def delete(self, word: str) -> None:
|
||||||
"""
|
"""
|
||||||
Deletes a word in a Trie
|
Deletes a word in a Trie
|
||||||
:param word: word to delete
|
:param word: word to delete
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _delete(curr: TrieNode, word: str, index: int):
|
def _delete(curr: TrieNode, word: str, index: int) -> bool:
|
||||||
if index == len(word):
|
if index == len(word):
|
||||||
# If word does not exist
|
# If word does not exist
|
||||||
if not curr.is_leaf:
|
if not curr.is_leaf:
|
||||||
@ -75,7 +75,7 @@ class TrieNode:
|
|||||||
_delete(self, word, 0)
|
_delete(self, word, 0)
|
||||||
|
|
||||||
|
|
||||||
def print_words(node: TrieNode, word: str):
|
def print_words(node: TrieNode, word: str) -> None:
|
||||||
"""
|
"""
|
||||||
Prints all the words in a Trie
|
Prints all the words in a Trie
|
||||||
:param node: root node of Trie
|
:param node: root node of Trie
|
||||||
@ -89,7 +89,7 @@ def print_words(node: TrieNode, word: str):
|
|||||||
print_words(value, word + key)
|
print_words(value, word + key)
|
||||||
|
|
||||||
|
|
||||||
def test_trie():
|
def test_trie() -> bool:
|
||||||
words = "banana bananas bandana band apple all beast".split()
|
words = "banana bananas bandana band apple all beast".split()
|
||||||
root = TrieNode()
|
root = TrieNode()
|
||||||
root.insert_many(words)
|
root.insert_many(words)
|
||||||
@ -112,11 +112,11 @@ def print_results(msg: str, passes: bool) -> None:
|
|||||||
print(str(msg), "works!" if passes else "doesn't work :(")
|
print(str(msg), "works!" if passes else "doesn't work :(")
|
||||||
|
|
||||||
|
|
||||||
def pytests():
|
def pytests() -> None:
|
||||||
assert test_trie()
|
assert test_trie()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
"""
|
"""
|
||||||
>>> pytests()
|
>>> pytests()
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user