Exemple #1
0
        static void Main(string[] args)
        {
            // Generate a Dataset
            Console.WriteLine("Welcome to SGProgrammingTest.");
            Console.WriteLine("Generating Dataset...");
            Console.WriteLine("");
            var PeopleData = new PersonContainer(10);

            foreach (var person in PeopleData.People)
            {
                Console.WriteLine(person);
            }
            Console.WriteLine("");

            // Find the year with the most people
            Console.WriteLine("Solving for the most people alive during a certain year.");
            var YearsWithMostAlive = new List <int>();
            var Analytics          = new AlgorithmAnalytics();

            AlgorithmHelper.CountYearsInPeopleData(PeopleData.People, YearsWithMostAlive, Analytics);

            // Display results
            Console.WriteLine("");
            Console.WriteLine("Results:");
            foreach (var year in YearsWithMostAlive)
            {
                Console.WriteLine(year);
            }
            Console.WriteLine("");
            Console.WriteLine("We found {0} people alive during year(s) listed above.", Analytics.MaxNumberOfPeopleInYears);
            Console.WriteLine("Total years were {0}.", Analytics.TotalYearsFound);
            Console.WriteLine("");

#if DEBUG
            Console.WriteLine("Press enter to continue...");
            Console.Read();
#endif
        }
Exemple #2
0
        /// <summary>
        /// Solves for the year (or years) with the most people alive based on the data set.
        /// </summary>
        /// <param name="PeopleData">A collection of people based on the person class.</param>
        /// <param name="YearsWithMostAlive">The solution found after counting.</param>
        /// <param name="info">Some addition, but not necessary, information for displaying to a human.</param>
        static public void CountYearsInPeopleData(ICollection <Person> PeopleData, List <int> YearsWithMostAlive, AlgorithmAnalytics info = null)
        {
            // This KV pairs represent years as the key and and increment of people alive as the value
            Dictionary <int, int> YearCountKV = new Dictionary <int, int>(100); // only 100 years to care about.

            foreach (var p in PeopleData)
            {
                for (int year = p.BirthYear; year < p.DeathYear; year++)
                {
                    if (!YearCountKV.ContainsKey(year))
                    {
                        YearCountKV.Add(year, 1);
                    }
                    else
                    {
                        YearCountKV[year]++;
                    }
                }
            }

            // List used to store the number of years with the same amount of people alive.
            // IE: 2 years could have 10 people alive in them, so we need to store both years as results
            if (YearsWithMostAlive == null)
            {
                YearsWithMostAlive = new List <int>(100);
            }
            else
            {
                YearsWithMostAlive.Clear();
            }
            // NOTE: This may be faster if we do a sort + insert method on the years with the highest counts.
            //      However because the max number of years is only 100, seems like a small upgrade at best.  KISS
            foreach (var year in YearCountKV.Keys)
            {
                // Show our yearly data to the console window.
                Console.WriteLine(string.Format("Year {0} had {1} people alive.", year, YearCountKV[year]));

                if (YearsWithMostAlive.Count == 0)
                {
                    YearsWithMostAlive.Add(year);
                }
                else
                {
                    if (YearCountKV[YearsWithMostAlive[0]] < YearCountKV[year])
                    {
                        YearsWithMostAlive.Clear();
                        YearsWithMostAlive.Add(year);
                    }
                    else if (YearCountKV[YearsWithMostAlive[0]] == YearCountKV[year])
                    {
                        YearsWithMostAlive.Add(year);
                    }
                }
            }

            // Analytic info stored here.
            if (info != null)
            {
                info.MaxNumberOfPeopleInYears = YearCountKV[YearsWithMostAlive[0]];
                info.TotalYearsFound          = YearsWithMostAlive.Count;
            }
        }