0

I'm very new to programming, and my good friend is given me some "assignments" that I can solve.

He recently asked me to write a program (console app) that will write all the permutations of the first N letters of the alphabeth. So if N = 3, it will write:

ABC ACB BAC BCA CAB CBA

I've learned about recursive functions, arrays and lists. This is what I got so far, I made it from several answers here from stackoverflow, all I need to learn is how I can make it write only the first N letters out:

 class Program
    {
        static void Main()
        {
            string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            foreach (string[] permutation in Permutation.GetPermutations<string>(items))
            {
                Console.WriteLine(String.Join(", ", permutation));
            }
            Console.ReadKey();
        }

    }

    public class Permutation
    {

        public static IEnumerable<T[]> GetPermutations<T>(T[] items)
        {
            int[] work = new int[items.Length];
            for (int i = 0; i < work.Length; i++)
            {
                work[i] = i;
            }
            foreach (int[] index in GetIntPermutations(work, 0, work.Length))
            {
                T[] result = new T[index.Length];
                for (int i = 0; i < index.Length; i++) result[i] = items[index[i]];
                yield return result;
            }
        }

        public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
        {
            if (len == 1)
            {
                yield return index;
            }
            else if (len == 2)
            {
                yield return index;
                Swap(index, offset, offset + 1);
                yield return index;
                Swap(index, offset, offset + 1);
            }
            else
            {
                foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
                {
                    yield return result;
                }
                for (int i = 1; i < len; i++)
                {
                    Swap(index, offset, offset + i);
                    foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
                    {
                        yield return result;
                    }
                    Swap(index, offset, offset + i);
                }
            }
        }

        private static void Swap(int[] index, int offset1, int offset2)
        {
            int temp = index[offset1];
            index[offset1] = index[offset2];
            index[offset2] = temp;
        }


    }
3
  • 2
    Create a new array from your old array (which is called a subset or a slice or a range of the array): stackoverflow.com/questions/1792470/subset-of-array-in-c-sharp Commented Nov 5, 2017 at 15:19
  • 1
    string[] validLetters = items.Take(n).ToArray(); Commented Nov 5, 2017 at 15:22
  • Your question is really too broad. The marked duplicate shows several the possible ways to solve the problem. Or, you could just pass the N value to your GetPermutations<T>() method and initialize work as new int[n] instead of new int[items.Length]. Or any number of other ways. Commented Nov 5, 2017 at 19:17

1 Answer 1

0

Just change N to set how much letters you want to involve. You also can random then so it doesn't get you always first N letters.

public class Program
{
    public static void Main(string[] args)
    {
        int N = 5;
        string[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
        var take = items.Take(N);
        string str = "";
        foreach(string s in take)
        {
            str += s;
        }
        char[] charArry = str.ToCharArray();
        permute(charArry, 0, str.Length-1);
    }

    static void permute(char[] arry, int i, int n)
    {
        int j;
        if (i==n)
            Console.WriteLine(arry);
        else
        {
            for(j = i; j <=n; j++)
            {
                swap(ref arry[i],ref arry[j]);
                permute(arry,i+1,n);
                swap(ref arry[i], ref arry[j]);
            }
        }
    }

    static void swap(ref char a, ref char b)
    {
        char tmp;
        tmp = a;
        a=b;
        b = tmp;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.