Lesson 3.5 Popcorn Hacks and Homework

Popcorn Hack #1

Javascript Version

%%js
function A() {
    return true;  
}

function B() {
    return true;  
}

if (A()) {
    console.log("It's raining, so I must have taken an umbrella:", B());
} else {
    console.log("It's not raining, we cannot conclude anything about the umbrella.");
}

if (!B()) {
    console.log("I didn't take an umbrella, therefore it must not be raining:", !A());
} else {
    console.log("I took an umbrella, we cannot conclude anything about the rain.");

<IPython.core.display.Javascript object>

Python version

def A():
    return True  

def B():
    return True 

if A():
    print("It's raining, so I must have taken an umbrella:", B())
else:
    print("It's not raining, we cannot conclude anything about the umbrella.")

if not B():
    print("I didn't take an umbrella, therefore it must not be raining:", not A())
else:
    print("I took an umbrella, we cannot conclude anything about the rain.")

It's raining, so I must have taken an umbrella: True
I took an umbrella, we cannot conclude anything about the rain.

Hoemwork 3.5

Javascript Version

%%js
import java.util.Scanner;

public class LogicGatesSimulator {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Did Player A score? (true/false): ");
        boolean playerAScore = scanner.nextBoolean();
        System.out.print("Did Player B score? (true/false): ");
        boolean playerBScore = scanner.nextBoolean();

        System.out.println("Logic Gates Simulation Results:");
        System.out.println("AND Gate: " + (playerAScore && playerBScore));
        System.out.println("OR Gate: " + (playerAScore || playerBScore));
        System.out.println("NOT Gate A: " + (!playerAScore));
        System.out.println("NOT Gate B: " + (!playerBScore));
        System.out.println("NAND Gate: " + !(playerAScore && playerBScore));
        System.out.println("NOR Gate: " + !(playerAScore || playerBScore));
        System.out.println("XOR Gate: " + (playerAScore ^ playerBScore));

        System.out.println("\nTruth Table:");
        System.out.println("A     B     AND     OR      NOT A   NOT B   NAND    NOR     XOR");
        System.out.println("---------------------------------------------------------------");

        for (boolean a : new boolean[]{true, false}) {
            for (boolean b : new boolean[]{true, false}) {
                System.out.printf("%d   %d   %d   %d   %d    %d   %d   %d   %d%n",
                        (a ? 1 : 0), (b ? 1 : 0),
                        (a && b ? 1 : 0), (a || b ? 1 : 0),
                        (a

<IPython.core.display.Javascript object>

Python Version

def logic_gates_simulator(player_a, player_b):
    print("Player A:", player_a)
    print("Player B:", player_b)
    print("Logic Gates Simulation Results:")
    print("AND Gate:         ", player_a and player_b)
    print("OR Gate:          ", player_a or player_b)
    print("NOT Gate A:      ", not player_a)
    print("NOT Gate B:      ", not player_b)
    print("NAND Gate:       ", not (player_a and player_b))
    print("NOR Gate:        ", not (player_a or player_b))
    print("XOR Gate:        ", player_a ^ player_b)
    
    print("\nTruth Table:")
    print("A     B     AND     OR      NOT A   NOT B   NAND    NOR     XOR")
    print("---------------------------------------------------------------")
    for a in [True, False]:
        for b in [True, False]:
            print(f"{int(a)}   {int(b)}   {int(a and b)}   {int(a or b)}   {int(not a)}    {int(not b)}   {int(not (a and b))}   {int(not (a or b))}   {int(a ^ b)}")

player_a_score = True  
player_b_score = False  

logic_gates_simulator(player_a_score, player_b_score)
Player A: True
Player B: False
Logic Gates Simulation Results:
AND Gate:          False
OR Gate:           True
NOT Gate A:       False
NOT Gate B:       True
NAND Gate:        True
NOR Gate:         False
XOR Gate:         True

Truth Table:
A     B     AND     OR      NOT A   NOT B   NAND    NOR     XOR
---------------------------------------------------------------
1   1   1   1   0    0   0   0   0
1   0   0   1   0    1   1   0   1
0   1   0   1   1    0   1   0   1
0   0   0   0   1    1   1   1   0