def loop(): while True: try: line = input(prompt) except (EOFError, KeyboardInterrupt): print() print('Goodbye!') break line = line.strip() if not line: continue try: expr = parse(line) except ParseException as ex: print(ex) continue table = TruthTable(expr) table.display_table()
def test_disjunction(self): t = TruthTable('A | B') t.generate() expected = [(0, 0, 0), (0, 1, 1), (1, 0, 1), (1, 1, 1)] self.assertEqual(t.table, expected)
def test_negation(self): t = TruthTable('~A') t.generate() expected = [(0, 1), (1, 0)] self.assertEqual(t.table, expected)
def test_false(self): t = TruthTable('A & ~A') t.generate() expected = [(0, 0), (1, 0)] self.assertEqual(t.table, expected)
class Identity(UnaryOperation): symbol = "" join = TruthTable(1, {(F, ): F, (T, ): T})
from kit103_assign2 import q4_acme_letter_detector from truthtable import TruthTable def q4_original(a, b, c, d): return \ (not a and not b and not c and d) or \ (not a and not b and c and d) or \ (not a and b and not c and not d) or \ (not a and b and c and d) or \ (a and not b and c and d) or \ (a and b and c and d) original = TruthTable(4, q4_original) answer = TruthTable(4, q4_acme_letter_detector) original.print(answer, labels=('original', 'optimised')) if original == answer: print('Success! The answer matches the original predicate.') else: print('Whoops! The answer does not match the original predicate.')
def test_braces(self): t = TruthTable('(((A <-> B) -> A) | B) & ~A') t.generate() expected = [(0, 0, 0), (0, 1, 1), (1, 0, 0), (1, 1, 0)] self.assertEqual(t.table, expected)
def test_operation_precedence(self): t = TruthTable('A <-> B -> A | B & ~A') t.generate() expected = [(0, 0, 0), (0, 1, 0), (1, 0, 1), (1, 1, 1)] self.assertEqual(t.table, expected)
from truthtable import TruthTable from sys import argv import fileinput lines = [] inp = fileinput.input(argv[1]) if len(argv) > 1 else fileinput.input() for line in inp: lines.append(line) lines = (x.strip().upper() for x in lines) lines = (x for x in lines if x) for line in lines: t = TruthTable(line) t.generate() print(t, '\n')
import kit103_assign2 as assign2 from truthtable import TruthTable questions = {'a': 2, 'b': 4, 'c': 3, 'd': 1} for q, var_count in questions.items(): print() predicate1 = getattr(assign2, 'q2_' + q) predicate2 = getattr(assign2, 'q3_' + q) result1 = TruthTable(var_count, predicate1) result2 = TruthTable(var_count, predicate2) result1.print(result2, labels=('2.' + q, '3.' + q)) out1, out2 = ('Pass', 'identical') if result1 == result2 else ('Fail', 'different') print( f'{out1}ed: Question {q.upper()} has {out2} results for the two predicates.' ) print()
class Implication(BinaryOperation): symbol = "->" join = TruthTable(2, {(F, F): T, (F, T): T, (T, F): F, (T, T): T}, symbol)
class ExclDisjunction(BinaryOperation): symbol = "+" join = TruthTable(2, {(F, F): F, (F, T): T, (T, F): T, (T, T): F}, symbol)
class Conjunction(BinaryOperation): symbol = "&" join = TruthTable(2, {(F, F): F, (F, T): F, (T, F): F, (T, T): T}, symbol)
class Negation(UnaryOperation): symbol = "~" join = TruthTable(1, {(F, ): T, (T, ): F}, symbol)
def test_implication(self): t = TruthTable('A -> B') t.generate() expected = [(0, 0, 1), (0, 1, 1), (1, 0, 0), (1, 1, 1)] self.assertEqual(t.table, expected)
def test_equality(self): t = TruthTable('A <-> B') t.generate() expected = [(0, 0, 1), (0, 1, 0), (1, 0, 0), (1, 1, 1)] self.assertEqual(t.table, expected)
def truth(self) -> TruthTable: return TruthTable((Input(self.value, {self.value}), ), {(self.value, ): self.value}, str(self))
def test_identity(self): t = TruthTable('A') t.generate() expected = [(0, 0), (1, 1)] self.assertEqual(t.table, expected)
def truth(self) -> TruthTable: return TruthTable((Input(self), ), {(F, ): F, (T, ): T}, str(self))
def test_pretty_printing(self): t = TruthTable('A|B&~A<->C->( ~ A |C )') self.assertEqual(t.formula, 'A | B & ~A <-> C -> (~A | C)')
def draw_truthtable(self, expr): truthtable = TruthTable(expr, "Y", size=self.board.board_canvas.size) self.board.board_canvas.clear_widgets() self.board.board_canvas.add_widget(truthtable)
def test_true(self): t = TruthTable('A | ~A') t.generate() expected = [(0, 1), (1, 1)] self.assertEqual(t.table, expected)
class Biconditional(BinaryOperation): symbol = "<->" join = TruthTable(2, {(F, F): T, (F, T): F, (T, F): F, (T, T): T}, symbol)