truth table: Binary Operations part 3


requirements

Binary Operations part 2


test_exclusive_disjunction

red: make it fail

I add a new test

def test_logical_disjunction(self):
    ...

def test_exclusive_disjunction(self):
    self.assertFalse(src.truth_table.exclusive_disjunction(True, True))

the terminal shows AttributeError

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

green: make it pass

I add the function

def logical_disjunction(p, q):
    return p or q


def exclusive_disjunction(p, q):
    return False

the test passes

refactor: make it better

  • I add the next case

    def test_exclusive_disjunction(self):
        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add an if statement

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

    the test is green again

  • I add the third case

    def test_exclusive_disjunction(self):
        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
        self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add another if statement

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

    the test is green

  • I add the last case

    def test_exclusive_disjunction(self):
        self.assertFalse(src.truth_table.exclusive_disjunction(True, True))
        self.assertTrue(src.truth_table.exclusive_disjunction(True, False))
        self.assertTrue(src.truth_table.exclusive_disjunction(False, True))
        self.assertFalse(src.truth_table.exclusive_disjunction(False, False))
    

    the test is still green

  • I can use Logical Disjunction to put the two if statements that return True together

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

    the test is still green. I remove the other if statements then use a simple return statement

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

    the terminal still shows green. I remove the other statements

    def exclusive_disjunction(p, q):
        return (not p and q) or (p and not q)
    
  • This function returns False in the 2 cases where p and q are the same, I can use this if statement

    def exclusive_disjunction(p, q):
        if p == q:
            return False
        else:
            return True
        return (not p and q) or (p and not q)
    

    I can change it to a simple return statement

    def exclusive_disjunction(p, q):
        return not (p == q)
        if p == q:
            return False
        else:
            return True
        return (not p and q) or (p and not q)
    

    there is an even simpler statement

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

    != is the symbol for NOT equal


test_material_non_implication

red: make it fail

I add another test

def test_exclusive_disjunction(self):
    ...

def test_material_non_implication(self):
    self.assertFalse(src.truth_table.material_non_implication(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'material_non_implication'. Did you mean: 'converse_non_implication'?

green: make it pass

I add a function

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


def material_non_implication(p, q):
    return False

the test passes

refactor: make it better

  • I add another case

    def test_material_non_implication(self):
        self.assertFalse(src.truth_table.material_non_implication(True, True))
        self.assertTrue(src.truth_table.material_non_implication(True, False))
    

    the terminal shows AssertionError

    AssertionError: False is not true
    

    I add an if statement

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

    the test passes

  • I add the next case

    def test_material_non_implication(self):
        self.assertFalse(src.truth_table.material_non_implication(True, True))
        self.assertTrue(src.truth_table.material_non_implication(True, False))
        self.assertFalse(src.truth_table.material_non_implication(False, True))
    

    the test is still green

  • I add the fourth case

    def test_material_non_implication(self):
        self.assertFalse(src.truth_table.material_non_implication(True, True))
        self.assertTrue(src.truth_table.material_non_implication(True, False))
        self.assertFalse(src.truth_table.material_non_implication(False, True))
        self.assertFalse(src.truth_table.material_non_implication(False, False))
    

    the terminal still shows green

  • there is only one case where the result is True, I add a return statement for it

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

    the test is still green. I remove the other statements

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

test_project_first

red: make it fail

I add a test

def test_material_non_implication(self):
    ...

def test_project_first(self):
    self.assertTrue(src.truth_table.project_first(True, True))

the terminal shows AttributeError

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

green: make it pass

I add a function definition for it

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


def project_first(p, q):
    return True

the test passes

refactor: make it better

  • I add the second case

    def test_project_first(self):
        self.assertTrue(src.truth_table.project_first(True, True))
        self.assertTrue(src.truth_table.project_first(True, False))
    

    the test is still green

  • on to the next case

    def test_project_first(self):
        self.assertTrue(src.truth_table.project_first(True, True))
        self.assertTrue(src.truth_table.project_first(True, False))
        self.assertFalse(src.truth_table.project_first(False, True))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add an if statement

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

    the test passes

  • I add the last case

    def test_project_first(self):
        self.assertTrue(src.truth_table.project_first(True, True))
        self.assertTrue(src.truth_table.project_first(True, False))
        self.assertFalse(src.truth_table.project_first(False, True))
        self.assertFalse(src.truth_table.project_first(False, False))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add another if statement

    def project_first(p, q):
        if not p and not q:
            return False
        if not p and q:
            return False
        return True
    
  • I add a return statement to show that this function returns the same value as p

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

    still green. I remove the other statements

    def project_first(p, q):
        return p
    

test_converse_implication

red: make it fail

I add a new test

def test_project_first(self):
    ...

def test_converse_implication(self):
    self.assertTrue(src.truth_table.converse_implication(True, True))

the terminal shows AttributeError

AttributeError: module 'src.truth_table' has no attribute 'converse_implication'. Did you mean: 'converse_non_implication'?

green: make it pass

I add a function definition

def project_first(p, q):
    return p


def converse_implication(p, q):
    return True

the test passes

refactor: make it better

  • I add the second case

    def test_converse_implication(self):
        self.assertTrue(src.truth_table.converse_implication(True, True))
        self.assertTrue(src.truth_table.converse_implication(True, False))
    

    the test is still green

  • time for the next case

    def test_converse_implication(self):
        self.assertTrue(src.truth_table.converse_implication(True, True))
        self.assertTrue(src.truth_table.converse_implication(True, False))
        self.assertFalse(src.truth_table.converse_implication(False, True))
    

    the terminal shows AssertionError

    AssertionError: True is not false
    

    I add an if statement

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

    the test passes

  • I add another case

    def test_converse_implication(self):
        self.assertTrue(src.truth_table.converse_implication(True, True))
        self.assertTrue(src.truth_table.converse_implication(True, False))
        self.assertFalse(src.truth_table.converse_implication(False, True))
        self.assertTrue(src.truth_table.converse_implication(False, False))
    

    the terminal still shows green

  • I add a return statement

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

    I “multiply not” by the symbols in the parentheses

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

    the terminal shows SyntaxError

    SyntaxError: invalid syntax
    

    I change “not and” to “or” to be correct

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

    back to green. I remove “not not” since it cancels out, the negation of a negation is the original thing

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

    all the tests are still green. I remove the other statements

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

review

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

do you want to test more binary operations?


truth table: tests and solutions