Skip to the content.

3.5_ipynb_2_

Popcorn Hacks

#Popcorn hack 1
def study_and_pass(study, pass_exam):
    # Original statement: If study then pass_exam
    if study:
        print("You studied. You will pass the exam.")
    else:
        print("You didn't study. We cannot conclude about passing the exam.")

    # Contrapositive: If not pass_exam then not study
    if not pass_exam:
        print("You did not pass the exam. Therefore, you did not study.")
    else:
        print("You passed the exam. We cannot conclude if you studied or not.")

# Example usage
study = True  # Change this to simulate studying
pass_exam = False  # Change this to simulate passing the exam

study_and_pass(study, pass_exam)
%%js
// Popcorn hack 1
import java.util.Scanner;

public class ContrapositiveLaw {

    public static void studyAndPass(boolean study, boolean passExam) {
        // Original statement: If study then pass_exam
        if (study) {
            System.out.println("You studied. You will pass the exam.");
        } else {
            System.out.println("You didn't study. We cannot conclude about passing the exam.");
        }

        // Contrapositive: If not pass_exam then not study
        if (!passExam) {
            System.out.println("You did not pass the exam. Therefore, you did not study.");
        } else {
            System.out.println("You passed the exam. We cannot conclude if you studied or not.");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // User input for study and pass_exam
        System.out.print("Did you study? (true/false): ");
        boolean study = scanner.nextBoolean();

        System.out.print("Did you pass the exam? (true/false): ");
        boolean passExam = scanner.nextBoolean();

        // Call the function to demonstrate contrapositive's law
        studyAndPass(study, passExam);

        scanner.close();
    }
}

Homework Hack

%%js
// Homework hack 1
function andGate(a, b) {
    return a && b;
}

function orGate(a, b) {
    return a || b;
}

function notGate(a) {
    return !a;
}

function nandGate(a, b) {
    return !(a && b);
}

function norGate(a, b) {
    return !(a || b);
}

function xorGate(a, b) {
    return a ^ b;
}

function printTruthTable() {
    console.log("A B | AND | OR  | NOT A | NAND | NOR | XOR");
    console.log("-".repeat(37));
    for (let a of [0, 1]) {
        for (let b of [0, 1]) {
            console.log(`${a} ${b} |  ${andGate(a, b)}  |  ${orGate(a, b)}  |   ${notGate(a)}   |  ${nandGate(a, b)}  |  ${norGate(a, b)}  |  ${xorGate(a, b)}`);
        }
    }
}

// Call the function to print the truth table
printTruthTable();
#Homework hack 1
def and_gate(a, b):
    return a and b

def or_gate(a, b):
    return a or b

def not_gate(a):
    return not a

def nand_gate(a, b):
    return not (a and b)

def nor_gate(a, b):
    return not (a or b)

def xor_gate(a, b):
    return a ^ b

def print_truth_table():
    print("A B | AND | OR  | NOT A | NAND | NOR | XOR")
    print("-" * 37)
    for a in [0, 1]:
        for b in [0, 1]:
            print(f"{a} {b} |  {and_gate(a, b)}  |  {or_gate(a, b)}  |   {not_gate(a)}   |  {nand_gate(a, b)}  |  {nor_gate(a, b)}  |  {xor_gate(a, b)}")

if __name__ == "__main__":
    print_truth_table()