Skip to content

Commit fdcabe7

Browse files
committed
Merge pull request mission-peace#102 from orsenthil/python/symbolexpressioneval
Dynamic Prgramming - Symbolic Expression Evaluation - Bring additional clarity
2 parents dc54380 + 498ae0d commit fdcabe7

File tree

1 file changed

+62
-45
lines changed

1 file changed

+62
-45
lines changed
Lines changed: 62 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
1-
# Let there be a binary operation for 3 symbols a, b, c and result of these binary operation given in a table.
2-
# Given an expression of these 3 symbols and a final result, tell if this expression can be parenthesize in certain
3-
# way to produce the final result.
4-
5-
class Holder(object):
6-
def __init__(self):
7-
self.value_holder = set()
8-
9-
def add(self, ch):
10-
self.value_holder.add(ch)
11-
12-
def values(self):
13-
return self.value_holder
14-
15-
def __str__(self):
16-
return repr(self.value_holder)
17-
18-
def __repr__(self):
19-
return self.__str__()
20-
21-
def evaluate_expression(input, index, expression, result):
22-
T = [[Holder() for i in range(len(expression))] for j in range(len(expression))]
23-
24-
for i,ch in enumerate(expression):
25-
T[i][i].add(ch)
26-
27-
for l in range(2, len(T) + 1):
28-
for i in range(len(T) - l + 1):
29-
j = i + l - 1
30-
for k in range(i, j):
31-
for ch in T[i][k].values():
32-
for ch1 in T[k+1][j].values():
33-
T[i][j].add(input[index[ch]][index[ch1]])
34-
35-
for ch in T[0][len(T) - 1].values():
36-
if result == ch:
1+
"""
2+
Problem Statement
3+
=================
4+
5+
Let there be a binary operation for 3 symbols a, b, c and result of these binary operation given in a table.
6+
Given an expression of these 3 symbols and a final result, tell if this expression can be parenthesize in certain
7+
way to produce the final result.
8+
9+
Complexity
10+
----------
11+
12+
* Run time Complexity: O(n^3)
13+
* SpaceL O(n^2)
14+
15+
Where n is the length of the expression.
16+
"""
17+
18+
19+
def evaluate_expression(expression_map, expression, result):
20+
expression_length = len(expression)
21+
T = [[set() for _ in range(expression_length)] for _ in range(len(expression))]
22+
23+
for idx, expr in enumerate(expression):
24+
T[idx][idx].add(expr)
25+
26+
# We take a sub expression of length 2 until the total expression length
27+
for sub_length in range(2, expression_length + 1):
28+
for left_index in range(0, expression_length - sub_length + 1):
29+
right_index = left_index + sub_length - 1
30+
# we split the expression at different k indices for the total sub-expression length and store the result.
31+
# at T[left_index][right_index]
32+
# Like bbc, will be treated for (b(bc) and ((bb) c) and the final result is stored in a set at T[0][2]
33+
for k in range(left_index, right_index):
34+
for expr1 in T[left_index][k]:
35+
for expr2 in T[k+1][right_index]:
36+
T[left_index][right_index].add(expression_map[(expr1, expr2)])
37+
38+
for expr in T[0][-1]:
39+
if result in expr:
3740
return True
3841

3942
return False
4043

4144
if __name__ == '__main__':
42-
index = {}
43-
index['a'] = 0
44-
index['b'] = 1
45-
index['c'] = 2
46-
47-
input = [['b', 'b', 'a'], ['c', 'b', 'a'], ['a', 'a', 'c']]
48-
print(evaluate_expression(input, index, 'bbbbac', 'a'))
49-
50-
45+
expressions = ['a', 'b', 'c']
46+
# expression table denotes the binary operation between two expression and its result.
47+
expression_table = [
48+
['b', 'b', 'a'],
49+
['c', 'b', 'a'],
50+
['a', 'a', 'c']
51+
]
52+
53+
# For convenience, we can modify it to be more explicit and use the expression table
54+
55+
expression_map = {
56+
('a', 'a'): 'b',
57+
('a', 'b'): 'b',
58+
('a', 'c'): 'a',
59+
('b', 'a'): 'c',
60+
('b', 'b'): 'b',
61+
('b', 'c'): 'a',
62+
('c', 'a'): 'a',
63+
('c', 'b'): 'a',
64+
('c', 'c'): 'c'
65+
}
66+
67+
assert True == evaluate_expression(expression_map, 'bbbbac', 'a')

0 commit comments

Comments
 (0)