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

Very easy expression to simplify

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.


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.


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

This is , so shortest code in bytes wins!

Test cases

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.

  • \$\begingroup\$ 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.) \$\endgroup\$ Feb 5, 2017 at 3:26
  • \$\begingroup\$ @GregMartin, no, there will never be any concatenation \$\endgroup\$
    – Daniel
    Feb 5, 2017 at 3:27
  • 1
    \$\begingroup\$ 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.... \$\endgroup\$ Feb 5, 2017 at 3:55
  • 2
    \$\begingroup\$ @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. \$\endgroup\$
    – Daniel
    Feb 5, 2017 at 4:03
  • 1
    \$\begingroup\$ @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). \$\endgroup\$ Feb 5, 2017 at 19:35

1 Answer 1


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!



result: 9

  • \$\begingroup\$ That seems to have trouble with parens \$\endgroup\$ Feb 5, 2017 at 15:46
  • \$\begingroup\$ @ChristianSievers Thanks, answer updated. \$\endgroup\$
    – rahnema1
    Feb 5, 2017 at 18:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.