Evaluating Circuit Expressions
Learn this problemProblem statement
Given a list circuitsExpression of valid circuit-expression strings, evaluate every expression and return its circuit value.
A circuit expression uses the following symbols:
[and]group an operation and determine precedence.&denotes logical AND.|denotes logical OR.!denotes logical NOT.1denotes logical true.0denotes logical false.
Every bracketed expression places its operator first:
[!, x]evaluates to NOTx.[&, x, y]evaluates toxANDy.[|, x, y]evaluates toxORy.
An operand may be 0, 1, or another bracketed expression. Commas separate entries, and whitespace may appear between tokens.
Return a list of integers containing the value of each expression, in the same order as circuitsExpression. Every returned value is either 0 or 1.
Function
circuitsOutput(circuitsExpression: List<String>) → List<Integer>Examples
Example 1
circuitsExpression = ["[|, [&, 1, [!, 0]], [!, [|, [|, 1, 0], [!, 1]]]]"]return = [1]Evaluate the innermost operations first. The left branch is 1 AND NOT 0 = 1. The right branch is NOT ((1 OR 0) OR NOT 1) = NOT 1 = 0. The outer OR therefore evaluates to 1 OR 0 = 1.
Example 2
circuitsExpression = ["[!, 1]","[&, [|, 1, 0], [!, 0]]","[|, [&, 1, 0], [!, 1]]"]return = [0,1,0]The three expressions evaluate independently: NOT 1 = 0, (1 OR 0) AND NOT 0 = 1, and (1 AND 0) OR NOT 1 = 0.
Example 3
circuitsExpression = ["[!, [!, [!, 0]]]","[|, [&, 1, 1], [!, [|, 0, 1]]]"]return = [1,1]Three consecutive NOT operations applied to 0 produce 1. In the second expression, the left operand is 1, so the outer OR is also 1.
Constraints
- The total number of expressions is less than
10. - Each expression contains no more than
10^6characters. - Every expression is valid and contains only
0,1,[,],&,|,!, commas, and whitespace.