Skip to content

[mypy] Fix type annotations for trie.py #5022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions data_structures/trie/trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@


class TrieNode:
def __init__(self):
self.nodes = dict() # Mapping from char to TrieNode
def __init__(self) -> None:
self.nodes: dict[str, TrieNode] = dict() # Mapping from char to TrieNode
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
:param words: list of string words
Expand All @@ -20,7 +20,7 @@ def insert_many(self, words: list[str]):
for word in words:
self.insert(word)

def insert(self, word: str):
def insert(self, word: str) -> None:
"""
Inserts a word into the Trie
:param word: word to be inserted
Expand All @@ -46,14 +46,14 @@ def find(self, word: str) -> bool:
curr = curr.nodes[char]
return curr.is_leaf

def delete(self, word: str):
def delete(self, word: str) -> None:
"""
Deletes a word in a Trie
:param word: word to delete
:return: None
"""

def _delete(curr: TrieNode, word: str, index: int):
def _delete(curr: TrieNode, word: str, index: int) -> bool:
if index == len(word):
# If word does not exist
if not curr.is_leaf:
Expand All @@ -75,7 +75,7 @@ def _delete(curr: TrieNode, word: str, index: int):
_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
:param node: root node of Trie
Expand All @@ -89,7 +89,7 @@ def print_words(node: TrieNode, word: str):
print_words(value, word + key)


def test_trie():
def test_trie() -> bool:
words = "banana bananas bandana band apple all beast".split()
root = TrieNode()
root.insert_many(words)
Expand All @@ -112,11 +112,11 @@ def print_results(msg: str, passes: bool) -> None:
print(str(msg), "works!" if passes else "doesn't work :(")


def pytests():
def pytests() -> None:
assert test_trie()


def main():
def main() -> None:
"""
>>> pytests()
"""
Expand Down