def test_trie_char_to_index(): """ Test functionalities of Trie data structure """ assert Trie.char_to_index('a') == 0 assert Trie.char_to_index('d') == 3 assert Trie.char_to_index('z') == 25
def test_trie_search2(): """ test search conditions """ trie_ds = Trie() insert_element = "zazzfgdi" trie_ds.insert(insert_element) assert trie_ds.search(insert_element) is True
def test_trie_insert2(): """ test insert conditions """ trie_ds = Trie() insert_element = "z" trie_ds.insert(insert_element) assert trie_ds.root.children[Trie.char_to_index( insert_element)] is not None
def test_trie_searh_multiword4(): """ test search conditions for multiwords: general case """ trie_ds = Trie() insert_element1 = "apple" insert_element2 = "zelle" trie_ds.insert(insert_element1) trie_ds.insert(insert_element2) assert trie_ds.search_multi_words(insert_element1 + insert_element2) is True
def test_trie_searh_multiword3(): """ test search conditions for multiwords: what if string is empty """ trie_ds = Trie() insert_element1 = "" insert_element2 = "z" trie_ds.insert(insert_element1) trie_ds.insert(insert_element2) assert trie_ds.search_multi_words(insert_element1 + insert_element2) is True
def test_trie_searh_multiword1(): """ test search conditions for multiwords """ trie_ds = Trie() insert_element1 = "a" insert_element2 = "z" trie_ds.insert(insert_element1) trie_ds.insert(insert_element2) assert trie_ds.search_multi_words(insert_element1 + insert_element2) is True
def test_trie_get_new_node(): """ Test functionalities of Trie data structure """ node = Trie.get_new_node() assert node.is_end_of_the_word is False assert None in node.children
class Dictionary: """ Dictionary DS Mehods: len(object) add_single_word add_to_dictionary_from_txt search search_multi_words """ def __init__(self): self.data = Trie() self.total_count = 0 def __len__(self): """ Return the length of Dictionary """ return self.total_count def add_to_dictionary_from_txt(self, dict_name): """ Import information from txt file which contains words in each line """ with open(dict_name) as file: for line in file: if len(line) > 1: self.data.insert(line.split()[0]) self.total_count = self.total_count + 1 def add_single_word(self, word_to_add): """ Insert one word into dictionary """ self.data.insert(word_to_add) self.total_count = self.total_count + 1 def search(self, word_to_search): """ Search a word in dictionary """ return self.data.search(word_to_search) def search_multi_words(self, multi_words): """ Search whether all words containing in string is present """ return self.data.search_multi_words(multi_words)
def __init__(self): self.data = Trie() self.total_count = 0
def test_trie_search4(): """ test search conditions """ trie_ds = Trie() assert trie_ds.search("jfdhjekjebt") is False
def test_trie_search3(): """ test search conditions """ trie_ds = Trie() assert trie_ds.search("random") is False