0

In have created a simplified lookup table,

For the array sample (The look up table), I want the value to be either true or false.The user will input the response array. The program then compares the array to the sample for sequence equality. Any idea on how i can do this.

//Note code has been simplified

// Array for look up
bool [] firstArray = new bool []{true,false| true};


//....................


//array for response
bool [] sampl = new bool[] {true,false};

if(sample.SequenceEqual(sampl))
{
  Console.WriteLine("There are equal");

//Output should be true
}
Michael
  • 3
  • 3
  • 2
    `if(sample.SequenceEqual(sampl))` where is the definition of `sample`? – fubo May 03 '17 at 12:22
  • @fubo check the line after 'array for response' – Michael May 03 '17 at 12:23
  • 1
    Please correct your question and add more details – Samvel Petrosov May 03 '17 at 12:25
  • 2
    By what logic should an array containing *true* be equal to an array containing *false* ? `|` is the bitwise operator so `false|true` is `true`. Did you want to create a lookup table of boolean expressions? – Panagiotis Kanavos May 03 '17 at 12:25
  • @PanagiotisKanavos , yes. It should be a table of boolean expressions sort of – Michael May 03 '17 at 12:31
  • @MichaelMusora then update the question and explain what you want. What you posted is completely different. For example why use `SequenceEquals`? What are you trying to look up? A single value or are you trying to compare bit masks? What are the rules? Why not use an array of `Func` that takes an input flag and returns true/false? – Panagiotis Kanavos May 03 '17 at 12:37

2 Answers2

0

There are many ways to do this. One way is to traverse both arrays and do a projection on the values of each array. Code below will run through both arrays and store a bool by comparing the values of the item at each index in the array:

var zipped = firstArray.Zip(sampl, (a, b) => (a == b));

Now we can check if there were items that are different.

var hasDiiff = zipped.Any(x=> x == false);

Please note if your arrays do not have the same length, Zip will stop when the first one ends.

You can do the whole thing in one line if you want:

var hasDiff = first array.Zip(sampl, (a, b) => (a == b))
       .Any(x=> x == false);

See my answer here for an in-depth explanation of how Zip works.

Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
0

The value of false| true is true so your definition of firstArray is actually the equivalent of this:

bool [] firstArray = new bool []{true, true};

What you need to do is create a set of rules that you're matching:

Func<bool, bool>[] rules = new Func<bool, bool>[] { x => x == true, x => true };

Then you can do this:

bool[] sampl = new bool[] { true, false };

if (rules.Zip(sampl, (r, s) => r(s)).All(x => x))
{
    Console.WriteLine("There are equal");

    //Output should be true
}

If you want it to read a little easier you can do this:

Func<bool, bool> trueOnly = x => x == true;
Func<bool, bool> falseOnly = x => x == false;
Func<bool, bool> trueOrFalse = x => true;

Func<bool, bool>[] rules = new Func<bool, bool>[] { trueOnly, trueOrFalse };
Enigmativity
  • 113,464
  • 11
  • 89
  • 172