Evaluate a string arithmetic expression whose literals are positive integers and whose binary operators are +, -, *, and /. Return its integer value if every evaluated subexpression has a strictly positive integer result. If any literal, operand, or intermediate result fails that rule, return Boolean false for the whole expression; later operations cannot make that subexpression valid.
Supplied Decomposition Helper
Assume a helper split(s) is provided for expressions in this task. It decomposes an expression at its outermost operation and returns either a one-element tuple containing a literal expression or a three-element tuple (left_expression, operator, right_expression). The returned operand expressions may themselves need evaluation.
split("3 / 5") -> ("3", "/", "5")
split("5") -> ("5",)
split("(10 / 2) / 5") -> ("(10 / 2)", "/", "5")
The helper determines the decomposition of a valid input expression. This task does not ask you to implement a parser. The source does not specify the helper's behavior for malformed expressions or every unparenthesized operator combination.
Arithmetic and Output Rules
-
A literal must be a positive integer after permitted surrounding whitespace and enclosing parentheses are removed. Zero is invalid.
-
Addition and multiplication use exact integer arithmetic. Subtraction is invalid if its result is zero or negative.
-
Division is invalid if the divisor is zero or if the dividend is not exactly divisible by the divisor. Do not round or truncate a fractional quotient.
-
Return the final positive integer, or Boolean
false
when any subexpression is invalid.
Examples
expression = "(3 + (3 * 5)) / 2"
result = 9
expression = "((3 - 5) + 12) / 2"
result = false
expression = "8 / 12"
result = false
expression = "3 - 3"
result = false
In the second example, the subtraction yields a negative intermediate result. In the third, division is not exact. The fourth is invalid because this task requires strictly positive results; zero from subtraction is invalid too.