-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp.py
More file actions
177 lines (149 loc) · 4.84 KB
/
Copy pathExp.py
File metadata and controls
177 lines (149 loc) · 4.84 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
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
# Expressions:
# Main object: {"LETTERS": [...], "EXP": {...}}
#
# {"AND": [{...}, {...}, ...]}
# {"OR": [{...}, {...}, ...]}
# {"NOT": {...}}
# {"VAL": letter (object) / pure value (bool)}
# ---- CONSTANTS ----
SE_TRUE = {"VAL": True}
SE_FALSE = {"VAL": False}
# ---- BASIC FUNCTIONS ----
def exp_check (exp):
if exp["EXP"] == SE_TRUE: return True
elif exp["EXP"] == SE_FALSE: return False
return None
def letter_update (exp):
new_letters = []
def expl (subexp):
if "VAL" in subexp:
if not isinstance(subexp["VAL"], bool) and subexp["VAL"] not in new_letters:
new_letters.append(subexp["VAL"])
elif "NOT" in subexp:
expl(subexp["NOT"])
elif "AND" in subexp:
for e in subexp["AND"]: expl(e)
elif "OR" in subexp:
for e in subexp["OR"]: expl(e)
expl(exp["EXP"])
return {"LETTERS": new_letters, "EXP": exp["EXP"]}
def letter_create (bexp):
return exp_simp1(letter_update({"LETTERS": [], "EXP": bexp}))
def exp_replace (exp, letter, val):
if letter not in exp["LETTERS"]: return exp
def expl (subexp):
if "VAL" in subexp:
if subexp["VAL"] == letter:
return {"VAL": val}
else: return {"VAL": subexp["VAL"]}
elif "NOT" in subexp:
return {"NOT": expl(subexp["NOT"])}
elif "AND" in subexp:
return {"AND": [expl(e) for e in subexp["AND"]]}
elif "OR" in subexp:
return {"OR": [expl(e) for e in subexp["OR"]]}
new_letters = exp["LETTERS"].copy()
new_letters.remove(letter)
return {"LETTERS": new_letters, "EXP": expl(exp["EXP"])}
def exp_replace_multiple (exp, values):
tmp = exp
for letter in values:
tmp = exp_replace(tmp, letter, values[letter])
return tmp
def exp_simp1 (exp):
def expl (subexp):
if "VAL" in subexp:
return {"VAL": subexp["VAL"]}
elif "NOT" in subexp:
a = expl(subexp["NOT"])
if a == SE_TRUE: return SE_FALSE
elif a == SE_FALSE: return SE_TRUE
else: return {"NOT": a}
elif "AND" in subexp:
tmp = []
for e in subexp["AND"]:
a = expl(e)
if a == SE_TRUE: continue
elif a == SE_FALSE: return SE_FALSE
else: tmp.append(a)
if len(tmp) == 1: return tmp[0]
elif len(tmp) == 0: return SE_TRUE
return {"AND": tmp}
elif "OR" in subexp:
tmp = []
for e in subexp["OR"]:
a = expl(e)
if a == SE_TRUE: return SE_TRUE
elif a == SE_FALSE: continue
else: tmp.append(a)
if len(tmp) == 1: return tmp[0]
elif len(tmp) == 0: return SE_FALSE
return {"OR": tmp}
return {"LETTERS": exp["LETTERS"].copy(), "EXP": expl(exp["EXP"])}
def exp_replace_simp1 (exp, values):
return exp_simp1(exp_replace_multiple(exp, values))
def get_value (exp, values):
return exp_check(exp_replace_simp1(exp, values))
def create_table (exp):
letters = exp["LETTERS"]
if len(letters) == 0: return []
t = [[False], [True]]
for i in range(1, len(letters)):
t = [[False] + e for e in t] + [[True] + e for e in t]
q = []
for e in t:
b = {letters[i]: e[i] for i in range(len(e))}
q.append((b, get_value(exp, b)))
return q
# ---- SOLUTIONS FUNCTIONS ----
def is_same_base (s1, s2):
for e in s1:
if e in s2 and s1[e] != s2[e]: return False
return True
def union (s1, s2):
s = {}
for e in s1:
s[e] = s1[e]
for e in s2:
s[e] = s2[e]
return s
def get_simple_solutions (exp, val):
q = create_table(exp)
t = []
for e in q:
if e[1] == val: t.append(e[0])
return t
def update_solutions (s, si):
tmp = []
if len(s) == 0:
return si
for e in s:
for f in si:
if is_same_base(f, e):
tmp.append(union(e, f))
return tmp
def get_solutions (exps, vals):
s = get_simple_solutions(exps[0], vals[0])
for i in range(1, len(vals)):
tmp1 = get_simple_solutions(exps[i], vals[i])
s = update_solutions(s, tmp1)
return s
def solutions_always_same (s):
if len(s) == 0: return {}
t = {}
for l in s[0]:
tmp0 = s[0][l]
tmp1 = True
for e in s[1:]:
tmp1 = e[l] == tmp0
if not tmp1: break
if tmp1: t[l] = tmp0
return t
def solutions_remove (s, letters):
t = []
for e in s:
for f in letters:
e.pop(f)
if e != {}:
t.append(e)
return t