-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathsymbolexpressionevaluation.py
67 lines (53 loc) · 2.17 KB
/
symbolexpressionevaluation.py
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
"""
Problem Statement
=================
Let there be a binary operation for 3 symbols a, b, c and result of these binary operation given in a table.
Given an expression of these 3 symbols and a final result, tell if this expression can be parenthesize in certain
way to produce the final result.
Complexity
----------
* Run time Complexity: O(n^3)
* SpaceL O(n^2)
Where n is the length of the expression.
"""
def evaluate_expression(expression_map, expression, result):
expression_length = len(expression)
T = [[set() for _ in range(expression_length)] for _ in range(len(expression))]
for idx, expr in enumerate(expression):
T[idx][idx].add(expr)
# We take a sub expression of length 2 until the total expression length
for sub_length in range(2, expression_length + 1):
for left_index in range(0, expression_length - sub_length + 1):
right_index = left_index + sub_length - 1
# we split the expression at different k indices for the total sub-expression length and store the result.
# at T[left_index][right_index]
# Like bbc, will be treated for (b(bc) and ((bb) c) and the final result is stored in a set at T[0][2]
for k in range(left_index, right_index):
for expr1 in T[left_index][k]:
for expr2 in T[k+1][right_index]:
T[left_index][right_index].add(expression_map[(expr1, expr2)])
for expr in T[0][-1]:
if result in expr:
return True
return False
if __name__ == '__main__':
expressions = ['a', 'b', 'c']
# expression table denotes the binary operation between two expression and its result.
expression_table = [
['b', 'b', 'a'],
['c', 'b', 'a'],
['a', 'a', 'c']
]
# For convenience, we can modify it to be more explicit and use the expression table
expression_map = {
('a', 'a'): 'b',
('a', 'b'): 'b',
('a', 'c'): 'a',
('b', 'a'): 'c',
('b', 'b'): 'b',
('b', 'c'): 'a',
('c', 'a'): 'a',
('c', 'b'): 'a',
('c', 'c'): 'c'
}
assert True == evaluate_expression(expression_map, 'bbbbac', 'a')