Skip to content

Commit

Permalink
solving for sub equations
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Dec 15, 2022
1 parent 9270aaa commit c3ce432
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def __str__(self):
class Equation:
def __init__(self):
self.eq = []
self.sub_eqs = []
self.table = None

def solve(self, *args):
Expand All @@ -166,8 +167,19 @@ def generate_truth_table(self):
"x + y": [0, 1, 1, 1],
}
"""

if self.table is not None:
# the equation is already solved
return self.table

# solve all the sub equations
# Note: solved sub equations is not used or solving sub equations is not necessary to solve given equation
for i in self.sub_eqs:
i.generate_truth_table()

variable_count = 0
variables = []
sub_eq_strings = []
table = dict()

eq_string = str(self)
Expand All @@ -181,6 +193,11 @@ def generate_truth_table(self):
combinations = 2**variable_count
table[eq_string] = [0] * combinations # to store result

for i in self.sub_eqs:
sub_eq_str = str(i)
table[sub_eq_str] = [0] * combinations
sub_eq_strings.append(sub_eq_str)

for i in range(combinations):
results_list = table[eq_string]
eval_string = eq_string
Expand All @@ -202,6 +219,29 @@ def generate_truth_table(self):
else 0
)

for eq in sub_eq_strings:
eq_string = str(eq)

for i in range(combinations):
eval_string = eq_string
results_list = table[eq_string]

for j in range(variable_count):
eval_string = eval_string.replace(
str(variables[j]), "1" if i & (1 << j) else "0"
)

print(eval_string)
results_list[i] = (
1
if eval(
eval_string.replace("+", "or")
.replace("*", "and")
.replace("~", "not")
)
else 0
)

self.table = table

return table
Expand Down Expand Up @@ -239,7 +279,7 @@ def __add__(self, other):
return eq

if isinstance(other, Equation):
# adding exisiting operations
# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)
Expand All @@ -251,6 +291,11 @@ def __add__(self, other):
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)

return eq

raise TypeError("...")
Expand Down Expand Up @@ -280,11 +325,15 @@ def __radd__(self, other):
# add operator
eq.eq.append(ADD)

# adding exisiting operations
# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)

return eq

raise TypeError("...")
Expand All @@ -306,7 +355,7 @@ def __mul__(self, other):
return eq

if isinstance(other, Equation):
# adding exisiting operations
# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)
Expand All @@ -318,6 +367,11 @@ def __mul__(self, other):
eq.eq.append(OB)
eq.eq.extend(other.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)

return eq

raise TypeError("...")
Expand Down Expand Up @@ -347,11 +401,15 @@ def __rmul__(self, other):
# add operator
eq.eq.append(MUL)

# adding exisiting operations
# adding existing operations
eq.eq.append(OB)
eq.eq.extend(self.eq)
eq.eq.append(CB)

# adding sub equations
eq.sub_eqs.append(self)
eq.sub_eqs.extend(self.sub_eqs)

return eq

raise TypeError("...")
Expand Down Expand Up @@ -384,5 +442,4 @@ def __str__(self):
y = Variable("y")
z = Variable("z")

# (x + (y * z)).display_table()
(~x + (y * z)).display_table()
((y * z) + (x * y)).display_table()

0 comments on commit c3ce432

Please sign in to comment.