CGCC hasn't always had MathJax. Back in the dark ages, it would have been necessary to write \$x^2\$ as x²
(the horror!). In this challenge, you will be given some math which may include superscripts, and you should convert it to MathJax.
Input:
Input will consist of one or more letters a
to z
, some with superscripts. Answers may choose to handle only upper or lower case, or both.
Examples of inputs would be x
, x²
, xy²
, x²y²
, or x²yz
. There will never be duplicates of a letter (so xx
or x²zx
won't be given), but you cannot assume the superscripts or letters are in any particular order. The first character in the input will never be a superscript.
Superscripts consist of the characters ¹
, ²
, ³
, ⁴
, ⁵
, ⁶
, ⁷
, ⁸
, ⁹
, and ⁰
. These can be joined into multi-digit superscripts, like x²¹ (\$x^{21}\$). You can assume ⁰
or ¹
are never given as superscripts, and there will not be leading ⁰
s.
Output:
Output should consist of a MathJax representation of the input. This must start and finish with \$
.
Characters without superscripts can be written as themselves; xyz
would simply become \$xyz\$
. Characters with superscripts should be followed by ^
, then the superscript written with normal digits and wrapped in {}
. For example, x²¹z⁴⁸⁸
would become \$x^{21}z^{488}\$
. Optionally, single digit superscripts can be written without the {}
. For example, x²
could be either \$x^{2}\$
or \$x^2\$
.
Test cases:
x \$x\$
xyz \$xyz\$
x² \$x^{2}\$ OR \$x^2\$
x²¹ \$x^{21}\$
x²yz² \$x^{2}yz^{2}\$ OR \$x^2yz^2\$
xy¹¹z \$xy^{11}z\$
x⁴ \$x^{4}\$ OR \$x^4\$
x⁶⁰ \$x^{60}\$
Other:
This is code-golf, shortest answer (in bytes) per language wins!
x⁶⁰ \$x^{40}\$
?? how does 60 turn into 40? \$\endgroup\$