示例#1
0
        public static void TestGetPermutation()
        {
            int[] factors = PermutationEnumerator.GetFactors(4);
            int[] result  = PermutationEnumerator.GetPermutation(4, 21, factors);

            Log.Debug(".testGetPermutation result=" + result.Render());
            Assert.IsTrue(Collections.AreEqual(result, new[] { 3, 1, 2, 0 }));
        }
示例#2
0
 public static void TestFaculty()
 {
     Assert.AreEqual(0, PermutationEnumerator.Faculty(0));
     Assert.AreEqual(1, PermutationEnumerator.Faculty(1));
     Assert.AreEqual(2, PermutationEnumerator.Faculty(2));
     Assert.AreEqual(6, PermutationEnumerator.Faculty(3));
     Assert.AreEqual(24, PermutationEnumerator.Faculty(4));
     Assert.AreEqual(120, PermutationEnumerator.Faculty(5));
     Assert.AreEqual(720, PermutationEnumerator.Faculty(6));
 }
示例#3
0
 public void TestInvalid()
 {
     try {
         PermutationEnumerator.Create(0);
         Assert.Fail();
     }
     catch (ArgumentException ex) {
         // expected
     }
 }
        /// <summary>
        /// Creates the specified number set.
        /// </summary>
        /// <param name="numberSet">The number set.</param>
        /// <returns></returns>
        public static IEnumerable <int[]> New(int[] numberSet)
        {
            var permutationEnumerator = PermutationEnumerator.Create(numberSet.Length).GetEnumerator();

            while (permutationEnumerator.MoveNext())
            {
                var permutation = permutationEnumerator.Current;
                var result      = new int[numberSet.Length];
                for (var i = 0; i < numberSet.Length; i++)
                {
                    result[i] = numberSet[permutation[i]];
                }

                yield return(result);
            }
        }
        internal static IEnumerable <int[]> CreateInternal(int[] numberSet)
        {
            for (var shiftCount = 0; shiftCount < numberSet.Length; shiftCount++)
            {
                int[] result = new int[numberSet.Length];
                int   count  = shiftCount;
                for (int i = 0; i < numberSet.Length; i++)
                {
                    int index = count + i;
                    if (index >= numberSet.Length)
                    {
                        index -= numberSet.Length;
                    }
                    result[i] = numberSet[index];
                }

                yield return(result);
            }

            // Initialize the permutation
            // simply always make 4 buckets
            var buckets = new Dictionary <int, List <int> >();

            for (int i = 0; i < numberSet.Length; i++)
            {
                int        bucketNum = i % 4;
                List <int> bucket    = buckets.Get(bucketNum);
                if (bucket == null)
                {
                    bucket             = new List <int>();
                    buckets[bucketNum] = bucket;
                }

                bucket.Add(numberSet[i]);
            }

            var permutationEnumerator = PermutationEnumerator.Create(4).GetEnumerator();

            // we throw the first one away, it is the same as a shift result
            permutationEnumerator.MoveNext();

            while (permutationEnumerator.MoveNext())
            {
                yield return(Translate(numberSet, buckets, permutationEnumerator.Current));
            }
        }
示例#6
0
        public static void TestGetFactors()
        {
            int[] factors = PermutationEnumerator.GetFactors(5);
            Assert.IsTrue(Collections.AreEqual(factors, new[] { 24, 6, 2, 1, 0 }));

            factors = PermutationEnumerator.GetFactors(4);
            Assert.IsTrue(Collections.AreEqual(factors, new[] { 6, 2, 1, 0 }));

            factors = PermutationEnumerator.GetFactors(3);
            Assert.IsTrue(Collections.AreEqual(factors, new[] { 2, 1, 0 }));

            factors = PermutationEnumerator.GetFactors(2);
            Assert.IsTrue(Collections.AreEqual(factors, new[] { 1, 0 }));

            factors = PermutationEnumerator.GetFactors(1);
            Assert.IsTrue(Collections.AreEqual(factors, new[] { 0 }));

            //Log.Debug(".testGetFactors " + CompatExtensions.Render(factors));
        }
示例#7
0
        private void TryPermutation(int numElements, int[][] expectedValues)
        {
            /*
             * Total: 4 * 3 * 2 = 24 = 6!  (6 faculty)
             *
             * Example:8
             * n / 6 = first number        == index 1, total {1}, remains {0, 2, 3}
             * remainder 8 - 1 * 6         == 2
             * n / 2 = second number       == index 1, total {1, 2}, remain {0, 3}
             * remainder 2 - 1 * 2         == 0
             *                          == total {1, 2, 0, 3}
             *
             * Example:21   out {0, 1, 2, 3}
             * 21 / 6                      == index 3 -> in {3}, out {0, 1, 2}
             * remainder 21 - 3 * 6        == 3
             * 3 / 2 = second number       == index 1 -> in {3, 1}, remain {0, 2}
             * remainder 3 - 1 * 2         == 1
             *                          == index 1 -> in {3, 1, 2} out {0}
             */
            var enumeration = PermutationEnumerator.Create(numElements).GetEnumerator();

            int count = 0;

            while (enumeration.MoveNext())
            {
                int[] result   = enumeration.Current;
                int[] expected = expectedValues[count];

                Log.Debug(".tryPermutation result=" + result.Render());
                Log.Debug(".tryPermutation expected=" + result.Render());

                count++;
                Assert.AreEqual(result, expected, "Mismatch in count=" + count);
            }
            Assert.AreEqual(count, expectedValues.Length);

            Assert.IsFalse(enumeration.MoveNext());
        }