-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp47.py
More file actions
118 lines (104 loc) · 2.91 KB
/
p47.py
File metadata and controls
118 lines (104 loc) · 2.91 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
#!/usr/bin/env python3
import p46
def lexer(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":
func.append("A")
elif expr[i] == "B":
func.append("B")
elif expr[i:i+2] == "or":
func.append("or")
i += 1
elif expr[i:i+3] == "and":
func.append("and")
i += 2
elif expr[i:i+3] == "not":
func.append("not")
i += 2
elif expr[i:i+3] == "nor":
func.append("nor")
i += 2
elif expr[i:i+3] == "xor":
func.append("xor")
i += 2
elif expr[i:i+3] == "equ":
func.append("equ")
i += 2
elif expr[i:i+4] == "nand":
func.append("nand")
i += 3
elif expr[i:i+4] == "impl":
func.append("impl")
i += 3
else:
print("Unknown Character!")
i += 1
return func
def mover(func):
calc = []
i = 0
while i < len(func):
if func[i] == "(":
if func[i+1] == "not":
calc.append(func[i+1])
calc.append(func[i])
calc.append(func[i+2])
calc.append(")")
else:
calc.append(func[i+2])
calc.append(func[i])
calc.append(func[i+1])
calc.append(",")
elif func[i] == ")":
if func[i-2] == "not":
... # do nothing
else:
calc.append(")")
i += 1
return calc
def combine_list_to_str(calc):
expr = ""
for item in calc:
expr += item
return expr
# P47: Evaluates logical expressions (2).
# 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 p47(a, b, expr):
#print(expr)
func = lexer(expr)
#print(func)
calc = mover(func)
#print(calc)
expression = combine_list_to_str(calc)
#print(expression)
ret = p46.p46(a, b, expression)
return ret
# P47: Truth table for logic expression (2).
# 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 p47tt(expr):
print("---- " + str(expr) + " ----")
print("a b x")
a, b = 0, 0
x = p47(a, b, expr)
print(a, b, x)
a, b = 0, 1
x = p47(a, b, expr)
print(a, b, x)
a, b = 1, 0
x = p47(a, b, expr)
print(a, b, x)
a, b = 1, 1
x = p47(a, b, expr)
print(a, b, x)
if __name__ == "__main__":
p47tt("(A and (A or (not B)))")