private static void RunTests(string word) { var generator = new WordGenerator(word); Stopwatch sw = new Stopwatch(); sw.Start(); int t = 0; foreach (var w in generator.GetPermutations()) { var s = new string(w); t++; //Debug.WriteLine(t); //Console.Clear(); //Console.Write(t); //Console.WriteLine(s); if (source1.Contains(s)) { Debug.WriteLine($"Found in source1: {s}"); } } var t1 = sw.Elapsed; //Debug.WriteLine($"Time {t1}"); generator = new WordGenerator(word); t = 0; sw.Restart(); foreach (var w in generator.GetPermutations()) { var s = new string(w); t++; //Debug.WriteLine(t); //Console.Clear(); //Console.Write(t); //Console.WriteLine(s); if (source2.Contains(s)) { Debug.WriteLine($"Found in source2: {s}"); } } var t2 = sw.Elapsed; //Debug.WriteLine($"Time {t2}"); Debug.WriteLine($"Diff {(t2 - t1)}"); }
protected IEnumerable <string> EnumerateNeighbors(string word, IWordList dictionary) { for (int i = 0; i < word.Length; i++) { for (char newLetter = 'a'; newLetter <= 'z'; newLetter++) { if (word[i] != newLetter) { string newWord = word.Substring(0, i) + newLetter + word.Substring(i + 1, word.Length - i - 1); if (dictionary.Contains(newWord)) { yield return(newWord); } } } } }
private IEnumerable <WordSequence> FindWordSequences(int currentX, int currentY, string currentLetter) { letters[depth] = currentLetter; used[currentX, currentY] = true; places[depth].X = currentX; places[depth].Y = currentY; CombinationCount++; depth++; bool noWordsWithPrefix = false; if (depth >= 3) { string currentWord = string.Join("", letters, 0, depth); if (dict.Contains(currentWord)) { var tiles = new Point[depth]; for (var tileIndex = 0; tileIndex < depth; tileIndex++) { tiles[tileIndex] = places[tileIndex]; } yield return(new WordSequence(tiles, currentWord)); } noWordsWithPrefix = !dict.ContainsStartingWith(currentWord); } if (!noWordsWithPrefix) { for (int plusY = -1; plusY <= 1; plusY++) { for (int plusX = -1; plusX <= 1; plusX++) { if (plusX == 0 && plusY == 0) { continue; } int newX = currentX + plusX; int newY = currentY + plusY; if ((newX < 0) || (newX >= w) || (newY < 0) || (newY >= h)) { continue; } if (used[newX, newY]) { continue; } string nextLetter = board[newX, newY]; if (nextLetter.Contains("/")) { foreach (var letter in currentLetter.Split('/')) { foreach (var seq in FindWordSequences(newX, newY, letter)) { yield return(seq); } } } else { foreach (var seq in FindWordSequences(newX, newY, nextLetter)) { yield return(seq); } } } } } depth--; letters[depth] = ""; used[currentX, currentY] = false; }
public static void ShouldNotContain(this IWordList wordList, string word) { bool contains = wordList.Contains(word); Assert.IsFalse(contains); }