66
6
Say I have an expression:
9 * 8 + 1 - 4
This expression can be interpreted in six different ways, depending on operator precedence:
(((9 * 8) + 1) - 4) = 69 (* + -)
((9 * 8) + (1 - 4)) = 69 (* - +)
((9 * (8 + 1)) - 4) = 77 (+ * -)
(9 * ((8 + 1) - 4)) = 45 (+ - *)
((9 * 8) + (1 - 4)) = 69 (- * +)
(9 * (8 + (1 - 4))) = 45 (- + *)
Say I'm a developer, and I don't feel like memorizing precedence tables, etc., so I'm just going to guess.
In this case, the largest margin of error would be 45-77, which is a difference of 32. This means that my guess will only be off by a maximum of 32.
The challenge
Given an expression consisting of numbers and +
, -
, *
, /
(integer division) and %
, output the absolute difference of the largest and smallest possible value for that expression, based on the precedence of operators.
Specifications
- The input expression will not contain parenthesis and every operator is left-associative.
- The input expression will only contain nonnegative integers. However, subexpressions may evaluate to negatives (e.g.
1 - 4
). - You can take the expression in any reasonable format. For example:
"9 * 8 + 1 - 4"
"9*8+1-4"
[9, "*", 8, "+", 1, "-", 4]
[9, 8, 1, 4], ["*", "+", "-"]
- The input will contain at least 1 and at most 10 operators.
- Any expression that contains a division or modulo by 0 should be ignored.
- You can assume that modulo will not be given negative operands.
Test Cases
9 * 8 + 1 - 4 32
1 + 3 * 4 3
1 + 1 0
8 - 6 + 1 * 0 8
60 / 8 % 8 * 6 % 4 * 5 63
1@AndersKaseorg It looks like you're treating
%
as having two different precedences in your second example. – Esolanging Fruit – 2017-07-09T00:38:32.050@AndersKaseorg it may be that the second of your equations mixes the precedences of operators - at one point
*
beats%
and at another%
beats*
(i.e. there should only be 6 equations due to 3 distinct operators, not 120 due to 5 operators (or 6 numbers)). EDIT - ah I see Challenger5's edit said the same. – Jonathan Allan – 2017-07-09T00:44:16.8801Three of the 'six' are identical, as are another two. That leaves three actual cases, not six. – user207421 – 2017-07-09T12:13:39.080
3how
%
operator works on negative numbers? The way like C or Python or something else? – tsh – 2017-07-09T14:33:06.7908Just saying, you don't have to add the "and I'm lazy" part to your description. Just saying you're a developer is enough. :) – Gryphon – 2017-07-09T16:57:08.293
@EJP That's true, but in the 45 cases, as well as one of the 69 cases, the identical value comes from the semantics of
+
and-
, not because they are equivalent expressions. So really, it can be interpreted in three different ways. – Esolanging Fruit – 2017-07-09T20:39:07.9001
@tsh Any behavior. Do whatever you want. You can make demons fly out of my nose.
– Esolanging Fruit – 2017-07-10T07:11:26.670((60 / 8) % 8) * (6 % 4) * 5
=75,(60/8)%(8*6)%(4*5)
=7.5, so the last test case should be 67.5. What am I missing? – bartavelle – 2017-07-10T14:08:03.000@bartavelle Are you using
/
as real division instead of integer division? I get your answer (67.5) using real division, and the test answer (63) using integer division. – Brian J – 2017-07-10T17:43:59.310Ah that makes sense! – bartavelle – 2017-07-10T17:45:50.337
If you don't feel like memorizing precedence tables, just use APL/J/K! – Adám – 2017-07-11T09:41:11.643
@Adám My boss says that I can't use those languages, so this is my only hope. – Esolanging Fruit – 2017-07-11T21:00:06.090