Exemple #1
0
        public void GetCombinations_WhenCalledWithCombination5_IsValid()
        {
            int[] basketItems           = new int[] { 2, 2, 2, 1, 1 };
            int   bookCombinationNumber = 5;

            var results = _basketHelper.GetCombinations(basketItems, bookCombinationNumber);

            Assert.AreEqual(2, results.Count);
        }
Exemple #2
0
        public List <List <int[]> > GetBasketCombinations(int[] basketItems, int numberOfBooksInSeries)
        {
            if (basketItems.Length != numberOfBooksInSeries)
            {
                throw new Exception();
            }

            // how many zero's?
            var nonzeroCount = numberOfBooksInSeries - basketItems.Count(x => x == 0);

            // this will hold all the basket combinations to consider
            List <List <int[]> > allBasketCombinations = new List <List <int[]> >();
            List <int[]>         results = new List <int[]>();

            // get the different groups of combinations
            for (int i = 1; i <= numberOfBooksInSeries; i++)
            {
                int[] originalBasketItems = (int[])basketItems.Clone();
                results.Clear();
                results = _basketHelper.GetCombinations(originalBasketItems, i);

                allBasketCombinations.Add(results.Select(x => x).ToList());

                // no point looking for (e.g.) combinations of 4 if only 3 in the basket
                if (i == nonzeroCount)
                {
                    break;
                }
            }

            return(allBasketCombinations);
        }