truth table: Binary Operations IV¶
requirements¶
how to make a python test driven development environment with truth_table
as the name of the project
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
then 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 another if statement
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
the function returns True when
q
is False, I write another return statementdef 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 terminal still shows 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))
and 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
I add a return statement
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
def logical_nor(p, q): return not (p and q) return not p and not q
the terminal shows AssertionError
AssertionError: True is not false
I made a mistake, the return statement is logical nand. I multiply the statement to show the mistake
def logical_nor(p, q): return not (p and q) return not p not and not q return not p and not q
the terminal shows SyntaxError
SyntaxError: invalid syntax
not and is or, I should have used not or, I change the return statement
def logical_nor(p, q): return not (p or q) # return not p not or not q return not p and not q
green again, 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))
and the test is still green
This test expects True in 2 of the cases and False in the other 2. 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 else: return True if not p and q: return False if p and not q: return False return True
still green. I use not to write the 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 else: 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 fix the line
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
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 statements
def logical_equality(p, q): return (p or not q) and (not p or q)
and all tests are still passing.
All of this could have been done with the
==
symbol, since the 2 cases where True is the result are cases wherep
andq
are the samedef logical_equality(p, q): return p == q return (p or not q) and (not p or q)
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
I add a return statement with not
def material_implication(p, q): return not (p and not q) if p and not q: return False return True
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 fix the line
def material_implication(p, q): return not p or not not q return not (p and not q)
the test is passing 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 which could be True or False, if we name the first input p
and the second q
, the tests show that
Logical or Material Implication returns
not p or q
Logical Equality returns
p == q
Logical NOR returns
not (p or q)
Negate Second always returns
not q
Converse Implication returns
p or not q
Project First always returns
p
Material NonImplication returns
p and not q
Exclusive Disjunction returns
p != q
Logical Disjunction returns
p or q
Logical NAND returns
not (p and q)
Negate First always returns
not p
Converse NonImplication returns
not p and q
Project Second always returns
q
Logical Conjunction returns
p and q
Contradiction always returns False
Would you like to test the truth table tests?