Exemplo n.º 1
0
        private static ProductAndFactors GetLargestProductFromUpperLeftHalfOfMatrix(List <List <int> > numberGrid, int countOfAdjacentNumbers)
        {
            var result = new ProductAndFactors();

            for (var i = numberGrid.Count - 1; i >= countOfAdjacentNumbers - 1; i--)
            {
                var startingPointRow    = i;
                var startingPointColumn = 0;
                var series = new List <int>();
                for (var j = 0; j <= i; j++)
                {
                    series.Add(numberGrid[startingPointRow - j][startingPointColumn + j]);
                }

                var currentColumn = ProductInSeries.GetLargestProductInASeries(series, countOfAdjacentNumbers);
                if (currentColumn.Product <= result.Product)
                {
                    continue;
                }

                result.Product = currentColumn.Product;
                result.Factors.Clear();
                result.Factors = currentColumn.Factors;
            }
            return(result);
        }
Exemplo n.º 2
0
        public void GivenNumberInput_WhenConvertingStringToIntArray_ThenGetCorrectArray()
        {
            var result = ProductInSeries.ConvertStringToIntArray("02358");

            Assert.AreEqual(5, result.Count);
            Assert.AreEqual(0, result[0]);
            Assert.AreEqual(2, result[1]);
            Assert.AreEqual(3, result[2]);
            Assert.AreEqual(5, result[3]);
            Assert.AreEqual(8, result[4]);
        }
Exemplo n.º 3
0
        public void WhenSeriesIsLongerThanAmountOfAdjacentDigits_ThenReturnCorrectResult()
        {
            var series = new List <int> {
                1, 9, 5, 7, 3, 8, 4, 9
            };
            var result = ProductInSeries.GetLargestProductInASeries(series, 5);

            Assert.AreEqual(7560, result.Product);
            Assert.AreEqual(9, result.Factors[0]);
            Assert.AreEqual(5, result.Factors[1]);
            Assert.AreEqual(7, result.Factors[2]);
            Assert.AreEqual(3, result.Factors[3]);
            Assert.AreEqual(8, result.Factors[4]);
        }
Exemplo n.º 4
0
        private static ProductAndFactors GetLargestProductHorizontally(List <List <int> > numberGrid, int countOfAdjacentNumbers)
        {
            var result = new ProductAndFactors();

            foreach (var row in numberGrid)
            {
                var currentRow = ProductInSeries.GetLargestProductInASeries(row, countOfAdjacentNumbers);
                if (currentRow.Product <= result.Product)
                {
                    continue;
                }

                result.Product = currentRow.Product;
                result.Factors.Clear();
                result.Factors = currentRow.Factors;
            }
            return(result);
        }
Exemplo n.º 5
0
        private static ProductAndFactors GetLargestProductVertically(List <List <int> > numberGrid, int countOfAdjacentNumbers)
        {
            var result = new ProductAndFactors();

            for (var i = 0; i < numberGrid.Count; i++)
            {
                var series = new List <int>();
                foreach (var row in numberGrid)
                {
                    series.Add(row[i]);
                }
                var currentColumn = ProductInSeries.GetLargestProductInASeries(series, countOfAdjacentNumbers);
                if (currentColumn.Product <= result.Product)
                {
                    continue;
                }

                result.Product = currentColumn.Product;
                result.Factors.Clear();
                result.Factors = currentColumn.Factors;
            }
            return(result);
        }
Exemplo n.º 6
0
        public void WhenSeriesIsShorterThanAmountOfAdjacentDigits_ThenThrowError()
        {
            var series = new List <int>();

            ProductInSeries.GetLargestProductInASeries(series, 5);
        }
Exemplo n.º 7
0
 public void GivenLettersInInput_WhenConvertingStringToIntArray_ThenThrowError()
 {
     ProductInSeries.ConvertStringToIntArray("1234086532t324");
 }