Given an algebraic expression (eg
2ab, change it so it follows standard convention rules.
- Letters should be in alphabetical order. ie
2abis allowed but not
- Numbers should always go first. ie
2abis allowed but
- Exponentiation will be given as
ato the power of
- These rules must be applied to every sub-expression. ie
2ab + ba3becomes
2ab + 3ab,
2vd * vd5becomes
2dv * 5dv. This must work for at least addition, subtraction, multiplication, division, modulus and exponentiation.
- Exponentiation is assumed to belong to the variable immediately preceding it and must stay with the variable it is assigned to. ie
2a^2b. This means that all variables to the right of
^are part of the exponentiation.
Input can be given with any amount of white space, which must be ignored. Output can use either one space or none. For example,
2ab +3ba could become
2ab + 3ab or
Input -> Output 2ba -> 2ab 4gb + 5vh * gh7 -> 4bg + 5hv * 7gh 2ba^2 -> 2a^2b 5xk^2ba -> 5k^2abx
(The last couple of outputs don't make sense mathematically because of order of operations, but you don't have to worry about that.)
Standard code golf, shortest answer in bytes wins. Answer can be a program which takes input, or a function (can be anonymous).