1

I need a combination of n fields where each field can be equal to null or not null. For each combination, the fields cannot be repeated. Basically, there should be a total of 2^n combinations.

Example:

if I have 2 fields A and B, the combinations in the output should be:

A != null and B != null
A != null and B == null
A == null and B != null
A == null and B == null

if I have 3 fields A, B, and C, the combinations in the output should be:

A != null and B != null and C != null
A != null and B != null and C == null
A != null and B == null and C != null
A != null and B == null and C == null
A == null and B != null and C != null
A == null and B != null and C == null
A == null and B == null and C != null
A == null and B == null and C == null

I don't know what this combination is called, so how can I do this in code where the number of fields is a variable?

Thanks!

10
  • It's the power set of {A, B, C} if you consider that != null means "present in the subset" and == null means "not present in the subset". Commented Mar 2, 2016 at 10:52
  • I don't get what you need, you need the algorithm for generating all those possibilities? another thing is where do you put those returns? Commented Mar 2, 2016 at 11:00
  • @Martin - thanks i think i get an idea about what you mean regarding power sets.. can you give me an example? Commented Mar 2, 2016 at 11:01
  • @PierGiorgioMisley The output should be exactly what i have shown in my question Commented Mar 2, 2016 at 11:02
  • This looks like a state machine to me, for given states do something. Commented Mar 2, 2016 at 11:02

2 Answers 2

3

If you want a generator of such lines you can use Linq:

   int count = 2;

   var lines = Enumerable
     .Range(0, 1 << count) // 1 << count == 2 ** count
     .Select(item => String.Join(" and ", Enumerable
       .Range(0, count)
       .Select(index => ((Char) ('A' + index)).ToString() + 
                        ((item >> index) % 2 == 0 ? " != null" : " == null"))));


   // Let's print out all the lines generated
   Console.Write(String.Join(Environment.NewLine, lines));

For count = 2 the output is

  A != null and B != null
  A == null and B != null
  A != null and B == null
  A == null and B == null

Edit: A small modification lets you put your own names:

  String[] names = new String[] { "A", "B", "C" };

  var lines = Enumerable
    .Range(0, 1 << names.Length) // 1 << count == 2 ** count
    .Select(item => String.Join(" and ", Enumerable
       .Range(0, names.Length)
       .Select(index => names[index] +
                        ((item >> index) % 2 == 0 ? " != null" : " == null"))));

  // Let's print out all the lines generated
  Console.Write(String.Join(Environment.NewLine, lines));
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but the fields im using are variables.. they are not literally "A", "B", or "C"..
@Paul Adrian Pena: a small modification (see my edit) lets you provide your own names
i think i got it.. thanks a lot.. this is brilliant!
0

I usually stick to simple recursion in cases like this, because it's so easy to understand. No explanation, I'll just let the (untested) code talk for itself:

public void Generate()
{
    Create("", 0);
}

private string[] names = new[]{ "A", "B", "C" };

public void Create(string s, int current)
{
    if (current != 0)
    { 
        s += " and ";
    }

    if (current != names.Length)
    {
        string c1 = s + names[current] + " == null"; // case 1
        string c2 = s + names[current] + " != null"; // case 2

        Create(c1, current+1);
        Create(c2, current+1);
    }
    else
    {
        Console.WriteLine(s);
    }
}

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.