Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to create permutations of words without repetition in C#

Freaking Awesome WordPress Hosting
1,660 views
asked Apr 10, 2014 by avibootz
edited Mar 8, 2015 by avibootz

1 Answer

0 votes
using System;
using System.IO;
using System.Collections.Generic;

namespace Permutations_Words
{
    class Class1
    {
        static void Main(string[] args)
        {
            permutation_words();
        }
        private static void permutation_words()
        {
            string[] stringInput = { "word-1", "word-2", "word-3", "word-4" };

            ShowPermutations<string>(stringInput, stringInput.Length);
        }
        static void ShowPermutations<T>(IEnumerable<T> input, int count)
        {
            string path_write = @"c:\temp\Test.txt"; // You can change the path and file name
            StreamWriter sw = File.CreateText(path_write);
            int j;

            foreach (IEnumerable<T> permutation in PermutationUtils.Permute<T>(input, count))
            {
                j = 0;
                foreach (T pword in permutation)
                {
                    if (j == 0)
                        sw.Write(pword);
                    else
                        sw.Write(" " + pword);
                    j++;
                }
                sw.WriteLine();
            }
            sw.Close();
        }
    }
    class PermutationUtils
    {
        public static IEnumerable<IEnumerable<T>> Permute<T>(IEnumerable<T> list, int count)
        {
            if (count == 0)
            {
                yield return new T[0];
            }
            else
            {
                int startingElementIndex = 0;
                foreach (T startingElement in list)
                {
                    IEnumerable<T> remainingItems = AllExcept(list, startingElementIndex);

                    foreach (IEnumerable<T> permutationOfRemainder in Permute(remainingItems, count - 1))
                    {
                        yield return Concat<T>(new T[] { startingElement }, permutationOfRemainder);
                    }
                    startingElementIndex += 1;
                }
            }
        }
        public static IEnumerable<T> Concat<T>(IEnumerable<T> x, IEnumerable<T> y)
        {
            foreach (T item in x) { yield return item; }
            foreach (T item in y) { yield return item; }
        }
        public static IEnumerable<T> AllExcept<T>(IEnumerable<T> input, int indexToSkip)
        {
            int index = 0;
            foreach (T item in input)
            {
                if (index != indexToSkip) yield return item;
                index += 1;
            }
        }
    }
}







answered Apr 10, 2014 by avibootz
edited Jun 4, 2014 by avibootz
...