Esempio n. 1
0
        private PairOfPeople GetSmallestAgeDifference(List <Person> sortedPeople)
        {
            var smallestAgeDifferenceSoFar = new PairOfPeople()
            {
                FirstPerson   = sortedPeople[0],
                SecondPerson  = sortedPeople[1],
                AgeDifference = sortedPeople[1].BirthDate - sortedPeople[0].BirthDate
            };

            for (var i = 0; i < sortedPeople.Count - 1; i++)
            {
                var currentPerson        = sortedPeople[i];
                var nextPerson           = sortedPeople[i + 1];
                var currentAgeDifference = nextPerson.BirthDate - currentPerson.BirthDate;

                if (currentAgeDifference < smallestAgeDifferenceSoFar.AgeDifference)
                {
                    smallestAgeDifferenceSoFar = new PairOfPeople()
                    {
                        FirstPerson   = currentPerson,
                        SecondPerson  = nextPerson,
                        AgeDifference = currentAgeDifference
                    };
                }
            }

            return(smallestAgeDifferenceSoFar);
        }
Esempio n. 2
0
        private PairOfPeople GetLargestAgeDifference(List <Person> sortedPeople)
        {
            var indexOfPastPerson = sortedPeople.Count - 1;
            var oldestPerson      = sortedPeople[indexOfPastPerson];
            var youngestPerson    = sortedPeople[0];
            var ageGap            = oldestPerson.BirthDate - youngestPerson.BirthDate;

            var largestAgeGapPair = new PairOfPeople()
            {
                FirstPerson   = youngestPerson,
                SecondPerson  = oldestPerson,
                AgeDifference = ageGap
            };

            return(largestAgeGapPair);
        }
Esempio n. 3
0
        public PairOfPeople Find(AgeDifference ageDifference)
        {
            PairOfPeople output = new PairOfPeople();

            if (_allPeople.Count > 1)
            {
                var sortedPeople = SortPeopleByBirthDate();

                var possibleOutput = new PairOfPeople[2]
                {
                    GetSmallestAgeDifference(sortedPeople),
                    GetLargestAgeDifference(sortedPeople)
                };

                output = possibleOutput[(int)ageDifference];
            }

            return(output);
        }
Esempio n. 4
0
 private void AddAllPairs(List <PairOfPeople> listOfPairs)
 {
     for (var i = 0; i < people.Count - 1; i++)
     {
         for (var j = i + 1; j < people.Count; j++)
         {
             var pair = new PairOfPeople();
             if (people[i].BirthDate < people[j].BirthDate)
             {
                 pair.Younger = people[i];
                 pair.Older   = people[j];
             }
             else
             {
                 pair.Younger = people[j];
                 pair.Older   = people[i];
             }
             pair.AgeDifference = pair.Older.BirthDate - pair.Younger.BirthDate;
             listOfPairs.Add(pair);
         }
     }
 }
Esempio n. 5
0
        public PairOfPeople Find(FinderFlag flag)
        {
            var listOfPairs = new List <PairOfPeople>();

            AddAllPairs(listOfPairs);


            PairOfPeople answer = new PairOfPeople();

            if (listOfPairs.Count < 1)
            {
                return(answer);
            }

            listOfPairs = listOfPairs.OrderBy(p => p.AgeDifference).ToList();

            var finderFlag = new Dictionary <FinderFlag, Action <IEnumerable <PairOfPeople> > >()
            {
                { FinderFlag.Closest, (x) => x.First() }, { FinderFlag.Furthest, (x) => x.Last() }
            };

            return(() => finderFlag[flag]);

            // switch (flag)
            // {
            //     case FinderFlag.Closest:
            //         answer = listOfPairs.First();
            //         break;

            //     case FinderFlag.Furthest:
            //         answer = listOfPairs.Last();
            //         break;
            // }

            // return answer;
        }