Big Idea 3 Section 5

Popcorn Hack in Python

# It is hot, so the ground would be dry.

def A():
    return True

def B():
    return True

if A():
    print("It is hot, so the ground must also be dry:", B())
else:
    print("It is not hot, we cannot conclude anything about the ground being dry.")

if not B():
    print("It is not hot, therefore the ground must also be dry:", not A())
else:
    print("The ground is dry, we cannot conclude anything about it being hot.")
It is hot, so the ground must also be dry: True
The ground is dry, we cannot conclude anything about it being hot.

Popcorn Hack in Javascript

%%js

function A() {
    return true;
}

function B() {
    return true;
}

function main() {
    if (A()) {
        console.log("It is hot, so the ground must also be dry:" + B());
    } else {
        console.log("It is not hot, we cannot conclude anything about the ground being dry.");
    }
    if (!B()) {
        console.log("It is not hot, therefore the ground must also be dry:" + !A());
    } else {
        console.log("The ground is dry, we cannot conclude anything about it being hot.");
    }
}
main();
<IPython.core.display.Javascript object>

Homework in Python

def AND(A, B):
    return A and B

def OR(A, B):
    return A or B

def NOT(A):
    return not A

print("A     B | AND | OR | NOT A")
print("---------------------------")
for A in [True, False]:
    for B in [True, False]:
        print(f"{A:<5} {B:<5} | {AND(A, B):<4} | {OR(A, B):<3} | {NOT(A)}")
A     B | AND | OR | NOT A
---------------------------
1     1     | 1    | 1   | False
1     0     | 0    | 1   | False
0     1     | 0    | 1   | True
0     0     | 0    | 0   | True

Homework in Javascript

%%js

function AND(A, B) {
    return A && B;
}
function OR(A, B) {
    return A || B;
}
function NOT(A) {
    return !A;
}

console.log("A     B | AND | OR  | NOT A");
console.log("----------------------------");

let values = [true, false];
for (let A of values) {
    for (let B of values) {
        console.log(`${A}  ${B} | ${AND(A, B)}  | ${OR(A, B)}  | ${NOT(A)}`);
    }
}

<IPython.core.display.Javascript object>