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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,927 questions

51,860 answers

573 users

How to create permutations of words without repetition in C#

2 Answers

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;
            }
        }
    }
}

// Show Test.txt file content:

/*
run:

word-1 word-2 word-3 word-4
word-1 word-2 word-4 word-3
word-1 word-3 word-2 word-4
word-1 word-3 word-4 word-2
word-1 word-4 word-2 word-3
word-1 word-4 word-3 word-2
word-2 word-1 word-3 word-4
word-2 word-1 word-4 word-3
word-2 word-3 word-1 word-4
word-2 word-3 word-4 word-1
word-2 word-4 word-1 word-3
word-2 word-4 word-3 word-1
word-3 word-1 word-2 word-4
word-3 word-1 word-4 word-2
word-3 word-2 word-1 word-4
word-3 word-2 word-4 word-1
word-3 word-4 word-1 word-2
word-3 word-4 word-2 word-1
word-4 word-1 word-2 word-3
word-4 word-1 word-3 word-2
word-4 word-2 word-1 word-3
word-4 word-2 word-3 word-1
word-4 word-3 word-1 word-2
word-4 word-3 word-2 word-1

*/


answered Apr 10, 2014 by avibootz
edited Sep 14, 2024 by avibootz
0 votes
using System;
using System.Collections.Generic;

class PermutationsOfWordsWithoutRepetition_CSharp
{
    static void Main()
    {
        string[] words = { "word-1", "word-2", "word-3", "word-4" };

        var result = GeneratePermutations(words, 0, words.Length - 1);
        
        foreach (var permutation in result) {
            Console.WriteLine(string.Join(" ", permutation));
        }
    }

    static List<string[]> GeneratePermutations(string[] words, int start, int end) {
        var result = new List<string[]>();

        if (start == end) {
            result.Add((string[])words.Clone());
        }
        else {
            for (int i = start; i <= end; i++) {
                Swap(ref words[start], ref words[i]);
                result.AddRange(GeneratePermutations(words, start + 1, end));
                Swap(ref words[start], ref words[i]); 
            }
        }

        return result;
    }

    static void Swap(ref string a, ref string b) {
        string temp = a;
        a = b;
        b = temp;
    }
}


/*
run:
       
word-1 word-2 word-3 word-4
word-1 word-2 word-4 word-3
word-1 word-3 word-2 word-4
word-1 word-3 word-4 word-2
word-1 word-4 word-3 word-2
word-1 word-4 word-2 word-3
word-2 word-1 word-3 word-4
word-2 word-1 word-4 word-3
word-2 word-3 word-1 word-4
word-2 word-3 word-4 word-1
word-2 word-4 word-3 word-1
word-2 word-4 word-1 word-3
word-3 word-2 word-1 word-4
word-3 word-2 word-4 word-1
word-3 word-1 word-2 word-4
word-3 word-1 word-4 word-2
word-3 word-4 word-1 word-2
word-3 word-4 word-2 word-1
word-4 word-2 word-3 word-1
word-4 word-2 word-1 word-3
word-4 word-3 word-2 word-1
word-4 word-3 word-1 word-2
word-4 word-1 word-3 word-2
word-4 word-1 word-2 word-3
    
*/

 



answered Sep 14, 2024 by avibootz
edited Sep 14, 2024 by avibootz

Related questions

1 answer 100 views
2 answers 98 views
1 answer 90 views
1 answer 106 views
1 answer 89 views
...