1

Ok, here is the problem that I'm having at the moment: I have a list of elements with which I want to make pairs of all possible combinations.

For example: I have a list of elements "A", "B", "C", "E".

Out of those elements I want to make pairs of all possible combinations (without duplicate elements that already exist while pairing elements), so it would become:

AB

AC

AE

BC

BE

CE

ABC

ABE

ACE

BCE

ABCE

So far, my code doesn't make all combinations like in the example above and it seems to be having a problem with duplicates and I've ran out of ideas how to approach this any further.

    List<char> charList = new List<char> { 'A', 'B', 'C', 'E' };
    List<string> copy = new List<string>();
    List<string> stringList = new List<string>();

    for (int i = 0; i < charList.Count() - 1; i++)
    {
        for (int j = i + 1; j < charList.Count(); j++)
        {
            stringList.Add(charList[i].ToString() + charList[j].ToString());
            copy = stringList.ToList();

        }
    }


    for (int i = 0; i < charList.Count() - 1; i++)
    {
        for (int j = i + 1; j < charList.Count(); j++)
        {
            for (int g = 0; g < stringList.Count(); g++)
            {

                if (!stringList[g].Contains(charList[i]))
                {
                    stringList[g] += charList[i];
                    copy.Add(stringList[g]);
                }
                else if (!stringList[g].Contains(charList[j]))
                {
                    stringList[g] += charList[j];
                    copy.Add(stringList[g]);
                }
            }

        }
    }


    foreach (string value in copy)
    {
        Console.WriteLine(value);
    }

Thanks.

4

3 Answers 3

1

you can use library like this one on CodeProject: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

The code will look something like this:

for (int i = 1; i <= charList.Count(); i++)
{
  Combinations<char> combinations = new Combinations<char>(charList , i);
  foreach(IList<char> c in combinations)
  {
     Console.WriteLine("{{{0}}}", String.Join(" ", chars));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
var charList = new List<char> { 'A', 'B', 'C', 'E' };
List<string> stringList = new List<string>();

for (var i = 1; i < Math.Pow(2, charList.Count); i++)
{
    var sb = new StringBuilder();
    for (var j = 0; j < charList.Count; j++)
    {
        int power = (int)Math.Pow(2, j);
        if ((i & power) == power) sb.Append(charList[j]);
    }
    var s = sb.ToString();
    if (s.Length > 1) stringList.Add(sb.ToString());
}

// Sort results.
stringList.Sort((s1, s2) => s1.Length != s2.Length
    ? s1.Length.CompareTo(s2.Length) : s1.CompareTo(s2));

6 Comments

It doesn't it look like an error to me. Maybe he ment ((i & power) == 1)
Result does not look good AB ABC ABCE ABE AC ACE AE BC BCE BE CE
I tried and got: AB, AC, BC, ABC, AE, BE, ABE, CE, ACE, BCE, ABCE.
@PaulSinnema: Just add sorting and the result will be as expected. stringList = stringList.OrderBy(s => s).OrderBy(s => s.Length).ToList();
Yeah. I added stringlist.Sort() but there are way too little results are there not? I get pages of them
|
0
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string[] list = new string[] { "A", "B", "C", "D" };
        List<string> combined = new List<string>();

        for (int i = 0; i < list.Length; i++)
        {
            combined.Add(list[i]);
            for (int j = 0; j < list.Length; j++)
            {
                combined.Add(list[i] + list[j]);
                for (int k = 0; k < list.Length; k++)
                {
                    combined.Add(list[i] + list[j] + list[k]);
                    for (int l = 0; l < list.Length; l++)
                    {
                        combined.Add(list[i]+list[j] + list[k] + list[l]);
                    }
                }
            }
        }

        combined.ForEach(item => Console.WriteLine(item));

        Console.ReadKey();
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.