13
3
Introduction
There comes a point in childhood when you think you've mastered adding and multiplying, then someone comes along and informs you that:
a * b + c = (a * b) + c != a * (b + c),
and that it wasn't as simple or linear a process as you were earlier taught. You learn that there exists something called the order of operations. This is a very important way of keeping some level of consistency and in expressions, without having parentheses getting in the way of everything.
Generic storyline
One day, you wake up to the sound of panic in the streets. An extremist group under the name "The 2560" (Short for "Organisation Against the Order of Operations", with a dorky hex-ish twist) have used their evil methods to take control over all of the nuclear weapons in the world. They are holding the whole planet hostage, and they have a simple demand: reverse the accepted order of operations or face eradication (parentheses are to maintain their priority). The new system is called PSADME (parentheses, subtraction/addition, division/multiplication, exponents), and expressions evaluate right-to-left:
a - b - c = a - (b - c) = a + c - b
Days pass, and the transition is in progress. Whilst mathematicians and physicists are all busy rewriting their equations, the computer scientists are faced with the task of changing the fashion in which mathematical expressions are interpreted by computers. You belong to a secret rebel programming group which aims to cause as much torment for the new global overlords - and, by chance, you are randomly selected by The 2560 and tasked to produce the benchmark calculation program.
Your mission
Write a program (or function) which takes a (numerical) mathematical expression as input, calculates the expression using PSADME as the order of operations and outputs the result. Expressions should evaluate right-to-left, so $$1 - 3 + 4 = 1 - 7 = -6.$$
For simplicity, all numbers provided will be integers, and the calculations will produce integer outcomes.
Rules and scoring
- The program should accept input up to 128 characters in length - if your language/platform has a lower maximum input length, that is an acceptable excuse.
- Standard loopholes are forbidden.
- The winning code will be chosen on 18th November (4 weeks from this post date).
- Feel free to post code that would not be considered golf-worthy. This is about fun. If you have an interesting way of doing this but can't golf it yourself (or by the nature of your method), you can post it anyway.
As per usual, the winning code is the one with least number of bytes, with some entertainment-value bonuses:
- -5 for avoiding any use of the characters in the provided expression: +,-,(,),^,*,/
- -5 for making the calculations take more than 5 minutes (but no more than 10 minutes) to calculate on a standard computer, without the method being obvious (using the clock or unnecessary loops); The aim is to convince the new overlords that you are not trying to disrupt their calculations of doom.
- -(5+N) for a direct offensive message (of length N, not including leading/trailing whitespace) about the members of The 2560 to be written in plain sight within your code, with some ridiculous explanation as to why it needs to be there. If it is removed, the code must not function correctly. Yes, free points for entertainment value.
Examples and explanations
[program] 2 - 2 - 2
2
2 - (2 - 2) = 2
[program] (2 + 2 * 3 + 3) / 3 + 3
4
(4 * 6)/(3 + 3) = 4
[program] 3 + 2 + 1 ^ 3
216
(3 + 2 + 1)^3 = 216
[program] -5^2
25
(-5)^2 = 25
[program] 32 / 8 * 3 - 1
2
32 / (8 * (3 - 1)) = 32 / 16 = 2
1 - 3 + 4 = 1 - 7
? Right to left would suggest so, but that's putting addition ahead of subtraction, contrary to PSADME, no? – LLlAMnYP – 2015-10-21T12:24:11.2801@LLlAMnYP Addition and subtraction are in the same "group", just like in PEMDAS, so they happen right to left. Same with multiply/divide. It's more like
P(SA)(DM)E
. – Geobits – 2015-10-21T12:33:21.4202The statement isn't meant to be processed right-to-left - rather, operations of equal precedence are evaluated right-first. So 4/2 = 2, 2-1 = 1, but a/bc = a/(bc) rather than the usual (a/b)*c. I hope this clears things up. – Jake – 2015-10-21T13:10:58.713
Probably the easiest way to do this is to write up a flex/bison or lex/yacc grammar. – None – 2015-10-21T18:47:17.493
Can we always guarantee that operators have a space on either side? If so, the
-5^2
example needs fixing. – Digital Trauma – 2015-10-21T18:55:18.413I hadn't intended on making the spaces guaranteed, but I will leave that up to you. So you may have the freedom of requiring spaces, requiring NO spaces or allowing arbitrary input - the latter is preferable, but I do not wish to invalidate your answer. – Jake – 2015-10-21T19:01:05.777
What, 3+3=9? (Example 2) – ev3commander – 2015-10-21T19:03:56.867
Maybe I'm actually part of The 2560 and wish to make addition and multiplication interchangeable in my next domination plan - or perhaps 6 and 9. Or maybe I'm more clumsy than I realised. Nevertheless, I will fix that. The result of the full expression is actually 4, of course - I checked that with the elite members of The 2560. – Jake – 2015-10-21T19:10:13.250
5You should change the acronym to PADME, since members of such an evil organisation would most certainly like the newer Star Wars trilogy more than the originals. It's also easier to remember. – mbomb007 – 2015-10-21T22:00:47.937
@JArkinstall yes, that clears stuff up. Awesome challenge. – LLlAMnYP – 2015-10-22T07:37:22.907