public static void GetAnagrams() { ITrie allowableCompletions = _words; IList anagramsList = null; StringBuilder sb = new StringBuilder(); if (allowableCompletions.ToString() == "") { if (sb.ToString().Contains("")) { anagramsList.Add(sb.Append("")); } else { sb.Append(" "); GetAnagrams(); sb.Remove(0, 0); } string letters = ""; foreach (char c in letters) { _words.GetCompletions(letters); if (allowableCompletions != null) { sb.Append(c); sb.Remove(0, 0); } } } }
private ITrie FindWords(int row, int col, bool[,] used, StringBuilder prefix, ITrie completions, ITrie found) { completions = completions.GetCompletions(_board[row, col]); if (completions == null) { return(found); } used[row, col] = true; prefix.Append(_board[row, col]); if (completions.Contains("")) { found = found.Add(prefix.ToString()); } int rowStart = Math.Max(0, col - 1); int rowEnd = Math.Min(col + 1, 4); int colStart = Math.Max(0, col - 1); int colEnd = Math.Min(col + 1, 4); for (int i = rowStart; i <= rowEnd; i++) { for (int j = colStart; j <= colEnd; j++) { if (!used[i, j]) { found = FindWords(i, j, used, prefix, completions, found); } } } used[row, col] = false; prefix.Length--; return(found); }
/// <summary> /// Gets all words in the given results, plus all words formed by starting /// with the content of the given StringBuilder, followed by a word that is /// both contained in the given trie of completions and formed by the letters /// on a nonempty path that starts from the given board location, does not /// use any die indicated as used in the given array, and does not use any /// die more than once. /// </summary> /// <param name="row">The row containing the current die.</param> /// <param name="col">The column containing the current die.</param> /// <param name="used">Element [i, j] indicates that the die at location /// [i, j] has been used in the path to the current die.</param> /// <param name="path">The letters on the path to the current die.</param> /// <param name="completions">All completions of the letters on the path to /// form valid words.</param> /// <param name="results">The words found so far.</param> /// <returns>All the words found.</returns> private ITrie GetWords(int row, int col, bool[,] used, StringBuilder path, ITrie completions, ITrie results) { completions = completions.GetCompletions(_board[row, col]); if (completions == null) { return(results); } else { used[row, col] = true; path.Append(_board[row, col]); if (completions.Contains("")) { results = results.Add(path.ToString()); } for (int i = Math.Max(0, row - 1); i < Math.Min(_gridSize, row + 2); i++) { for (int j = Math.Max(0, col - 1); j < Math.Min(_gridSize, col + 2); j++) { if (!used[i, j]) { results = GetWords(i, j, used, path, completions, results); } } } used[row, col] = false; path.Length -= _board[row, col].Length; return(results); } }
/// <summary> /// looks to see if any prefix is presents /// </summary> /// <param name="prefix">the prefix ebing searched for</param> /// <returns>a set of names </returns> private SortedSet <Employee> PrefixSearch(string prefix) { uxListBox.Items.Clear(); ITrie names = _names.GetCompletions(prefix); List <string> temp = new List <string>(); if (names != null) { uxListBox.BeginUpdate(); names.AddAll(new StringBuilder(prefix), temp); uxListBox.EndUpdate(); } SortedSet <Employee> nNames = new SortedSet <Employee>(); foreach (string s in temp) { int i = 0; if (_namesDictionary.TryGetValue(s, out List <Employee> tempList) == true) { nNames.Add(tempList[i]); i++; } } return(nNames); }
/// <summary> /// Gets a trie containing all words previously found, plus all words /// formed by starting with the content of the StringBuilder, followed by /// a word that is both contained in the trie of completions and formed by /// the letters on a nonempty path that starts from the current board location, /// does not use any die on the path to the current location and does not use /// any die more than once. /// </summary> /// <param name="row">The row of the current board position</param> /// <param name="col">The column of the current borad position</param> /// <param name="used">Indicates the board positions that have been used in the current prefix.</param> /// <param name="prefix">The current prefix.</param> /// <param name="completions">The completions of the current prefix to full words.</param> /// <param name="words">The words found so far.</param> /// <returns>The words in "words", along with the words found by this method.</returns> private ITrie FindWords(int row, int col, bool[,] used, StringBuilder prefix, ITrie completions, ITrie words) { ITrie t = completions.GetCompletions(_board[row, col]); if (t == null) { return(words); } used[row, col] = true; prefix.Append(_board[row, col]); if (t.Contains("")) { words = words.Add(prefix.ToString()); } for (int i = Math.Max(0, row - 1); i < Math.Min(_gridSize, row + 2); i++) { for (int j = Math.Max(0, col - 1); j < Math.Min(_gridSize, col + 2); j++) { if (!used[i, j]) { words = FindWords(i, j, used, prefix, t, words); } } } used[row, col] = false; prefix.Length -= _board[row, col].Length; return(words); }
/// <summary> /// Finds valid words within the Trie. /// </summary> /// <param name="used"></param> /// <param name="prefix"></param> /// <param name="_wordList"></param> /// <param name="found"></param> /// <returns></returns> private ITrie FindWords(int row, int col, bool[,] used, StringBuilder prefix, ITrie wordsFound, ITrie results) { ITrie complete = wordsFound.GetCompletions(_board[row, col]); if (complete == null) { return(results); } used[row, col] = true; prefix.Append(_board[row, col]); if (complete.Contains("")) { results = results.Add(prefix.ToString()); } for (int i = Math.Max(0, row - 1); i < Math.Min(_gridSize, row + 2); i++) { for (int j = Math.Max(0, col - 1); j < Math.Min(_gridSize, col + 2); j++) { if (used[i, j] == false) { results = FindWords(i, j, used, prefix, complete, results); } } } used[row, col] = false; prefix.Length -= _board[row, col].Length; return(results); }
/// <summary> /// Handles a Click event on the "Look up Word" button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookUp_Click(object sender, EventArgs e) { uxCompletions.Items.Clear(); ITrie completions = _dictionary.GetCompletions(uxWord.Text); // Replace null with code to get the trie containing all completions of uxWord.Text if (completions != null) { uxCompletions.BeginUpdate(); completions.AddAll(new StringBuilder(uxWord.Text), uxCompletions.Items); uxCompletions.EndUpdate(); } }
/// <summary> /// Gets all of the strings that form words in this trie when appended to the given prefix. /// </summary> /// <param name="prefix">The prefix</param> /// <returns>A trie containing all of the strings that form words in this trie when appended /// to the given prefix.</returns> public ITrie GetCompletions(string prefix) { if (prefix == "") { return(this); } else if (prefix[0] == _childLabel) { return(_child.GetCompletions(prefix.Substring(1))); } else { return(null); } }
/// <summary> /// Handles a Click event on the "Look up Completions" button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookUp_Click(object sender, EventArgs e) { uxCompletions.Items.Clear(); ITrie completions = _dictionary.GetCompletions(uxWord.Text); if (completions != null) { uxCompletions.BeginUpdate(); completions.AddAll(new StringBuilder(uxWord.Text), uxCompletions.Items); uxCompletions.EndUpdate(); } }
/// <summary> /// Handles a Click event on the "Look up Word" button. /// </summary> /// <param name="sender">boject from the event</param> /// <param name="e">event object</param> private void uxLookUp_Click(object sender, EventArgs e) { uxCompletions.Items.Clear(); ITrie completions = _dictionary.GetCompletions(uxWord.Text);// Fill in code to get the trie containing all completions of uxWord.Text if (completions != null) { uxCompletions.BeginUpdate(); // Add code to place all words beginning with uxWord.Text into the IList uxCompletions.Items. StringBuilder prefix = new StringBuilder(uxWord.Text); completions.AddAll(prefix, uxCompletions.Items); uxCompletions.EndUpdate(); } }