truth table: Binary Operations part 4


requirements

Binary Operations part 3


test_negate_second

red: make it fail

I add a test

def test_converse_implication(self):
    ...

def test_negate_second(self):
    self.assertFalse(src.truth_table.negate_second(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'negate_second'

green: make it pass

I add a function definition to truth_table.py

def converse_implication(p, q):
    return p or not q


def negate_second(p, q):
    return False

the test passes

refactor: make it better

  • I add the next case

    def test_negate_second(self):
        self.assertFalse(src.truth_table.negate_second(True, True))
        self.assertTrue(src.truth_table.negate_second(True, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add an if statement

    def negate_second(p, q):
        if p and not q:
            return True
        return False
    

    the test passes

  • I add the third case

    def test_negate_second(self):
        self.assertFalse(src.truth_table.negate_second(True, True))
        self.assertTrue(src.truth_table.negate_second(True, False))
        self.assertFalse(src.truth_table.negate_second(False, True))
    

    the test is still passing

  • I add another case

    def test_negate_second(self):
        self.assertFalse(src.truth_table.negate_second(True, True))
        self.assertTrue(src.truth_table.negate_second(True, False))
        self.assertFalse(src.truth_table.negate_second(False, True))
        self.assertTrue(src.truth_table.negate_second(False, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add an if statement for the case

    def negate_second(p, q):
        if not p and not q:
            return True
        if p and not q:
            return True
        return False
    

    the test is green again

  • this function returns True in the two cases where q is False, I add a return statement

    def negate_second(p, q):
        return not q
        if not p and not q:
            return True
        if p and not q:
            return True
        return False
    

    the test is still green. I remove the other statements

    def negate_second(p, q):
        return not q
    

test_logical_nor

red: make it fail

I add another test

def test_material_non_implication(self):
    ...

def test_logical_nor(self):
    self.assertFalse(src.truth_table.logical_nor(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'logical_nor'. Did you mean: 'logical_nand'?

green: make it pass

I add a function

def negate_second(p, q):
    return not q


def logical_nor(p, q):
    return False

the test passes

refactor: make it better

  • I add the next case

    def test_logical_nor(self):
        self.assertFalse(src.truth_table.logical_nor(True, True))
        self.assertFalse(src.truth_table.logical_nor(True, False))
    

    the terminal still shows green

  • on to the next case

    def test_logical_nor(self):
        self.assertFalse(src.truth_table.logical_nor(True, True))
        self.assertFalse(src.truth_table.logical_nor(True, False))
        self.assertFalse(src.truth_table.logical_nor(False, True))
    

    the test is still green

  • I add the last case

    def test_logical_nor(self):
        self.assertFalse(src.truth_table.logical_nor(True, True))
        self.assertFalse(src.truth_table.logical_nor(True, False))
        self.assertFalse(src.truth_table.logical_nor(False, True))
        self.assertTrue(src.truth_table.logical_nor(False, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    this is the only case that returns True, I add a return statement for it

    def logical_nor(p, q):
        return not p and not q
        return False
    

    the test passes

  • I want to factor out “not” since it happens 2 times, I rewrite the statement in terms of it

    def logical_nor(p, q):
        return not p not or not q
        return not p and not q
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I comment out the line then add the correct statement

    def logical_nor(p, q):
        return not (p or q)
        # return not p not or not q
        return not p and not q
    

    still green, I remove the other statements

    def logical_nor(p, q):
        return not (p or q)
    

test_logical_equality

red: make it fail

I add a new test

def test_logical_nor(self):
    ...

def test_logical_equality(self):
    self.assertTrue(src.truth_table.logical_equality(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'logical_equality'. Did you mean: 'logical_identity'?

green: make it pass

I add a function definition

def logical_nor(p, q):
    return not (p or q)


def logical_equality(p, q):
    return True

the test passes

refactor: make it better

  • I add the next case

    def test_logical_equality(self):
        self.assertTrue(src.truth_table.logical_equality(True, True))
        self.assertFalse(src.truth_table.logical_equality(True, False))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add an if statement

    def logical_equality(p, q):
        if p and not q:
            return False
        return True
    

    the test passes

  • I add another case

    def test_logical_equality(self):
        self.assertTrue(src.truth_table.logical_equality(True, True))
        self.assertFalse(src.truth_table.logical_equality(True, False))
        self.assertFalse(src.truth_table.logical_equality(False, True))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add another if statement

    def logical_equality(p, q):
        if not p and q:
            return False
        if p and not q:
            return False
        return True
    

    the test passes

  • I add the last case

    def test_logical_equality(self):
        self.assertTrue(src.truth_table.logical_equality(True, True))
        self.assertFalse(src.truth_table.logical_equality(True, False))
        self.assertFalse(src.truth_table.logical_equality(False, True))
        self.assertTrue(src.truth_table.logical_equality(False, False))
    

    the test is still green

  • I use “or” to put the 2 statements that return False together

    def logical_equality(p, q):
        if (not p and q) or (p and not q):
            return False
        if not p and q:
            return False
        if p and not q:
            return False
        return True
    

    still green. I remove the other if statements then write a new return statement

    def logical_equality(p, q):
        return not ((not p and q) or (p and not q)):
        if (not p and q) or (p and not q):
            return False
        return True
    

    the test is still green. I “multiply not” by every symbol in the parentheses

    def logical_equality(p, q):
        return (not not p not and not q) not or (not p not and not not q)
        return not ((not p and q) or (p and not q):)
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I change “not and” to “or” in both parentheses and “not or” to “and” in between them

    def logical_equality(p, q):
        return (not not p or not q) and (not p or not not q)
        return not ((not p and q) or (p and not q))
    

    the test is green again. I remove “not not” from both parentheses

    def logical_equality(p, q):
        return (p or not q) and (not p or q)
        return not ((not p and q) or (p and not q))
    

    the terminal shows green. I remove the other line

    def logical_equality(p, q):
        return (p or not q) and (not p or q)
    

    the test is still passing.

  • The 2 cases that return True are when p and q are the same, which means I can write a simpler return statement

    def logical_equality(p, q):
        return p == q
        return (p or not q) and (not p or q)
    

    the test is still green


test_material_implication

red: make it fail

I add a new test

def test_logical_equality(self):
    ...

def test_material_implication(self):
    self.assertTrue(src.truth_table.material_implication(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'material_implication'

green: make it pass

I add the method

def logical_equality(p, q):
    return p == q
    return (p or not q) and (not p or q)


def material_implication(p, q):
    return True

the test passes

refactor: make it better

  • I add the next case

    def test_material_implication(self):
        self.assertTrue(src.truth_table.material_implication(True, True))
        self.assertFalse(src.truth_table.material_implication(True, False))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add an if statement

    def material_implication(p, q):
        if p and not q:
            return False
        return True
    

    the terminal shows green

  • I add the next case

    def test_material_implication(self):
        self.assertTrue(src.truth_table.material_implication(True, True))
        self.assertFalse(src.truth_table.material_implication(True, False))
        self.assertTrue(src.truth_table.material_implication(False, True))
    

    the test is still green

  • I add the fourth case

    def test_material_implication(self):
        self.assertTrue(src.truth_table.material_implication(True, True))
        self.assertFalse(src.truth_table.material_implication(True, False))
        self.assertTrue(src.truth_table.material_implication(False, True))
        self.assertTrue(src.truth_table.material_implication(False, False))
    

    the test is still passing

  • there is only one case where this function returns False, I add a return statement for it

    def material_implication(p, q):
        return not (p and not q)
        if p and not q:
            return False
        return True
    

    the test is still green, I “multiply not” by the symbols in the parentheses

    def material_implication(p, q):
        return not p not and not not q
        return not (p and not q)
    

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I change “not and” to “or

    def material_implication(p, q):
        return not p or not not q
        return not (p and not q)
    

    the test is green again. I remove “not not

    def material_implication(p, q):
        return not p or q
        return not (p and not q)
    

    all tests are still passing. I remove the other statement

    def material_implication(p, q):
        return not p or q
    

review

Binary Operations take 2 inputs, each of the inputs can be True or False, if we name the first input p and the second one q, the tests show that

Would you like to test the truth table tests?


truth table: tests and solutions