truth table: Binary Operations III


requirements

how to make a python test driven development environment with truth_table as the name of the project


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 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))
        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))
    

    and the terminal still shows green

  • I use or to make one if statement for the 2 that return True

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

    the test is still green. I 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
        else:
            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 True in the 2 cases when p and q are NOT the same, it could also be written as

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

    which I can change to a 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)
    

    or

    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 last 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 the simple return statement

    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 new 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 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))
    

    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 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))
        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
    
  • the function returns the same value as p. I add another return statement

    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 to truth_table.py

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 the last 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 fix the line, not and is or

    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

    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 which could be True or False, if we name the first input p and the second q, the tests show that

do you want to test more binary operations?


truth table: tests and solutions