![]() |
Home | Accounts | Setup | Verify | Play | Hacks |
3.5 Boolean Expressions all Hacks
all Hacks for Boolean Expressions
Popcorn Hack #1
def Eating():
return True # Condition A
def Drinking_water():
return True # Condition B
# Original statement: if Eating then Drinking_water
if Eating():
print("A is true, so B must also be true:", Drinking_water())
else:
print("If you do not get food, you dont get a drink.")
# Contrapositive: if not Eating then not Drinking_Water
if not Drinking_water():
print("You cant drink water, so you cannot get food:", not Eating())
else:
print("You can drink water, you can get food.")
A is true, so B must also be true: True
You can drink water, you can get food.
Homework Hack #1
def AND(a, b):
return a and b
def OR(a, b):
return a or b
def NOT(a):
return not a
def NAND(a, b):
return not (a and b)
def NOR(a, b):
return not (a or b)
def XOR(a, b):
return a != b
def display_truth_table():
print("Truth Table for Logic Gates")
print("A\tB\tAND\tOR\tNAND\tNOR\tXOR\tNOT A\tNOT B")
for a in [0, 1]:
for b in [0, 1]:
print(f"{a}\t{b}\t{int(AND(a, b))}\t{int(OR(a, b))}\t{int(NAND(a, b))}\t{int(NOR(a, b))}\t{int(XOR(a, b))}\t{int(NOT(a))}\t{int(NOT(b))}")
display_truth_table()
<IPython.core.display.Javascript object>
%%js
function AND(a, b) {
return a && b;
}
function OR(a, b) {
return a || b;
}
function NOT(a) {
return !a;
}
function NAND(a, b) {
return !(a && b);
}
function NOR(a, b) {
return !(a || b);
}
function XOR(a, b) {
return a != b;
}
function displayTruthTable() {
console.log("A\tB\tAND\tOR\tNAND\tNOR\tXOR\tNOT A\tNOT B");
[0, 1].forEach(a => {
[0, 1].forEach(b => {
console.log(
`${a}\t${b}\t${AND(a, b)}\t${OR(a, b)}\t${NAND(a, b)}\t${NOR(a, b)}\t${XOR(a, b)}\t${NOT(a)}\t${NOT(b)}`
);
});
});
}
displayTruthTable();