|
| 1 | +# A Program to Implement a word search with wildcards |
| 2 | +# This version uses a single Trie structure to search for words |
| 3 | + |
| 4 | +# Node Class |
| 5 | +# value - each node has a value which is a single character |
| 6 | +# children - each node has a dictionary of child nodes. the key is the |
| 7 | +# character represented by the child node |
| 8 | +# is_word - a boolean value indicating if this node is the end of |
| 9 | +# a "word path". |
| 10 | +class Node: |
| 11 | + def __init__(self, value, is_word): |
| 12 | + self.value = value |
| 13 | + self.children = {} |
| 14 | + self.is_word = is_word |
| 15 | + |
| 16 | +def add_to_trie(word, root): |
| 17 | + # recursion base case |
| 18 | + if len(word) == 0: |
| 19 | + root.is_word = True |
| 20 | + return |
| 21 | + |
| 22 | + # split off the first character of the current word |
| 23 | + first_char = word[0:1] |
| 24 | + sufx = word[1:] |
| 25 | + |
| 26 | + # recursive step |
| 27 | + if first_char in root.children: |
| 28 | + add_to_trie(sufx, root.children[first_char]) |
| 29 | + else: |
| 30 | + new_node = Node(first_char, False) |
| 31 | + root.children[first_char] = new_node |
| 32 | + add_to_trie(sufx, new_node) |
| 33 | + |
| 34 | +def setup(words, root): |
| 35 | + for w in words: |
| 36 | + add_to_trie(w.strip(), root) |
| 37 | + |
| 38 | +# convenience function to initiate recursion |
| 39 | +def find_words(word, node, num_visits): |
| 40 | + fwords = list() |
| 41 | + return find_words_r(word, node, fwords, num_visits) |
| 42 | + |
| 43 | +# recursive search |
| 44 | +def find_words_r(word, node, fwords, num_visits, path=""): |
| 45 | + num_visits[0] +=1 |
| 46 | + |
| 47 | + # recursion base case |
| 48 | + if len(word) == 0: |
| 49 | + if node.is_word: |
| 50 | + fwords.append(path) |
| 51 | + return fwords |
| 52 | + |
| 53 | + # split the first character off the curent word |
| 54 | + first_char = word[0:1] |
| 55 | + sufx = word[1:] |
| 56 | + |
| 57 | + # recursive step |
| 58 | + if first_char in node.children: |
| 59 | + return find_words_r(sufx, node.children[first_char], fwords, num_visits, path + first_char) |
| 60 | + elif first_char == '.': |
| 61 | + for k in node.children.keys(): |
| 62 | + find_words_r(sufx, node.children[k], fwords, num_visits, path + k) |
| 63 | + return fwords |
| 64 | + else: |
| 65 | + return fwords |
| 66 | + |
| 67 | + |
| 68 | +# Main Program |
| 69 | +words = list() |
| 70 | +root = Node("", False) |
| 71 | + |
| 72 | +with open("16-words_alpha.txt") as f: |
| 73 | + |
| 74 | + # Read the dictionary file into a list |
| 75 | + words = list(f) |
| 76 | + setup(words, root) |
| 77 | + print("Loaded {} words.".format(len(words))) |
| 78 | + |
| 79 | + # Do the search |
| 80 | + num_visits = [0] |
| 81 | + fwords = find_words(".one", root, num_visits) |
| 82 | + |
| 83 | + # Print the results |
| 84 | + print("Found words: {}. Visited: {}".format(len(fwords), num_visits[0])) |
| 85 | + print(fwords) |
| 86 | + |
| 87 | + |
| 88 | + # WORDLE SOLVER |
| 89 | + # The next lines are extra work to further filter the list of found words |
| 90 | + # by excluding words with certain characters or requiring that the words |
| 91 | + # contain certain characters. There is also an option to prohibit characters |
| 92 | + # in specific positions which is used to override the list of included characters |
| 93 | + # by specifying that they cannot exist in certain positions. |
| 94 | + exclude = ['i', 'l', 'c', 'r'] |
| 95 | + include = ['d', 'e', 'a'] |
| 96 | + anti_positions = {'1':'d', '2':'e'} |
| 97 | + |
| 98 | + candidates = [] |
| 99 | + for w in fwords: |
| 100 | + candidate = True |
| 101 | + for c in exclude: |
| 102 | + if c in w: |
| 103 | + candidate = False |
| 104 | + for c in include: |
| 105 | + if c not in w: |
| 106 | + candidate = False |
| 107 | + for k, v in anti_positions.items(): |
| 108 | + if w[int(k)] == v: |
| 109 | + candidate = False |
| 110 | + if candidate: |
| 111 | + candidates.append(w) |
| 112 | + |
| 113 | + # print("Found words: {}. Visited: {}".format(len(candidates), num_visits[0])) |
| 114 | + # print(candidates) |
| 115 | + |
| 116 | + |
0 commit comments