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()
예제 #2
0
 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)
예제 #3
0
 def test_negation(self):
     t = TruthTable('~A')
     t.generate()
     expected = [(0, 1), (1, 0)]
     self.assertEqual(t.table, expected)
예제 #4
0
 def test_false(self):
     t = TruthTable('A & ~A')
     t.generate()
     expected = [(0, 0), (1, 0)]
     self.assertEqual(t.table, expected)
예제 #5
0
class Identity(UnaryOperation):
    symbol = ""

    join = TruthTable(1, {(F, ): F, (T, ): T})
예제 #6
0
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.')
예제 #7
0
 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)
예제 #8
0
 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)
예제 #9
0
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')
예제 #10
0
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()
예제 #11
0
class Implication(BinaryOperation):
    symbol = "->"

    join = TruthTable(2, {(F, F): T, (F, T): T, (T, F): F, (T, T): T}, symbol)
예제 #12
0
class ExclDisjunction(BinaryOperation):
    symbol = "+"

    join = TruthTable(2, {(F, F): F, (F, T): T, (T, F): T, (T, T): F}, symbol)
예제 #13
0
class Conjunction(BinaryOperation):
    symbol = "&"

    join = TruthTable(2, {(F, F): F, (F, T): F, (T, F): F, (T, T): T}, symbol)
예제 #14
0
class Negation(UnaryOperation):
    symbol = "~"

    join = TruthTable(1, {(F, ): T, (T, ): F}, symbol)
예제 #15
0
 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)
예제 #16
0
 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)
예제 #17
0
 def truth(self) -> TruthTable:
     return TruthTable((Input(self.value, {self.value}), ),
                       {(self.value, ): self.value}, str(self))
예제 #18
0
 def test_identity(self):
     t = TruthTable('A')
     t.generate()
     expected = [(0, 0), (1, 1)]
     self.assertEqual(t.table, expected)
예제 #19
0
 def truth(self) -> TruthTable:
     return TruthTable((Input(self), ), {(F, ): F, (T, ): T}, str(self))
예제 #20
0
 def test_pretty_printing(self):
     t = TruthTable('A|B&~A<->C->( ~ A     |C )')
     self.assertEqual(t.formula, 'A | B & ~A <-> C -> (~A | C)')
예제 #21
0
 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)
예제 #22
0
 def test_true(self):
     t = TruthTable('A | ~A')
     t.generate()
     expected = [(0, 1), (1, 1)]
     self.assertEqual(t.table, expected)
예제 #23
0
class Biconditional(BinaryOperation):
    symbol = "<->"

    join = TruthTable(2, {(F, F): T, (F, T): F, (T, F): F, (T, T): T}, symbol)