Build an algorithm to parse and process the dynamic expression based on the dictionary
provided. The expression basically consists of Operators, Variables, and Parentheses.
Parentheses have higher precedence (i.e the subexpression in the parentheses should be
executed first)
Basically, the algorithm should support two operators - AND (concatenates the two variables) &
OR (provides first variable value if it is available. Otherwise, go for the second variable)
The algorithm should parse the given expression and apply the respective dictionary values on
the expression and provides the respective computed results.
INPUTS OUTPUT
Sno Expression Dictionary Expected Results
1 A and B
2 A and C
3 D or B
4 A or B
5 A and B and C {'A':'Hello', 'B': 'World', 'C': 'Buddy'}
6 A and (B or C) {'A':'Hello', 'B': 'World', 'C': 'Buddy'}
7 A and (C or D) {'A':'Hello', 'B': 'World', 'C': 'Buddy'}
8 A and (B or C) and D {'A':'Hello', 'C': 'Buddy', ‘D’: ‘Welcome’}
def search(arr, x):
for index, value in enumerate(arr):
if value == x:
return index
return -1
arr = [1, 2,3,4]
x = 30
print(x, "is present at index",
search(arr, x))
Comments
Leave a comment