| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | Perl 5 | 250327T155541Z | Xcali |
| 015 | Japt h | 170920T131231Z | Shaggy |
| 013 | Pyth | 180310T032859Z | hakr14 |
| 082 | C gcc | 180803T221558Z | Jonathan |
| 029 | plain TeX | 170921T110334Z | Manuel |
| 035 | PowerShell | 170920T130113Z | AdmBorkB |
| 012 | SOGL V0.12 | 170920T130317Z | dzaima |
| 012 | Jelly | 170920T154843Z | Erik the |
| 013 | 05AB1E | 170920T154703Z | Erik the |
| 032 | Python 2 | 170920T150729Z | Erik the |
| 037 | JavaScript ES6 | 170920T125617Z | Neil |
| 2120 | MATL | 170920T141304Z | Cinaski |
| 017 | 05AB1E | 170920T140616Z | Emigna |
| 167 | Java OpenJDK 8 | 170920T131124Z | Roberto |
| 040 | Wolfram Language Mathematica | 170920T123802Z | Vitaliy |
Japt -h, 21 20 18 16 15 bytes
The 5 at the beginning can be removed to be able to take the level as input.
5Æ="\{^_}"¬qUgx
Outdated Explanation
5Æ="\{^_}"¬qUª'x
5Æ :Map the range [0,5)
= : Assign to variable U
"\{^_}" : Literal string (the "{" is escaped as it's used for string interpolation in Japt)
¬ : Split
q : Join with
Uª'x : Logical OR of U (initially 0) and "x"
:Implicit output of last element
Pyth, 17 16 13 bytes
jF+\x*5]"{^_}
Python 3 translation:
from functools import*;print(reduce(lambda x,y:x.join(y),["x"]+5*["{^_}"]))
C (gcc), 82 bytes
O(o){o--?_(125,O(o,_(95,O(o,_(94,O(o,_(123))))))):_(120);}main(){O(5);}
plain TeX, 29 bytes
\def~#1x{{#1x_#1x^#1x}}~~~~~x
That outputs what others have output. But if we need the code to be compilable it would be 6 bytes more
\def~#1x{{#1x_#1x^#1x}}$~~~~~x$\bye
Explanation
~ is an active character in TeX, so we can give it a (new) definition.
\def~#1x{{#1x_#1x^#1x}} defines ~ as a macro, so that when TeX sees ~, it does the following:
- Read everything up to the next
x, and call that#1(pattern-matching). - Replace the whole thing with
{#1x_#1x^#1x}
For example, ~ABCx would get replaced with {ABCx_ABCx^ABCx}.
When ~~~~~x is used, #1 is ~~~~, so the whole thing gets replaced with {~~~~x_~~~~x^~~~~x}. And so on.
Once we have the long string, we can print it out to terminal with \message (and ending with a \bye so TeX stops), so \message{~~~~~x}\bye. Or typeset the resulting expression (as a mathematical formula), by surrounding it in $s : so $~~~~~x$\bye.
PowerShell, 44 35 bytes
"'x'"+"-replace'x','{x^x_x}'"*5|iex
Uses string multiplication to repeatedly -replace xes with the sub- and super-scripts, then output.
Saved 9 bytes thanks to Joey.
JavaScript (ES6), 45 42 37 bytes
f=n=>n>4?'x':[...'{^_}'].join(f(-~n))
Edit: Saved 3 2 bytes thanks to @Arnauld. Specifying 5 still costs me 2 bytes; this 41 40 35-byte version takes a parameter instead:
f=n=>n?[...'{^_}'].join(f(n-1)):'x'
05AB1E, 17 bytes
'x5F'x¡"{x^x_x}"ý
Explanation
'x # push "x"
5F # 5 times do
'x¡ # split on "x"
"{x^x_x}"ý # join on "{x^x_x}"
Other programs at the same byte-count include
"{x^x_x}"©4F'x¡®ý
'x5F'x"{x^x_x}".:
Java (OpenJDK 8), 179 167 bytes
@Neil port
interface Y{static void main(String[]a){System.out.println(t.apply(1));}java.util.function.Function<Integer,String>t=N->N>0?Y.t.apply(N-1).replace("x","{x^x_x}"):"x";}
Wolfram Language (Mathematica) - 40 characters
Summarizing 3 best answers here:
40 bytes:
Nest["{"<>#<>"_"<>#<>"^"<>#<>"}"&,"x",5]
41 bytes:
Nest[StringReplace["x"->"{x^x_x}"],"x",5]
44 bytes:
Last@SubstitutionSystem["x"->"{x^x_x}","x",5]