-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogic.py
112 lines (82 loc) · 3.19 KB
/
logic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import itertools
class LogicTables:
def __init__(self):
self.variables = set()
self.expressions = []
def add_variable(self, variable):
self.variables.add(variable)
def add_expression(self, expression):
self.expressions.append(expression)
def evaluate_expression(self, expression, values):
for var, val in values.items():
expression = expression.replace(var, str(val))
return eval(expression)
def generate_truth_table(self):
if not self.variables or not self.expressions:
return "No variables or expressions to evaluate."
headers = list(self.variables) + self.expressions
table = [headers]
for values in itertools.product([True, False], repeat=len(self.variables)):
row = list(values)
value_dict = dict(zip(self.variables, values))
for expression in self.expressions:
row.append(self.evaluate_expression(expression, value_dict))
table.append(row)
return table
def display_truth_table(self):
table = self.generate_truth_table()
for row in table:
print("\t".join(map(str, row)))
def main():
if __name__ == '__main__':
logic = LogicTables()
logic.add_variable('P')
logic.add_variable('Q')
logic.add_expression('P and Q')
logic.add_expression('P or Q')
logic.add_expression('not P')
logic.display_truth_table()
if __name__ == '__main__':
main()
# Improvements from improved_logic.py Start
import itertools
class LogicTables:
def __init__(self):
self.variables = set()
self.expressions = []
def add_variable(self, variable):
self.variables.add(variable)
def add_expression(self, expression):
self.expressions.append(expression)
def evaluate_expression(self, expression, values):
for var, val in values.items():
expression = expression.replace(var, str(val))
return eval(expression)
def generate_truth_table(self):
if not self.variables or not self.expressions:
return "No variables or expressions to evaluate."
headers = list(self.variables) + self.expressions
table = [headers]
for values in itertools.product([True, False], repeat=len(self.variables)):
row = list(values)
value_dict = dict(zip(self.variables, values))
for expression in self.expressions:
row.append(self.evaluate_expression(expression, value_dict))
table.append(row)
return table
def display_truth_table(self):
table = self.generate_truth_table()
for row in table:
print("\t".join(map(str, row)))
def main():
if __name__ == '__main__':
logic = LogicTables()
logic.add_variable('P')
logic.add_variable('Q')
logic.add_expression('P and Q')
logic.add_expression('P or Q')
logic.add_expression('not P')
logic.display_truth_table()
if __name__ == '__main__':
main()
# Improvements from improved_logic.py End