Japt,62 60 55 52 51字节
V=Us1 n;U<'t?Vo ç*pV):0oV2 £S²pY iY'*pV-X})·z2*!Uf-
在线尝试!
我们需要做的第一件事是弄清楚我们的形状需要多大。这很简单:
// Implicit: U = input string, S = space
V= // Set variable V to
Us1 // everything after the first char of U,
n; // converted to a number. This turns e.g. "12+" into 12.
现在,我们组织输出的形状:
U<'t? // If U comes before "t" lexicographically (here, if the first char is "s"),
Vo // make a list of V items,
ç*pV) // and set each item to V asterisks.
:0oV2 // Otherwise, create the range [0, V) with steps of 2 (e.g. 7 -> [0,2,4,6]),
£ }) // and map each item X and index Y to:
S²pY // Repeat 2 spaces Y times. This creates a string of Y*2 spaces.
iY'*pV-X // At position Y in this string (right in the middle), insert V-X asterisks.
· // Join with newlines.
到目前为止,我们已经考虑了输出的大小和形状。剩下的就是旋转。当前已指向三角形,因此如果第三个字符为+
:,我们需要翻转三角形:
!Uf- // Take the logical not of U.match("-").
// If U contains "-", this returns false; otherwise, returns true.
2* // Multiply by two. This converts true to 2, false to 0.
z // Rotate the list 90° that many times.
// Altogether, this turns the shape by 180° if necessary.
有了隐式输出,我们的工作就完成了。:-)