Confusing Boolean Expression - boolean

Given these values for the boolean variables x, y, and z:
x = true
y = false
z = true
Why does the following logical expression evaluate to true?
(x || !y) && (!x || z)

Substitute in the values of x, y, and z:
(true || !false) && (!true || true)
Flip the negated values:
(true || true) && (false || true)
Replace the ORed statements (if one side is true, the whole statement is true):
true && true
Replace the ANDed statement (if both sides are true, the whole statement is true):
true

True or False is always True. true || false
True and True is always True. true && true

X is true in the first grouping causing the first grouping to be true. Z is true in the second grouping causing the second grouping to be true. Therefore group 1 and group 2 are true.

(x || !y) && (!x || z)
= (T || !F) && (!T || T) <-- plug in x = T, y = F, z = T
= (T || T) && (F || T) <-- !F = T, !T = F
= T && T <- T || T = T, F || T = T
= T <- T && T = T
Actually, please tell us what's so confusing; I am slightly confused that you find it confusing at all.

Related

Rewrite Boolean Expression without Parentheses

I have a black box processing system that accepts boolean expressions but does not support parentheses. I want to input a boolean expression like this:
A && (B || C)
However, when I input it as:
A && B || C
it evaluates expressions as if I had entered:
(A && B) || C
Is there any way to rewrite my expression to get the desired behavior of A && (B || C) without parentheses? Thanks!
There's a relation between precedence but, you can use associative and distributive laws to resolve this.
A && B || A && C
Truth tables match.
Be careful when implementing this. Not all compilers and languages use the same precedence and evaluation order.
Edit: No left-right precedence, sorry.
Use the distributive law and the precedence of && to check if either both A and B is true or both A and C is true.
Solution
A && B || A && C
Distributive Law
x ^ (y v z) = (x ^ y) v (x ^ z)
x && (y || z) = (x && y) || (x && z)
function original(A, B, C) {
return A && (B || C);
}
function mySolution(A, B, C) {
return A && B || C && A;
}
console.log(original(true, true, true) == mySolution(true, true, true));
console.log(original(true, true, false) == mySolution(true, true, false));
console.log(original(true, false, true) == mySolution(true, false, true));
console.log(original(true, false, false) == mySolution(true, false, false));
console.log(original(false, true, true) == mySolution(false, true, true));
console.log(original(false, true, false) == mySolution(false, true, false));
console.log(original(false, false, true) == mySolution(false, false, true));
console.log(original(false, false, false) == mySolution(false, false, false));

Simplifying a boolean expression with 4 variables

I can't figure this one out, I'm going insane looking through my textbook.
The expression is: (!w && !x && !y && z) || (w && !x && !y && z) || (x && z) || (x && y && !z)
Using distributive and compliment laws, I have narrowed it down to:
(!x && !y && z) || (x && z) || (x && y && !z)
But I can't narrow it any further! The answer is supposed to be (x && y) || ( !y && z) but I can't figure out what law would put me there. My teacher told me to use distributive but wouldn't help me further. That doesn't make any sense since there's nothing I can factor out and get rid of. What law am I supposed to use next?
You can expand xz to x(y + !y)z => xyz + x!yz.
Now you have !x!yz + xyz + x!yz + xy!z.
Then !x!yz + x!yz simplifies to !yz, and xyz + xy!z simplifies to xy,
giving the minimal expression.

Logical operator outside parentheses

I can't figure out what the logical operator "not" (~/!), does in front of a parenthesis.
Here is the task:
I have a = 1, b = 2 and c = 2.
and this if statement:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
What does it mean having a logical operator "not" before the parenthesis (look at the bold ~)?
It plays the role of a logic-negation. It'll return the logical negation of (a ~= b && a ~= c && b ~= c). Meaning if (a ~= b && a ~= c && b ~= c) is true, then ~(a ~= b && a ~= c && b ~= c) is false. and vice versa.
not(A) is equivalent to ~A. The symbol ~ acts like the not(A) logical operator if A is a logical expression. The same symbol ~ placed before = acts like the ne(A) relational operator.
Matlab employs short-circuiting behavior when dealing with logical operators && and ||. Meaning that if we are evaluating e.g expr1 && expr2, then expr2 is not evaluated if expr1 is logical 0 (false). Same way happens when we evaluate e.g expr1 || expr2, expr2 is not evaluated if expr1 is logical 1 (true).
The symbols | and || perform different operations in a MATLAB application. The symbol | is an element-wise OR operator. The short-circuit OR operator is ||. meaning that if we have e.g expr1 || expr2, both expr1 and expr2 are evaluated. Same thing happens with the & element-wise AND operator.
Now, let's have a look at the expression you asked for:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
Knowing that: a = 1, b = 2 and c = 2.
expr1 = (a ~= b | a ~= c)
= (1 ~= 2 | 1 ~= 2)
= 1 (true)
expr1 is true, so Matlab proceeds to evaluate expr2:
expr2 = ~(a ~= b && a ~= c && b ~= c)
= ~(1 ~= 2 && a ~= c && b ~= c)
= ~(1 && 1 ~= 2 && b ~= c)
= ~(1 && 1 && 2 ~= 2)
= ~(1 && 1 && 0)
= ~(0)
= 1 (true)
So the final result of expr1 && expr2 is 1 (true).
Hope it's useful!
I'll try to complement the Academia's answer - the evaluation order is important and every logical operator has it's precedence. In Matlab the negation operator '~' not() has higher precedence than other logical operators. In this case the expression a ~= b && a ~= c && b ~= c will be evaluated before the negation operator as a whole because it was placed inside the parenthesis. This expression can be read as "all three variables have different values". By placing it inside the parenthesis and negating it, you get "not all three variables have different values". Without the parenthesis you'd get not(a) ~= b && a ~= c && b ~= c. Note also that comparison operators have higher precedence than &&.
Hope it helps.

Parenthesis Balancing in Scala

Here is my problem: I don't understand how the code works.
How does "())(" return false?
def balance(chars: List[Char], numOpens: Int): Boolean = {
if (chars.isEmpty) {
numOpens == 0
} else {
val h = chars.head
val n =
if (h == '(') numOpens + 1
else if (h == ')') numOpens - 1
else numOpens
if (n >= 0) balance(chars.tail, n)
else false
}
}
The lines:
if (n >= 0) balance(chars.tail, n)
else false
mean that if at any point there are any unbalanced ) characters, false will be returned immediately (n will be < 0). For the specific example you give: ())( we can follow how the value of n varies as the function works through the string:
First character - (: n -> 1, continue check using remaining characters: ))(
Second character - ): n -> 0, continue check using remaining characters: )(
Third character - ): n -> -1, else triggered - return false immediately. The fourth character is never checked.
Shadowlands's answer is correct, but I hope you'll find this more demonstrative:
Let's imagine we are on the top upper stair of infinite stairway. And there is an instruction: ( means step down (↓), ) means step up (↑) and you should ignore all other instructions.
val n =
if (h == '(') numOpens + 1 // step down
else if (h == ')') numOpens - 1 // step up
else numOpens // ignore
After the last step we have to stand on the top upper stair. Otherwise instruction is invalid.
if (chars.isEmpty) {
numOpens == 0
} else { ... }
You can't make a step up from the top upper stair, otherwise instruction is invalid.
if (n >= 0) ...
else false // there is no stairs upper than the upper one (0)
Example:
())( means ↓↑↑↓. After this part ↓↑ you'll be on the top upper stair, so you can't make step up after next command (↑) (n < 0), so the instruction is invalid.

Writing (A && C) || (B && C) conditional shorter

(E.G. In Perl) When either condition A or condition B have the same consequence
if (A){
# Consequence X
}elsif (B){
# Consequence X
}
we can write
if ( A || B ) {
# Consequence X
}
How about we have the following condition: Either when A and C are true, or B and C are true, consequence C follows.
This can be written very long:
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
My question is, is there any way to write this shorter?
Something like this:
if ( (A && C) || (B && C) )
is syntactically ok ???
Yes.
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
is the same as:
if ( (A && C) || (B && C) ){
#Consequence X
}
And this avoids evaluating C twice:
if ( (A || B) && C){
#Consequence X
}
BTW, this is more like a logical question, the logic here isn't limited to Perl.
Try the following:
if (C && (A || B)) {
}