MCPcopy Create free account
hub / github.com/geekcomputers/Python / Dictionary

Class Dictionary

Word_Dictionary/dictionary.py:4–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2
3
4class Dictionary:
5 def __init__(self):
6 self.node = {}
7
8 def add_word(self, word: str) -> None:
9 node = self.node
10 for ltr in word:
11 if ltr not in node:
12 node[ltr] = {}
13 node = node[ltr]
14 node["is_word"] = True
15
16 def word_exists(self, word: str) -> bool:
17 node = self.node
18 for ltr in word:
19 if ltr not in node:
20 return False
21 node = node[ltr]
22 return "is_word" in node
23
24 def list_words_from_node(self, node: Dict, spelling: str) -> None:
25 if "is_word" in node:
26 self.words_list.append(spelling)
27 return
28 for ltr in node:
29 self.list_words_from_node(node[ltr], spelling + ltr)
30
31 def print_all_words_in_dictionary(self) -> List[str]:
32 node = self.node
33 self.words_list = []
34 self.list_words_from_node(node, "")
35 return self.words_list
36
37 def suggest_words_starting_with(self, prefix: str) -> List[str]:
38 node = self.node
39 for ltr in prefix:
40 if ltr not in node:
41 return False
42 node = node[ltr]
43 self.words_list = []
44 self.list_words_from_node(node, prefix)
45 return self.words_list
46
47
48# Your Dictionary object will be instantiated and called as such:

Callers 1

dictionary.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected