# Arbitrary PEMDAS [duplicate]

Scrolling through social media, one might encounter something like this:

We can see that, following PEMDAS order of operations, the answer is clearly 11 9.

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.

### The challenge

Given an expression and an order of operations, output the value it equals.

### Input

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 1.

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.

### Output

The inputted expression evaluated and rounded to at least two decimal places.

### Test cases

PEMDAS 4*3-2+1             =  9
PDEMAS 2*4^3+2             =  130
PMDASE 17^2-1*2            =  1
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.

• Will concatenation ever be used to indicate multiplication, or will there be an explicit * every time? (Note that this is precisely the issue that caused people to think that the answer to the original puzzle is 9, even though it really is 1: concatenation-notated multiplication has a higher precedence than division in practice.) – Greg Martin Feb 5 '17 at 3:26
• @GregMartin, no, there will never be any concatenation – Daniel Feb 5 '17 at 3:27
• I cannot understand for the life of me how the answer to the top equation is 9 if multiplication comes before division. Maybe I'll read the challenge again when I'm not so tired and understand it.... – ETHproductions Feb 5 '17 at 3:55
• @ETHproductions, the answer is 9 under normal PEMDAS, where for * and / it is whichever comes first from left to right (same for + and -). Normally, * and / have equal precedence. However, in this challenge, the precedence is decreasing from left to right in the acronym/input. – Daniel Feb 5 '17 at 4:03
• @ETHproductions: when kids are taught PEMDAS, they're taught that multiplication and division have the same predecence and should be evaluated left to right. However, as a practicing mathematician, I can report that this is oversimplistic: in practice, multiplication-by-juxtaposition does have higher precedence than division. Expressions like 1/3x and a/b(c-d) are never interpreted as (1/3)x or (a/b)(c-d). The ÷ symbol is not often used outside of school math, but even then, no practicing mathematician would look at 6÷2(1+2) and interpret it as (6÷2)(1+2). – Greg Martin Feb 5 '17 at 19:35

# Maxima, 61 67 bytes

f(O,E):=(for i:1 thru 5 do infix(O[7-i],i*20,i*20),eval_string(E));


A function that takes operators as a list of strings and the expression and returns the result.

Try it online!

Example:

f(["()","^","*","/","+","-"],"4*3-2+1");


result: 9

• That seems to have trouble with parens – Christian Sievers Feb 5 '17 at 15:46
• @ChristianSievers Thanks, answer updated. – rahnema1 Feb 5 '17 at 18:18