Example #1
0
        /// <summary>
        /// returns the maximum sum of the pyramid. Starts from the bottom section and continues until it reaches the top.
        /// </summary>
        /// <param name="bottomSection">bottom section of the pyramid</param>
        /// <returns>maximum sum</returns>
        public int Totalize(IPyramidSection bottomSection)
        {
            var nextSection     = bottomSection.Previous;
            var totalizedValues = CrossSumArrays(bottomSection.Values, nextSection.Values);

            while (nextSection.Previous != null)
            {
                totalizedValues = CrossSumArrays(totalizedValues, nextSection.Previous.Values);
                nextSection     = nextSection.Previous;
            }
            if (totalizedValues.Count != 1)
            {
                throw new InvalidOperationException("Something went wrong");
            }
            return(totalizedValues[0]);
        }
 /// <summary>
 /// private constructor for linking cotinuous sections of the pyramid
 /// </summary>
 /// <param name="array">value array</param>
 /// <param name="prevSection">previous section that created this section</param>
 private PyramidSection(int[] array, PyramidSection prevSection)
 {
     Values    = array;
     Step      = array.Length;
     _previous = prevSection;
 }