Popcorn Hack

Look up Contrapositive Law. What is it, and how can it apply to your code? Make a blog post about it.

Contrapositive’s Law in Python

def A():
    return True  # Condition A

def B():
    return True  # Condition B

# Original statement: if A then B
if A():
    print("A is true, so B must also be true:", B())
else:
    print("A is false, we cannot conclude anything about B.")

# Contrapositive: if not B then not A
if not B():
    print("B is false, therefore A must also be false:", not A())
else:
    print("B is true, we cannot conclude anything about A.")

A is true, so B must also be true: True
B is true, we cannot conclude anything about A.

Contrapostive’s Law in Java

%%javascript

public class ContrapositiveExample {
    
    public static boolean A() {
        return true; // Condition A
    }
    
    public static boolean B() {
        return true; // Condition B
    }
    
    public static void main(String[] args) {
        // Original statement: if A then B
        if (A()) {
            System.out.println("A is true, so B must also be true: " + B());
        } else {
            System.out.println("A is false, we cannot conclude anything about B.");
        }
        
        // Contrapositive: if not B then not A
        if (!B()) {
            System.out.println("B is false, therefore A must also be false: " + !A());
        } else {
            System.out.println("B is true, we cannot conclude anything about A.");
        }
    }
}

<IPython.core.display.Javascript object>