Ejemplo n.º 1
0
        static void DebugListActivePossibilities()
        {
            Console.Write("Range> ");

            int range = int.Parse(Console.ReadLine());

            List <int> input = new List <int>();

            for (int i = 0; i < range; i++)
            {
                input.Add(i);
            }

            List <List <int> > possibilities = ProgramMath.GetListActivePossibilities(input);

            foreach (List <int> possibility in possibilities)
            {
                foreach (int n in possibility)
                {
                    Console.Write(n.ToString() + ' ');
                }

                Console.WriteLine();
            }
        }
        /// <summary>
        /// Get the likelihood of possible key lengths given repetitions that have been found in a text
        /// </summary>
        /// <param name="repetitions">The repetitions that have been found in a text</param>
        /// <returns>{keyLength : occurences}</returns>
        public static Dictionary <int, int> GetKeyLengthWeights(Repetition[] repetitions,
                                                                bool discardSingleOccurence = false)
        {
            Dictionary <int, int> lengthWeightings = new Dictionary <int, int>();

            foreach (Repetition repetition in repetitions)
            {
                List <List <int> > positionsActivePossibilities = ProgramMath.GetListActivePossibilities(repetition.positions, true);

                foreach (List <int> positions in positionsActivePossibilities)
                {
                    //Try each combination of repetitions being checked and not being checked in case some where coincidence

                    bool valid = true; /*All spacings are equal*/

                    int spacing = positions[1] - positions[0];

                    if (positions.Count > 2)
                    {
                        for (int i = 2; i < positions.Count; i++)
                        {
                            if (positions[i] - positions[i - 1] != spacing)
                            {
                                valid = false;
                            }
                        }
                    }

                    if (valid)
                    {
                        if (lengthWeightings.ContainsKey(spacing))
                        {
                            lengthWeightings[spacing]++;
                        }
                        else
                        {
                            lengthWeightings.Add(spacing, 1);
                        }
                    }
                }
            }

            if (discardSingleOccurence)
            {
                Dictionary <int, int> newOutput = new Dictionary <int, int>();

                foreach (int key in lengthWeightings.Keys)
                {
                    if (lengthWeightings[key] > 1)
                    {
                        newOutput.Add(key, lengthWeightings[key]);
                    }
                }

                return(newOutput);
            }
            else
            {
                return(lengthWeightings);
            }
        }