oshirowanen
oshirowanen

Reputation: 15925

More than your normal permutations

After using Google to find examples, I found the following example goes which works for generating permutations:

namespace ConsoleApp1
{
    class Program
    {
        public static void Main()
        {
            int n, i;
            formPermut test = new formPermut();
            int[] arr1 = new int[5];

            Console.WriteLine("\n\n Recursion : Generate all possible permutations of an array :");
            Console.WriteLine("------------------------------------------------------------------");

            Console.Write(" Input the number of elements to store in the array [maximum 5 digits ] :");
            n = Convert.ToInt32(Console.ReadLine());
            Console.Write(" Input {0} number of elements in the array :\n", n);
            for (i = 0; i < n; i++)
            {
                Console.Write(" element - {0} : ", i);
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.Write("\n The Permutations with a combination of {0} digits are : \n", n);
            test.prnPermut(arr1, 0, n - 1);
            Console.Write("\n\n");
            Console.ReadKey();
        }

        class formPermut
        {
            public void swapTwoNumber(ref int a, ref int b)
            {
                int temp = a;
                a = b;
                b = temp;
            }
            public void prnPermut(int[] list, int k, int m)
            {
                int i;
                if (k == m)
                {
                    for (i = 0; i <= m; i++)
                        Console.Write("{0}", list[i]);
                    Console.Write(" ");
                }
                else
                    for (i = k; i <= m; i++)
                    {
                        swapTwoNumber(ref list[k], ref list[i]);
                        prnPermut(list, k + 1, m);
                        swapTwoNumber(ref list[k], ref list[i]);
                    }
            }
        }
    }
}

So, if I have 2 input values 1 and 2, the above code will return the following results 12 and 21.

I need it to return more results than that for example:

1 12 2 21

If I type in 3 values 1 and 2 and 3, at the moment it returns:

123 132 213 231 321 312

But I need it to return all the double digit and single digit permutations at the same time.

Anyone know how I can go about doing that?

For example:

1 2 3 12 13 21 23 31 32 123 132 213 321 312 and so on, I may have missed out some combinations/permutations.

My end goal is to be able to do the same thing, but with strings, so if the input is one and two, the output would be:

one onetwo two twoone

for three inputs such as:

one and two and three

the output would be:

one two three onetwo onethree twoone twothree threeone threetwo onetwothree onethreetwo twoonethree threetwoone threeonetwo assuming i have not missed out any combinations.

Upvotes: 2

Views: 86

Answers (1)

Elliveny
Elliveny

Reputation: 2203

This seems to do the job?

Test cases:

Input: 1,2 gives...

2 1 21 12 

Input 1, 2, 3 gives...

3 2 32 23 1 31 13 21 12 321 312 231 213 123 132 

Input "one", "two", "three" gives...

three two threetwo twothree one threeone onethree twoone onetwo threetwoone threeonetwo twothreeone twoonethree onetwothree onethreetwo 

From this code:

class Program
{
    public static void Main()
    {
        formPermut test = new formPermut();
        test.prnPermutWithSubsets(new object[] { 1, 2 });
        Console.WriteLine();
        test.prnPermutWithSubsets(new object[] { 1, 2, 3 });
        Console.WriteLine();
        test.prnPermutWithSubsets(new string[] { "one", "two", "three" });
        Console.WriteLine();
        return;
    }

    class formPermut
    {
        private void swapTwoNumber(ref object a, ref object b)
        {
            object temp = a;
            a = b;
            b = temp;
        }
        public void prnPermutWithSubsets(object[] list)
        {
            for (int i = 0; i < Math.Pow(2, list.Length); i++)
            {
                Stack<object> combination = new Stack<object>();
                for (int j = 0; j < list.Length; j++)
                {
                    if ((i & (1 << (list.Length - j - 1))) != 0)
                    {
                        combination.Push(list[j]);
                    }
                }
                this.prnPermut(combination.ToArray(), 0, combination.Count() - 1);
            }
        }

        public void prnPermut(object[] list, int k, int m)
        {
            int i;
            if (k == m)
            {
                for (i = 0; i <= m; i++)
                    Console.Write("{0}", list[i]);
                Console.Write(" ");
            }
            else
                for (i = k; i <= m; i++)
                {
                    swapTwoNumber(ref list[k], ref list[i]);
                    prnPermut(list, k + 1, m);
                    swapTwoNumber(ref list[k], ref list[i]);
                }
        }
    }
}

Upvotes: 1

Related Questions