예제 #1
0
        public void Blank()
        {
            string[] words = { "a", "", "b" };

            var collection = new LetterStatisticsCollection(words);

            Assert.AreEqual(2, collection.Count);
        }
예제 #2
0
        static void Main(string[] args)
        {
            //This word list was obtained from
            //http://www.puzzlers.org/pub/wordlists/enable1.txt
            var path = @"..\..\App_Data\enable1.txt";

            var collection = new LetterStatisticsCollection();

            //Read in data from the file and add it to the statistics collection

            #region Stream Variables
            var buffersize = 1048576;  //1 MB buffer size
            var buffer = new char[buffersize];
            int count = 1;
            string prepend = "";
            string[] pieces;

            //Common whitespace characters to split the string that is read in
            char[] splitChars = { ' ', '\n', '\t', '\r' };
            #endregion

            using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read))
            using (BufferedStream bs = new BufferedStream(fs))
            using (StreamReader sr = new StreamReader(bs))
            {
                while (count > 0)
                {
                    //Read in a chunk of data from the file
                    count = sr.Read(buffer, 0, buffersize);

                    //Convert it to a string
                    string readinstring = new String(buffer, 0, count);

                    //Split that string into individual words
                    pieces = readinstring.Split(splitChars);

                    //Prepend the remainder of the last loop to the first item
                    pieces[0] = prepend + pieces[0];

                    //Since the last word may have been cut off, add all but the last
                    //word to the collection
                    collection.Add(pieces.Take(pieces.Length - 1).ToArray());

                    //Save the last word for the next iteration
                    prepend = pieces[pieces.Length - 1];
                }
            }

            //Add the last word
            collection.Add(prepend);

            //Print the collection
            Console.Write(collection.ToString());

            Console.ReadLine();
        }
예제 #3
0
        public void SingleLetter()
        {
            var word = "a";
            var collection = new LetterStatisticsCollection(word);

            Assert.AreEqual("a", collection['a'].LongestWord);
            Assert.AreEqual("a", collection['a'].ShortestWord);
            Assert.AreEqual(1, collection['a'].BeginWith);
            Assert.AreEqual(1, collection['a'].EndWith);
            Assert.AreEqual(1, collection['a'].AverageLength);
        }
예제 #4
0
        public void ABTest()
        {
            string[] words = { "azure", "aab", "azures", "azurite", "azurites", "azygos",
                               "azygoses", "azygous", "ba", "baa", "baaed",
                               "baaing", "baal", "baalim", "baalism", "baalisms" };

            var collection = new LetterStatisticsCollection(words);

            Assert.AreEqual("azurites", collection['a'].LongestWord);
            Assert.AreEqual("aab", collection['a'].ShortestWord);
            Assert.AreEqual(8, collection['a'].BeginWith);
            Assert.AreEqual(2, collection['a'].EndWith);
            Assert.AreEqual(6.25, Math.Round(collection['a'].AverageLength, 2));

            Assert.AreEqual("baalisms", collection['b'].LongestWord);
            Assert.AreEqual("ba", collection['b'].ShortestWord);
            Assert.AreEqual(8, collection['b'].BeginWith);
            Assert.AreEqual(1, collection['b'].EndWith);
            Assert.AreEqual(5.12, Math.Round(collection['b'].AverageLength, 2));
        }
예제 #5
0
        public void ATest()
        {
            string[] words = { "aa", "aah", "aahed", "aahing", "aahs", "aal", "aalii",
                               "aaliis", "aals", "aardvark", "aardvarks", "aardwolf",
                               "aardwolves", "aargh", "aarrgh", "aarrghh", "aas",
                               "aasvogel", "aasvogels" };

            var collection = new LetterStatisticsCollection(words);

            Assert.AreEqual("aardwolves", collection['a'].LongestWord);
            Assert.AreEqual("aa", collection['a'].ShortestWord);
            Assert.AreEqual(19, collection['a'].BeginWith);
            Assert.AreEqual(1, collection['a'].EndWith);
            Assert.AreEqual(5.84, Math.Round(collection['a'].AverageLength, 2));
            Assert.AreEqual(4, collection['h'].EndWith);
            Assert.AreEqual(1, collection['d'].EndWith);
            Assert.AreEqual(1, collection['g'].EndWith);
            Assert.AreEqual(7, collection['s'].EndWith);
            Assert.AreEqual(2, collection['l'].EndWith);
            Assert.AreEqual(1, collection['i'].EndWith);
            Assert.AreEqual(1, collection['k'].EndWith);
            Assert.AreEqual(1, collection['f'].EndWith);
        }