Scrolling through social media, one might encounter something like this:
We can see that, following PEMDAS order of operations, the answer is clearly
For the sake of this challenge, PEMDAS means that multiplication will occur before division, so
a/b*c = a/(b*c), not
(a/b)*c. The same goes for addition and subtraction. For example, using PEMDAS,
1-2+3 = -4, but using PEMDSA,
1-2+3 = 2.
However, the answer quickly changes if we use some other order of operations, say PAMDES: now the answer is 12.
Given an expression and an order of operations, output the value it equals.
The input will contain two parts: the expression and the order of operations.
The expression will contain only
[0-9] ( ) ^ * / + -. This means that it will contain only integers; however, you may not use integer division, so
4/3=1.33 (round to at least two decimal places), not
The order of operations may be taken in any reasonable format. For example, you may take a string array of operations (ex.
["()", "^", ... ]), or something like
PEMDAS. Parenthesis will always have the highest priority, and will therefore always come first in the input.
The inputted expression evaluated and rounded to at least two decimal places.
This is code-golf, so shortest code in bytes wins!
PEMDAS 4*3-2+1 = 9 PDEMAS 2*4^3+2 = 130 PMDASE 17^2-1*2 = 1 PSEMAD 32*14/15-12+5^3+1/2 = 1.736 PEMDAS 6/2*(1+2) = 1 PEDMAS 6/2*(1+2) = 9 PDMSEA 2*(1*13+4)^3 = 39304
1. No joke: many thought the answer was 1.
2. Here we are using the "for the sake of this challenge" PEMDAS rules.