-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp48.py
More file actions
150 lines (132 loc) · 4.22 KB
/
p48.py
File metadata and controls
150 lines (132 loc) · 4.22 KB
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
#!/usr/bin/env python3
import p46
def lexer_universal(variables, expr):
func = []
i = 0
while i < len(expr):
if expr[i] == " ":
... # do nothing
elif expr[i] == "(":
func.append("(")
elif expr[i] == ")":
func.append(")")
elif (expr[i] >= "A") and (expr[i] <= "Z"):
func.append(str(variables[ord(expr[i]) - ord("A")]))
elif expr[i:i+2] == "or":
func.append("p46.f_or")
i += 1
elif expr[i:i+3] == "and":
func.append("p46.f_and")
i += 2
elif expr[i:i+3] == "not":
func.append("p46.f_not")
i += 2
elif expr[i:i+3] == "nor":
func.append("p46.f_nor")
i += 2
elif expr[i:i+3] == "xor":
func.append("p46.f_xor")
i += 2
elif expr[i:i+3] == "equ":
func.append("p46.f_equ")
i += 2
elif expr[i:i+4] == "nand":
func.append("p46.f_nand")
i += 3
elif expr[i:i+4] == "impl":
func.append("p46.f_impl")
i += 3
else:
print("Unknown Character!")
i += 1
return func
def find_matching_parentheses(func):
parentheses = []
i = 0
while i < len(func):
if func[i] == "(":
j = i + 1
count = 1
while j < len(func):
if func[j] == "(":
count += 1
elif func[j] == ")":
count -= 1
if count == 0:
parentheses.append([i, j])
break
j += 1
i += 1
return parentheses
def mover(func):
calc = []
i = 0
while i < len(func):
if func[i] == "(":
if func[i+1] == "p46.f_not":
calc.append(func[i+1])
calc.append(func[i])
calc.append(func[i+2])
calc.append(")")
i += 2
else:
calc.append(func[i+2])
calc.append(func[i])
calc.append(func[i+1])
calc.append(",")
i += 2
elif func[i] == ")":
if func[i-2] == "p46.f_not":
... # do nothing
else:
calc.append(")")
else:
calc.append(func[i])
i += 1
return calc
def combine_list_to_str(calc):
expr = ""
for item in calc:
expr += item
return expr
def evaluate(expr):
#print(expr)
if len(expr) == 1:
return expr[0]
else:
parentheses = find_matching_parentheses(expr)
if len(parentheses) == 0:
expr.insert(0, "(")
expr.append(")")
expression = mover(expr)
expr_as_string = combine_list_to_str(expression)
return int(eval(expr_as_string))
else:
par_start = parentheses[-1][0] + 1
par_end = parentheses[-1][1]
ret1 = int(evaluate(expr[par_start:par_end]))
new_expr = expr[0:par_start-1] + [str(ret1)] + expr[par_end+1:]
return int(evaluate(new_expr))
# P48: Evaluates logical expressions (3).
# You can only evaluate expressions of the form (A or B) and not (A or B or B) (at maximum two inputs per function).
# And only in the form (A and (A or B)) and not ((A or B) and A).
def p48(variables, expr):
func = lexer_universal(variables, expr)
return evaluate(func)
# P48: Truth tables for logical expressions (3).
# You can only evaluate expressions of the form (A or B) and not (A or B or B) (at maximum two inputs per function).
# And only in the form (A and (A or B)) and not ((A or B) and A).
def p48tt(variables, expr):
print("---- " + str(expr) + " ----")
for i in range(len(variables)):
print(chr(ord("A") + i) + " ", end="")
print("x")
for i in range(2**len(variables)):
args = []
for j in range(len(variables)-1, -1, -1):
value = (i >> j) & 1
args.append(value)
print(str(value) + " ", end="")
print(p48(args, expr))
if __name__ == "__main__":
p48tt([1, 0, 0], "((A and (B or C)) equ ((A and B) or (A and C)))")