20
3
I was browsing esolangs, and chanced upon this language: https://github.com/catseye/Quylthulg.
One interesting thing about this language, is that it doesn't use prefix, postfix, or infix, it uses all three of them, calling it "panfix" notation.
Here is an example. To represent normal infix 1+2
in panfix, it becomes: +1+2+
. Notice how the operator is both before, in between, and after the operands. Another example is (1+2)*3
. This becomes *+1+2+*3*
. Notice again how *
is in all three places with respect to the operands +1+2+
and 3
.
The Challenge
As you may have guessed, your task in this challenge is to convert an expression from infix to panfix.
A few clarifications:
- You only have to deal with the four basic operations:
+-*/
- You won't have to deal with the unary versions of those, only binary
- You have to deal with parenthesis
- Assume the normal precedence rules of
*/
then+-
and left associativity for all of them. - The numbers will be nonnegative integers
- You can optionally have a spaces in both the input and output
Test Cases
1+2 -> +1+2+
1+2+3 -> ++1+2++3+
(1+2)*3 -> *+1+2+*3*
10/2*5 -> */10/2/*5*
(5+3)*((9+18)/4-1) -> *+5+3+*-/+9+18+/4/-1-*
This is code-golf, so shortest code in bytes wins!
S.split\
`` should be[...S]
, although it may actually help to match on/\d+|./g
up-front and work on that instead. – Neil – 2016-07-02T00:27:15.697@Neil Thanks. I'll look into that. – PurkkaKoodari – 2016-07-04T09:33:14.537