2

How to generate various combinations of opposite values using LinQ/C# I have a list of operators in an array like "> < = + =" i also have a function which can return me the opposite value of each item in an array.Opposite value of ">" is "<" and so on.So considering the reverse operators for each and every value how to generate various possible combinations

Sample: Problem Statement :

`string[] arrSample = new string[]{"!=","="};` //arrSample can be any object array with property values.The property can accept operator values like 
!=,=,>,< etc..

Expected Output: The various combinations considering the reverse operator would be

Output Sequence1: =,!=
Output Sequence2: !=,=
Output Sequence3: = , =
Output Sequence4: !=,!=
SharpC
  • 43
  • 1
  • 8
  • Please can you supply the code for the reverse function - taa – Jon Egerton Apr 25 '11 at 10:03
  • @Jon Egerton: It doesnt have much of a code.Just returns the opposite value of any given operator.Say i pass "=" it returns me "!=" :-) – SharpC Apr 25 '11 at 10:06
  • So do you just want to get a list of the passed operators and their reverse operator, and permute the lot? What would the output be for `string[] arrSample = new string[]{"!=","<","+"};` – Jon Egerton Apr 25 '11 at 10:10
  • I don't understand sequence3 and sequence4. How are the 2 operators different? – Serge Wautier Apr 25 '11 at 10:10
  • How is this reversing the operator, or am I missing something? You're just outputting each combination of the operators – Rob Apr 25 '11 at 10:20
  • @Jon Egerton the answer for your question is [= > -] [= < - ] [= < +] [!= > -] [!= < -] [!= > +] [!= < -] [!= < +] totally 8 combinations for 3 operators – SharpC Apr 25 '11 at 10:30
  • @Rob If we just have = and = i.e = = So these operators will have to be reversed for combinations.Here the order doesnt matter. so for = and = The possible combinations will be = != ; != = ; != != ; = = – SharpC Apr 25 '11 at 10:40

4 Answers4

4

This turned out to be harder than it looks, but the following should demonstrate the idea.

There are two steps - create an array list pairs of each operator and its reverse and then recursively permute these together.

void DoProblem()
{
    //string[] arrSample = new string[] { "!=", "=" };
    string[] arrSample = new string[] { "!=", "<","+" };

    string[][] arrPairs = (from op in arrSample select new string[]{op, GetReverse(op)}).ToArray();

    List<Array> myList = new List<Array>();
    myList.AddRange(arrPairs);

    foreach (string x in Permute(0, myList))
    {
        Console.WriteLine(x);
    }

}

List<string> Permute(int a, List<Array> x)
{
    List<string> retval = new List<string>();
    if (a == x.Count)
    {
        retval.Add("");
        return retval;
    }
    foreach (Object y in x[a])
    {
        foreach (string x2 in Permute(a + 1, x))
        {
            retval.Add(y.ToString() + "," + x2.ToString());
        }

    }
    return retval;
}


string GetReverse(string op)
{
    switch (op) {
        case "=":
            return "!=";
        case "!=":
            return "=";
        case "<":
            return ">";
        case "+":
            return "-";    
        default:
            return "";
    }
}

NOTE: The permute function is based on the permute an Array of Arrays answer here: C# Permutation of an array of arraylists?

Community
  • 1
  • 1
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
  • Whats the best way to thank someone?.Thank you so much.This saved my day (i should say lifetime).Thanks once again. – SharpC Apr 25 '11 at 13:27
  • @SharpC - no problems - happy to help. It always nice when a bit of a brain teaser comes along. – Jon Egerton Apr 25 '11 at 13:57
0
from operator in arrSample select operator + "," + ReverseOperator(operator)

Or did I miss something?

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
  • arrSample contains two operators.Implies the possible combinations would be 2*2 = 4. To give another example. If there are 5 operators then the total possible combinations would be 2*2*2*2*2 = 32. It's not just the opposite value but various possible combinations. – SharpC Apr 25 '11 at 10:13
0

Do you want a union?

    string[] arrSample = new string[]{"!=","="};

    var list = (
            from item in arrSample
            select item + "," + item
        ).Union(
            from revItem in arrSample
            select revItem + "," + ReverseOperator(revItem)
        );

    List<string> final = new List<string>(list);
mcw
  • 3,500
  • 1
  • 31
  • 33
0

Please find the following easiest way to two string arrays with different combination

string[] roles = { "abc", "e", "f", "h" };

string[] allRoles = { "a", "b", "c", "abc", "e", "f", "g", "h", "i" };

    foreach (string nextRole in allRoles)
    {
        if (Array.IndexOf(roles, nextRole) != -1)
        {
            Response.Write(nextRole + "<br/>");
        }
    }