How to convert plain English into composed logical conditions?
I have 7 boolean variables name
A1
A2
A3
B1
B2
B3
C
Now the condition should evaluate to true if
- at least one of A and at least one of B is true
- C and at least one of A and B is true
I don't know exactly how I can make a short composed condition out of this:
Any hints how to start?
You didn't specify a context, so I am going by your tags and assuming you are writing this in Java. Below is the code you would use to evaluate the 2 conditional tests you posed.
For "At least one of A and one of B":
bool condition1 = (A1 || A2 || A3) && (B1 || B2 || B3)
For "C and at least one of A and B is true" (reading this as A & B variables are being tested as one):
bool condition2 = C && (A1 || A2 || A3 || B1 || B2 || B3)
At least one means you have to OR them
so your first condition wouwld be (A1 || A2 || A3) && (B1 || B2 || B3)
at least one of A and at least one of B is true or
C and at least one of A and B is true
( (a1 || a2 || a3) && (b1 || b2 || b3) ) || (c && ( (a1 || a2 || a3) && (b1 || b2 || b3) ) )
(A1 || A2 || A3) && (B1 || B2 || B3)
((A1 || A2 || A3) && (B1 || B2 || B3)) && C
"at least one" = OR =
||
"and" = AND =
&&
Therefore the two parts are:
(A1 || A2 || A3) && (B1 || B2 || B3)
or
C && (A1 || A2 || A3 || B1 || B2 || B3)
Therefore:
boolean R = ((A1 || A2 || A3) && (B1 || B2 || B3)) || (C && (A1 || A2 || A3 || B1 || B2 || B3));
Slightly easier on the eye (and, perhaps, the mind):
boolean T1 = (A1 || A2 || A3);
boolean T2 = (B1 || B2 || B3);
boolean R = (T1 && T2) || (C && (T1 || T2));
"At least one of" can also be written as "not none of", so:
!(!B1 && !B2 && !B3)
This can also be written as (applying De Morgan's Rule):
B1 || B2 || B3
The rest of what you've specified can more or less be translated directly, with appropriate use of &&
, ||
, and parentheses to resolve ambiguity.
Something like:
isTrue = ( ((A1 || A2 || A3) && ( B1 || B2 || B3)) ||
( C && ( (A1 || A2 || A3) && ( B1 || B2 || B3) ) ) )
The question is not very clear for the second part:
C and at least one of A and B = C or at least one of A and at least one of B ?
If that is the case, some answers are incorrect.
C and ( A1 and !B1) should evaluate to False, while some expressions in answers will evaluate to true.
精彩评论