import termios import fcntl import string import trie_shell def create_trie_node(): return [] def add_word(w, t): t.append(w) def is_word(w, t): return w in t def num_completions(p, t): # IMPORTANT: When you write the trie-based # version of this function, do NOT compute # the number of completions simply as # # len(get_completions(p, t)) # # See PA writeup for more details. return len(get_completions(p, t)) def get_completions(p, t): return [w[len(p):] for w in t if w.startswith(p)] if __name__ == "__main__": trie_shell.go("trie_list")
def create_trie_node(): return [] def add_word(w, t): t.append(w) def is_word(w, t): return w in t def num_completions(p, t): # IMPORTANT: When you write the trie-based # version of this function, do NOT compute # the number of completions simply as # # len(get_completions(p, t)) # # See PA writeup for more details. return len(get_completions(p, t)) def get_completions(p, t): return [w[len(p):] for w in t if w.startswith(p)] if __name__ == "__main__": trie_shell.go("trie_list")
if word_length == 1: curr_trie = trie[word] for st in curr_trie: if st != "final" and st != "count": sub_trie = curr_trie[st] if len(list(sub_trie.keys())) == 2: rv.append(st) else: completions = get_completions(st, curr_trie) string = '' for completes in completions: string = st + completes rv.append(string) return rv else: next_trie_step = word[1:] sub_prefix_trie = trie[word[0]] if (len(next_trie_step) == 1 and next_trie_step not in sub_prefix_trie.keys()): return [] return(get_completions(next_trie_step, sub_prefix_trie)) return rv if __name__ == "__main__": trie_shell.go("trie_dict")
''' remaining_trie = find_spot(word, trie) # selects strings that are words and have no other completions if is_word(word, trie) == True and num_completions(word, trie) == 1: return [''] elif not remaining_trie and word != '': return [] else: completion_list = [] if is_word(word, trie): completion_list.append('') if word != '': for k in remaining_trie: x = get_completions(word + k, trie) for j in x: y = k + j completion_list.append(y) # separates the case of an empty string so that we can return # all the words in the trie if word == '': for k in trie: x = get_completions(word + k, trie) for j in x: y = k + j completion_list.append(y) return completion_list if __name__ == "__main__": trie_shell.go("trie_dict")