/// <summary> /// Checks whether all words in msg are in the dictionary. /// </summary> /// <param name="msg">String being checked.</param> /// <returns>True if it is in it, otherwise false.</returns> public bool AllWords(string msg) { string[] pieces = msg.Split(new char [] { ' ' }); foreach (string s in pieces) { if (!_words.Contains(s.Trim())) { return(false); } } return(true); }
/// <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> /// 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); } }
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 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); }
protected void SpeedTestVerify(ITrie trie) { foreach (string address in _addresses) { Assert.IsTrue(trie.Contains(address)); } }
public void Trie_NotContains(ITrie trie) { trie.Add("con"); Assert.IsFalse(trie.Contains("coo")); Assert.IsFalse(trie.Contains("cona")); Assert.IsFalse(trie.Contains("ocon")); Assert.IsFalse(trie.Contains("noc")); Assert.IsFalse(trie.Contains("cno")); Assert.IsFalse(trie.Contains("ocn")); Assert.IsFalse(trie.Contains("ccc")); Assert.IsFalse(trie.Contains("nnn")); Assert.IsFalse(trie.Contains("ooo")); }
/// <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) { if (_dictionary.Contains(uxWord.Text)) { MessageBox.Show("The word is found."); } else { MessageBox.Show("The word is not found."); } }
public void Trie_EnglishWordsPerf(ITrie trie) { string[] strings = _englishWords; foreach (string s in strings) { trie.Add(s); } foreach (string s in strings) { Assert.IsTrue(trie.Contains(s)); } }
public bool Contains(string s) { if (s == "") { return(_hasEmptyString); } else if (s[0] == _label) { return(_child.Contains(s.Substring(1))); } return(false); }
public void Trie_MultiNotContains(ITrie trie) { trie.Add("con"); trie.Add("cono"); trie.Add("foo"); trie.Add("fon"); trie.Add("fondue"); Assert.IsTrue(trie.Contains("con")); Assert.IsTrue(trie.Contains("fon")); Assert.IsTrue(trie.Contains("fondue")); Assert.IsFalse(trie.Contains("fo")); Assert.IsFalse(trie.Contains("cor")); Assert.IsFalse(trie.Contains("corse")); Assert.IsFalse(trie.Contains("fondu")); Assert.IsFalse(trie.Contains("f")); Assert.IsFalse(trie.Contains("x")); }
/// <summary> /// check can be add or not /// </summary> /// <param name="s"></param> /// <returns></returns> public bool Contains(string s) { if (s == "") { return(_check); } else if (s[0].Equals(_label)) { return(_child.Contains(s.Substring(1))); } else { return(false); } }
public bool Contains(String s) { if (s.Length == 0) { return(_wordEndsHere); } else if (s[0] == _letter) { return(_child.Contains(s.Substring(1))); } else { return(false); } }
/// <summary> /// Gets whether the trie rooted at this node contains the given string. /// </summary> /// <param name="s">The string to look up.</param> /// <returns>Whether the trie rooted at this node contains s.</returns> public bool Contains(string s) { if (s == "") { return(_containsEmptyString); } else if (s[0] == _childLabel) { return(_onlyChild.Contains(s.Substring(1))); } else { return(false); } }
public void Trie_FooFonFondue(ITrie trie) { trie.Add("foo"); trie.Add("fon"); trie.Add("fondue"); Assert.IsTrue(trie.Contains("foo")); Assert.IsTrue(trie.Contains("fon")); Assert.IsTrue(trie.Contains("fondue")); Assert.IsFalse(trie.Contains("f")); Assert.IsFalse(trie.Contains("fo")); Assert.IsFalse(trie.Contains("fond")); Assert.IsFalse(trie.Contains("fondu")); }
/// <summary> /// Gets whether the trie rooted at this node contains the given string. /// </summary> /// <param name="s"> the trie to look up</param> /// <returns> the result of the lookup </returns> public bool Contains(string s) { if (s == "") { return(_empty); } else { if (_label != s[0]) { return(false); } else { return(_onlyChild.Contains(s.Substring(1))); } } }
/// <summary> /// Returns whether a word is contained in this trie /// </summary> /// <param name="word">The word to find</param> /// <returns>Whether word is contained in this trie</returns> public bool Contains(string s) { if (s == "") { return(_isWord); } else { if (s[0] == _childLabel) { return(_child.Contains(s.Substring(1))); } else { return(false); } } }
public void Trie_Empty(ITrie trie) { Assert.IsFalse(trie.Contains("")); }
public void Trie_SubstringNotContains(ITrie trie) { trie.Add("abcde"); Assert.IsFalse(trie.Contains("a")); Assert.IsFalse(trie.Contains("ab")); Assert.IsFalse(trie.Contains("abc")); Assert.IsFalse(trie.Contains("abcd")); Assert.IsFalse(trie.Contains("bcde")); Assert.IsFalse(trie.Contains("cde")); Assert.IsFalse(trie.Contains("de")); Assert.IsFalse(trie.Contains("e")); Assert.IsFalse(trie.Contains("b")); Assert.IsFalse(trie.Contains("c")); Assert.IsFalse(trie.Contains("d")); }
public void Trie_Contains(ITrie trie) { trie.Add("con"); Assert.IsTrue(trie.Contains("con")); }
public void Trie_NotContainsUnrelated(ITrie trie) { trie.Add("a"); Assert.IsFalse(trie.Contains("b")); }
/// <summary> /// Does the decreptions /// </summary> /// <param name="cipher">refrence to the text entered in uxText</param> /// <param name="partial">reference to the current 'theory' of what the ansewr is</param> /// <param name="alphaUsed">reference to what letters have been used.</param> /// <returns>If the answer is good</returns> private bool Decrypt(String[] cipher, StringBuilder[] partial, bool[] alphaUsed) { int solvedCount = 0; //Stuff to break recursion foreach (StringBuilder s in partial) { if (!_dictionary.WildcardSearch(s.ToString())) { return(false); } if (!s.ToString().Contains('?')) { if (_dictionary.Contains(s.ToString())) { solvedCount++; } } } if (solvedCount == partial.Length) { return(true); } //What character to replace throughout partial int wordIndex = 0, characterIndex = 0; for (int i = 0; i < cipher.Length; i++) { characterIndex = partial[i].ToString().IndexOf('?'); if (characterIndex != -1) { wordIndex = i; break; } } bool found = false; char v = cipher[wordIndex][characterIndex]; //Replace character with a new character we haven't used yet. for (int boolIndex = 0; boolIndex < 26; boolIndex++) { if (alphaUsed[boolIndex] == false) { for (int i = 0; i < partial.Length; i++) { for (int c = 0; c < partial[i].Length; c++) { if (cipher[i][c] == v) { partial[i][c] = (char)(boolIndex + 'a'); } } } alphaUsed[boolIndex] = true; found = Decrypt(cipher, partial, alphaUsed); //Undo //Could use stacks or linked lists to make this more CPU effecient //Hold change locations in these data structures so we aren't searching the entire array. if (!found) { alphaUsed[boolIndex] = false; for (int i = 0; i < partial.Length; i++) { for (int c = 0; c < partial[i].Length; c++) { if (cipher[i][c] == v) { partial[i][c] = '?'; } } } } else { return(true); } } } return(found); }
public void Trie_Blank(ITrie trie) { trie.Add(""); Assert.IsTrue(trie.Contains("")); }