Let's assume you are building a programming language for only boolean values, that is, true and false: true, false
(Variables: a, b, c... (booleans))
- You support the 3 boolean operations, AND (&&), OR (||), and NOT (!):
a && b
a || b
!a
!(a || (b && !c))
- You can represent an expression as a tree:
&&
/ \
a b
!
/
||
/\
a &&
/\
b !
/
c
Or using reverse Polish notation:
["a", "b", "&&"]
["a", "!"]
[a, b, c, !, &&, ||, !]
PROBLEM: Now write a function evaluate that takes an object representing the expressions, along with a dictionary of variables and their corresponding values, so that the function returns the final evaluated value of the expression:
evaluate(expression, dict : string -> boolean) --> true/false
evaluate(a || b, { a : true, b : false }) --> true
evaluate(!!a, { a : false }) --> false