Embercode
✕
TriesQuick quiz

Wildcard search

7 questions. Covers Wildcard search with dots in Python.

Questions 1 to 5 of 7

  1. 1. In the wildcard search lesson, what does a dot . in a search pattern match?
  2. 2. The dictionary holds bad, dad and mad. search for .ad returns True. Why does it never try the d or m child at the root?
  3. 3. What does this program print? ``python class Node: def __init__(self): self.children = [None] * 26 self.is_end = False class WordDictionary: def __init__(self): self.root = Node() def add_word(self, word): node = self.root for ch in word: i = ord(ch) - ord("a") if node.children[i] is None: node.children[i] = Node() node = node.children[i] node.is_end = True def search(self, pattern): return self._search(self.root, pattern, 0) def _search(self, node, pattern, i): if i == len(pattern): return node.is_end ch = pattern[i] if ch == ".": return any( self._search(child, pattern, i + 1) for child in node.children if child is not None ) child = node.children[ord(ch) - ord("a")] return child is not None and self._search(child, pattern, i + 1) def find_all(self, pattern): found = [] self._collect(self.root, pattern, 0, [], found) return found def _collect(self, node, pattern, i, path, found): if i == len(pattern): if node.is_end: found.append("".join(path)) return ch = pattern[i] if ch == ".": for k, child in enumerate(node.children): if child is not None: path.append(chr(ord("a") + k)) self._collect(child, pattern, i + 1, path, found) path.pop() else: child = node.children[ord(ch) - ord("a")] if child is not None: path.append(ch) self._collect(child, pattern, i + 1, path, found) path.pop() d = WordDictionary() for w in ["cot", "cut", "cat", "dog", "cart"]: d.add_word(w) print(d.find_all("c.t"), d.find_all(".o.")) ``
  4. 4. What does this program print? ``python class Node: def __init__(self): self.children = [None] * 26 self.is_end = False class WordDictionary: def __init__(self): self.root = Node() def add_word(self, word): node = self.root for ch in word: i = ord(ch) - ord("a") if node.children[i] is None: node.children[i] = Node() node = node.children[i] node.is_end = True def search(self, pattern): return self._search(self.root, pattern, 0) def _search(self, node, pattern, i): if i == len(pattern): return node.is_end ch = pattern[i] if ch == ".": return any( self._search(child, pattern, i + 1) for child in node.children if child is not None ) child = node.children[ord(ch) - ord("a")] return child is not None and self._search(child, pattern, i + 1) def find_all(self, pattern): found = [] self._collect(self.root, pattern, 0, [], found) return found def _collect(self, node, pattern, i, path, found): if i == len(pattern): if node.is_end: found.append("".join(path)) return ch = pattern[i] if ch == ".": for k, child in enumerate(node.children): if child is not None: path.append(chr(ord("a") + k)) self._collect(child, pattern, i + 1, path, found) path.pop() else: child = node.children[ord(ch) - ord("a")] if child is not None: path.append(ch) self._collect(child, pattern, i + 1, path, found) path.pop() d = WordDictionary() for w in ["cat", "cart"]: d.add_word(w) print(" ".join(f"{p}={d.search(p)}" for p in ["c...", "..r", "c..", "...."])) ``
  5. 5. The search below crashes on the pattern b.. Which line causes the crash? ``python def search(node, pattern, i): if i == len(pattern): return node.is_end ch = pattern[i] child = node.children[ord(ch) - ord("a")] return child is not None and search(child, pattern, i + 1) ``

5 question(s) left on this page.