コード例 #1
0
        public void Go()
        {
            var words   = new WordsCache();
            var lengths = words.Words.Keys.OrderBy(l => l).ToArray();

            foreach (var length in lengths)
            {
                Console.WriteLine(new { length, words = words.Words[length].Sum(kvp => kvp.Value.Length) });
            }
            Console.WriteLine();

            lengths.BinarySearch(widthiwi =>
            {
                var width          = widthiwi.Item;
                var smallerLenghts = lengths.Where(l => l <= width).ToArray();
                var solutionFound  = false;

                smallerLenghts.BinarySearch(heightiwi =>
                {
                    var height = heightiwi.Item;
                    Console.WriteLine("Trying: " + new { width, height });

                    foreach (var solution in Solve(words, width, height))
                    {
                        Console.WriteLine(Environment.NewLine.Join(solution));
                        Console.WriteLine();
                        solutionFound = true;
                    }

                    return(!solutionFound ? -1 : 1);
                });

                return(!solutionFound ? -1 : 1);
            });
        }
コード例 #2
0
        private static IEnumerable <string[]> Solve(WordsCache words, int width, int height)
        {
            var rowStrings = words.Words[width];
            var colStrings = words.Words[height];

            var rows    = new string[height];
            var columns = new string[width];

            foreach (var firstRow in words.WordsOverall[width])
            {
                foreach (var secondRow in words.WordsOverall[width])
                {
                    foreach (var firstCol in GetStringsByPrefix(colStrings, firstRow[0].ToString() + secondRow[0]))
                    {
                        foreach (var secondCol in GetStringsByPrefix(colStrings, firstRow[1].ToString() + secondRow[1]))
                        {
                            rows[0]    = firstRow;
                            rows[1]    = secondRow;
                            columns[0] = firstCol;
                            columns[1] = secondCol;
                            Array.Clear(rows, 2, height - 2);
                            Array.Clear(columns, 2, width - 2);

                            foreach (var solution in Solve(rowStrings, colStrings, rows, columns, 2))
                            {
                                yield return(solution);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
        public void WhenRequestingAnItemInTheCache_ThenReturnsSameItem()
        {
            var words1 = WordsCache.Get(FromDictionary.InternetUrl);
            var words2 = WordsCache.Get(FromDictionary.InternetUrl);

            words1.ShouldBeSameAs(words2);
        }
コード例 #4
0
 /// <summary>
 /// Gets a data source for a file dictionary, which can be built-in or a user-supplied text file.
 /// </summary>
 /// <param name="dictionaryName">The name of the file dictionary, without the extension.
 /// Recommended to use the FromDictionary list of constants for the built-in file dictionaries.
 /// Just use a normal string for user-supplied text files.</param>
 /// <returns></returns>
 public Words Words(string dictionaryName)
 {
     return(WordsCache.Get(dictionaryName));
 }
コード例 #5
0
 public void Dispose()
 {
     WordsCache.Clear();
 }
コード例 #6
0
        public void WhenUsingCachedWordsForNonExistentDictionary_ThenWordsSourceThrowsFileNotFoundException()
        {
            var words = WordsCache.Get(DictionaryThatDoesNotExist);

            Should.Throw <FileNotFoundException>(() => words.Next());
        }
コード例 #7
0
        public void WhenRequestingAnItemNotInTheCache_ThenReturnsWordsSourceForItemDictionary()
        {
            var words = WordsCache.Get(DictionaryThatDoesNotExist);

            words.DictionaryName.ShouldBe(DictionaryThatDoesNotExist);
        }
コード例 #8
0
 public WordsCacheTests()
 {
     WordsCache.Clear();
 }