How to get true if all array elements in the same position are correct

I work with n logical arrays such as the following:

A  = [1, 1, 0, 1]
A1 = [0, 1, 0, 1]
A2 = [0, 1, 0, 0]

B  = [1, 1, 0, 1]
B1 = [0, 0, 0, 1]
B2 = [0, 1, 0, 0]

I am working on a function that returns true if at least one column of 1 exists (for example, in three arrays of A I get a true value, because A[1] = A1[1] = A2[1] = true) In arrays of B, I get false instead.

I know that I can create the resulting logical array and get the result with loops, but I ask if there is a more elegant way to get this result without using too many loops.

+4
source share
7 answers

Something like this would do the trick:

bool[] A = ...;
bool[] A1 = ...;
bool[] A2 = ...;

var length = Math.Min(Math.Min(A.Length, A1.Length), A2.Length);
var result = Enumerable.Range(0, length).Any(i => A[i] && A1[i] && A2[i]);

, , , .

+6

,

class Program
    {
        static void Main(string[] args)
        {
            var A = new bool[] { true, true, false, true };
            var A1 = new bool[] { false, true, false, true };
            var A2 = new bool[] { false, true, false, false };

            var B = new bool[] { true, true, false, true };
            var B1 = new bool[] { false, false, false, true };
            var B2 = new bool[] { false, true, false, false };

            Console.WriteLine(AtLeastOneColumnIsTrue(A, A1, A2));
            Console.WriteLine(AtLeastOneColumnIsTrue(B, B1, B2));

            Console.ReadLine();
        }

        public static bool AtLeastOneColumnIsTrue(params bool[][] boolArrays)
        {
            for (int column = 0; column < boolArrays[0].Length; column++)
            {
                var columnIsAllTrue = true;

                for (int boolArray = 0; boolArray < boolArrays.Length && columnIsAllTrue; boolArray++)
                {
                    columnIsAllTrue = columnIsAllTrue && boolArrays[boolArray][column];
                }

                if (columnIsAllTrue)
                {
                    return true;
                }
            }

            return false;
        }
    }
+1

, :

class Program
{
    public static bool[] Convert(params bool[][] args)
    {            
        return (new List<bool[]>(args)).Aggregate((a, b) => a.Select((x, i) => x && b[i]).ToArray());
    }

    static void Main(string[] args)
    {
        var A = new[] { true, true, false, true };
        var A1 = new[] { false, true, false, true };
        var A2 = new[] { false, true, false, false };
        var A3 = new[] { true, true, true, true };

        var res = Convert(A, A1, A2, A3);
    }
}
+1

, boolean[][]:

:

bool[][] boolArrays1 = { 
                           new[]{true, true, false, true}, 
                           new[]{false, true, false, true}, 
                           new[]{false, true, false, false}, 
                       };
bool[][] boolArrays2 = { 
                           new[]{true, true, false, true}, 
                           new[]{false, false, false, true}, 
                           new[]{false, true, false, false}, 
                       };

Any All:

bool anyColumnTrue1 = Enumerable.Range(0, boolArrays1.Min(arr => arr.Length))
    .Any(ix => boolArrays1.All(arr => arr[ix]));  // true
bool anyColumnTrue2 = Enumerable.Range(0, boolArrays2.Min(arr => arr.Length))
    .Any(ix => boolArrays2.All(arr => arr[ix]));  // false

, , , :

public static bool AnyColumnTrue(this IEnumerable<IList<bool>> bools) 
{
    if (bools == null) throw new ArgumentNullException("bools");

    return Enumerable.Range(0, bools.Min(seq => seq.Count))
        .Any(ix => bools.All(arr => arr[ix]));
}

:

bool anyColumnTrue = boolArrays1.AnyColumnTrue();

, , :

bool anyColumnTrue = new[]{ A, A1, A2 }.AnyColumnTrue();
+1

( ):

    private static Boolean HasOnes(params Boolean[][] items) {
      for (int i = items.Min(line => line.Length) - 1; i >= 0; --i)
        if (items.All(line => line[i]))
          return true;

      return false;
    }

....

    Boolean[] A =  { true, true, false, true };
    Boolean[] A1 = { false, true, true, true };
    Boolean[] A2 = { false, true, false, false };

    Console.Write(HasOnes(A, A1, A2) ? "Yes" : "No");

    Boolean[] B  = {true, true, false, true};
    Boolean[] B1 = {false false, false, true};
    Boolean[] B2 = {false, true, false, false};

    Console.Write(HasOnes(B, B1, B2) ? "Yes" : "No");
+1

Another alternative using Linq:

var anyColumnAllTrue = A.Select((value, index) => 
    value && A1[index] && A2[index])
.Any(result => result);
+1
source

Although this conversion process is far from the fastest, I would rather consider this situation as a set of binary bits rather than a sequence of bools - at this point, the AND binary will give you the answer:

int ToInt(IEnumerable<bool> bits) => bits.Select((b, i) => ((b ? 1 : 0) << i))
                                         .Aggregate((b, b1) => (b | b1));    

bool HasCommonBits(params <IEnumerable<bool>[] seqs) => 
     seqs.Aggregate((s, i) => s.ToInt() & i) > 0;
...

if(HasCommonBits(A, A1, A2))
...
0
source

Source: https://habr.com/ru/post/1627251/


All Articles