Example #1
0
        public void AddOption(string key, string text)
        {
            Dictionary <char, int> charCounter = new Dictionary <char, int>()
            {
                { 'a', 0 }, { 'b', 0 }, { 'c', 0 }, { 'd', 0 },
                { 'e', 0 }, { 'f', 0 }, { 'g', 0 }, { 'h', 0 },
                { 'i', 0 }, { 'j', 0 }, { 'k', 0 }, { 'l', 0 },
                { 'm', 0 }, { 'n', 0 }, { 'o', 0 }, { 'p', 0 },
                { 'q', 0 }, { 'r', 0 }, { 's', 0 }, { 't', 0 },
                { 'u', 0 }, { 'v', 0 }, { 'w', 0 }, { 'x', 0 },
                { 'y', 0 }, { 'z', 0 }
            };
            List <int> percentageDiffs = new List <int>();

            int totalCharCount = 0;

            foreach (char c in text)
            {
                int charIndex = c;
                if ((charIndex >= 65 && charIndex <= 90) || (charIndex >= 97 && charIndex <= 122))
                {
                    totalCharCount++;
                }

                char newCharLower = char.ToLower(c);
                if (charCounter.ContainsKey(newCharLower))
                {
                    charCounter[newCharLower]++;
                }
            }
            foreach (KeyValuePair <char, int> item in charCounter)
            {
                double charPercentage = (100.00 / totalCharCount) * item.Value;
                double percentageDiff = ((EngFrequencies[item.Key] - charPercentage) / EngFrequencies[item.Key]) * 100;
                percentageDiff = Math.Abs(percentageDiff);
                percentageDiffs.Add(Convert.ToInt32(percentageDiff));
            }
            int averageDiff = 0;

            foreach (int item in percentageDiffs)
            {
                averageDiff += item;
            }

            averageDiff /= 26;

            Attempts.Add(key, averageDiff);

            AverageChange?.Invoke(string.Format("!!!Difference {1}", key, averageDiff));
        }
 public AverageAggregator(AverageChange averageChangeDelegate)
 {
     sum = 0;
     average = 0;
     AverageChanged += averageChangeDelegate;
 }