Example #1
0
        public static int CalculateTotalWaysToShare(int value, Coin g)
        {
            var sw = new Stopwatch();

            sw.Start();
            int cnt = 0;
            Dictionary <int, IEnumerable <int> > comboMap = new Dictionary <int, IEnumerable <int> >();

            foreach (var c in ValuePartitioner.PossibleWaysToDivideValueInTwo(value))
            {
                List <int> leafNodes = new List <int>();
                GenerateAllCombinationsForValueKeepingTrackOfTotalCoinsAndAddingLeafNodesToList(c.lhs, 0, g, leafNodes);
                comboMap[c.lhs] = leafNodes;
            }

            sw.Stop();

            Console.WriteLine(sw.ElapsedMilliseconds / 1000);

            sw.Start();

            foreach (var c in ValuePartitioner.PossibleWaysToDivideValueInTwo(value))
            {
                int this_cnt        = 0;
                var lhs_total_coins = comboMap[c.lhs];
                var rhs_total_coins = comboMap[c.rhs];
                foreach (var n in lhs_total_coins)
                {
                    this_cnt += rhs_total_coins.Count(b => b == n);
                }

                cnt += this_cnt;
            }

            Console.WriteLine(sw.ElapsedMilliseconds / 1000);


            return(cnt);
        }
Example #2
0
        public static int CalculateTotalWaysToShare(int value, Coin g)
        {
            int cnt = 0;
            Dictionary <int, IEnumerable <RuntimeCoin> > comboMap = new Dictionary <int, IEnumerable <RuntimeCoin> >();

            foreach (var c in ValuePartitioner.PossibleWaysToDivideValueInTwo(value))
            {
                comboMap[c.lhs] = GenerateAllCombinationsForValue(c.lhs, g);
            }

            foreach (var c in ValuePartitioner.PossibleWaysToDivideValueInTwo(value))
            {
                var lhs_total_coins = comboMap[c.lhs].SelectMany(TotalCoinsForEachCombination).ToArray();
                var rhs_total_coins = comboMap[c.rhs].SelectMany(TotalCoinsForEachCombination).ToArray();

                foreach (var n in lhs_total_coins)
                {
                    cnt += rhs_total_coins.Count(b => b == n);
                }
            }

            return(cnt);
        }
Example #3
0
 public void Partition_3_Has2v1()
 {
     Assert.IsTrue(ValuePartitioner.PossibleWaysToDivideValueInTwo(3).Count(x => x.lhs == 2 && x.rhs == 1) == 1);
     Assert.IsTrue(ValuePartitioner.PossibleWaysToDivideValueInTwo(3).Count(x => x.lhs == 1 && x.rhs == 2) == 1);
 }
Example #4
0
 public void Partition_3_HasTwoSequence()
 {
     Assert.IsTrue(ValuePartitioner.PossibleWaysToDivideValueInTwo(3).Count() == 2);
 }
Example #5
0
 public void Partition_2_HasEqualValues()
 {
     Assert.IsTrue(ValuePartitioner.PossibleWaysToDivideValueInTwo(2).All(x => x.lhs == 1 && x.rhs == 1));
 }
Example #6
0
 public void Partition_2_HasOneSequence()
 {
     Assert.IsTrue(ValuePartitioner.PossibleWaysToDivideValueInTwo(2).Count() == 1);
 }
Example #7
0
 public void Partition_1_IsEmptySet()
 {
     Assert.IsFalse(ValuePartitioner.PossibleWaysToDivideValueInTwo(1).Any());
 }