-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
180 lines (136 loc) · 4.6 KB
/
solver.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from itertools import product
from collections import OrderedDict
from tabulate import tabulate
import sys
import pprint
pp = pprint.PrettyPrinter(indent=2)
operators = ["=", ">", "+", "&", "~", "(", ")"] # do not change the order
def check_precendence(c):
if c in operators[:5]:
return operators.index(c) + 1
else:
return 0
def gen_table(prepostion, alpha_order=False):
values = OrderedDict()
opers = []
for p in prepostion:
if p.isalpha() and p not in opers:
opers.append(p)
if alpha_order:
opers = sorted(opers)
tf = list(product([True, False], repeat=len(opers)))
for i, x in enumerate(opers):
values[x] = []
for e, n in enumerate(tf):
values[x].append(n[i])
return values
def peek(stack):
if stack:
return stack[len(stack) - 1]
else:
return None
def get(table, operand):
if type(operand) is list:
return operand
else:
return table.get(operand)
def scanner(prepostion):
# basic scanner that checks for errors
for i, x in enumerate(prepostion):
if not x.isalpha() and not x.isdigit() and x not in operators:
print(prepostion)
print("{:>{spaces}}".format("^", spaces=i + 1))
print("invalid operator")
exit()
elif x.isdigit():
print(prepostion)
print("{:>{spaces}}".format("^", spaces=i + 1))
print("no digits allowed in the statement")
exit()
stack = []
for x in prepostion:
if x is "(":
stack.append("(")
elif x is ")":
stack.pop()
if stack:
print("bad brackets")
exit()
def solver(prepostion, alpha_order=False):
scanner(prepostion)
def calculate():
operation = operator_stack.pop()
if operation == "~":
operand = operand_stack.pop()
l = _not(get(table, operand))
operand_stack.append(l)
elif operation == "&":
operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
l = _and(get(table, operand1), get(table, operand2))
operand_stack.append(l)
elif operation == "+":
operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
l = _or(get(table, operand1), get(table, operand2))
operand_stack.append(l)
elif operation == "=":
operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
l = biconditional(get(table, operand1), get(table, operand2))
operand_stack.append(l)
elif operation == ">":
operand2 = operand_stack.pop()
operand1 = operand_stack.pop()
l = implies(get(table, operand1), get(table, operand2))
operand_stack.append(l)
operand_stack = []
operator_stack = []
table = gen_table(prepostion, alpha_order)
for current in prepostion:
if current.isalpha():
operand_stack.append(current)
else:
if len(operator_stack) == 0:
operator_stack.append(current)
elif current == "(":
operator_stack.append(current)
elif current == ")":
while (peek(operator_stack) != "("):
calculate()
operator_stack.pop()
else:
# that means that there's already something in stack
while check_precendence(peek(operator_stack)) >= check_precendence(
current
):
calculate()
operator_stack.append(current)
while len(operator_stack) != 0:
calculate()
# print(table.get("res"))
return table, operand_stack
def _and(left, right):
return [x and y for x, y in zip(left, right)]
def _or(left, right):
return [x or y for x, y in zip(left, right)]
def implies(left, right):
return [(not x) or y for x, y in zip(left, right)]
def biconditional(left, right):
return [x == y for x, y in zip(left, right)]
def _not(table):
return [not x for x in table]
if __name__ == "__main__":
prepostion = input("Enter a prepostion: ")
prepostion = prepostion.strip()
# removing all spaces
prepostion = prepostion.replace(" ", "")
alpha_order = False
if "-a" in sys.argv:
alpha_order = True
table, operand_stack = solver(prepostion, alpha_order)
headers = list(table.keys())
headers.append(prepostion)
print()
print(tabulate(zip(*table.values(), operand_stack[0]), headers=headers))
print()